Brain Marker
Brain Marker

Reputation: 83

Find two maximum peaks in a histogram according to some condition

In the first two images, among all the peaks found using matlab function 'findpeaks', i have marked in 'red' the peaks that i want to find, the condition is to find location of two maximum peaks having minimum values, between them.

enter image description here

For the case of the histograms below, the condition is not satisfied, so, there is no two peaks.

enter image description here Any ideas, Thank you

The histogram values of the tow first images :

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5,60000000000000 11,4000000000000 17,2000000000000 23,6000000000000 30,4000000000000 33,6000000000000 38 44,2000000000000 47,0000000000000 45,6000000000000 37,4000000000000 27,4000000000000 15,4000000000000 6,20000000000000 2,20000000000000 3,60000000000000 5,40000000000000 7 7,60000000000000 6,20000000000000 4,20000000000000 2,20000000000000 1 1,20000000000000 2,20000000000000 3,60000000000000 5,80000000000000 7,80000000000000 9 9,20000000000000 8,60000000000000 7,20000000000000 5,60000000000000 5,60000000000000 8 12 16,8000000000000 22,2000000000000 25,8000000000000 27,2000000000000 26,8000000000000 25,2000000000000 22,8000000000000 22 22,4000000000000 23,6000000000000 28,2000000000000 34,6000000000000 38,4000000000000 40 37,6000000000000 29 18,8000000000000 10,2000000000000 3,20000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0,600000000000000 1,40000000000000 2,20000000000000 3 3,80000000000000 4 4 4,20000000000000 4,20000000000000 4,20000000000000 4,20000000000000 4,20000000000000 4 4 4 4 4,20000000000000 4,40000000000000 4,60000000000000 4,80000000000000 5,20000000000000 5,40000000000000 5,60000000000000 5,80000000000000 5,80000000000000 5,80000000000000 5,60000000000000 5,40000000000000 5 4,60000000000000 3,40000000000000 2,40000000000000 1,40000000000000 0,600000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0,200000000000000 0,400000000000000 0,600000000000000 1 1,40000000000000 1,60000000000000 1,80000000000000 3,20000000000000 4,60000000000000 5,80000000000000 6,80000000000000 7,40000000000000 6,80000000000000 6,20000000000000 5,80000000000000 5,40000000000000 5,20000000000000 5,20000000000000 5 4,80000000000000 5 6,20000000000000 7,40000000000000 7,20000000000000 6,80000000000000 5,60000000000000 3,60000000000000 1,40000000000000 0,600000000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Upvotes: 0

Views: 2066

Answers (3)

mmoment
mmoment

Reputation: 1289

You can pre-process our data to sort out all values below a threshold:

data(data<threshold) = 0

then you can use

[maxval, index] = max(data)

to get the maximum and the index. Next you can look for peaks below or equal to the maxval, for example like this:

data(index) = 0; [maxval, index] = max(data)

which will give you the next peak (also of equal value) and index of it.

By repeating this process you can then find the indices and values of all the peaks, from largest to smallest.

Upvotes: 1

Brain Marker
Brain Marker

Reputation: 83

Thank a lot you for your answers, this works very well

    S % the Histogram
    TH % threshold
    [pks,locs] = findpeaks(S,'SortStr','descend'); % find peaks
            if size(locs,1)>=2
                for j=1:size(locs,1)-1
                    if locs(j+1)<locs(1)
                        Interval=S(locs(j+1):locs(1));
                    else
                        Interval=S(locs(1):locs(j+1));
                    end 
                    minimum=min(Interval);
                    if minimum<=TH;
                        plot(locs(j+1),pks(j+1),'go','lineWidth',4); 
                        plot(locs(1),pks(1),'go','lineWidth',4); 
                        break;
                    end
                end
            end

Upvotes: 0

rst
rst

Reputation: 2714

Polystep approach

Step 1, define your threshold and find all values below/above

minval = 20;
f = zeros(size(h));
f(h<minval) = 1;

step 2, use this code Split vector in MATLAB and remove empty cells

id = cumsum(f)+1;   
mask = f==0;        
groups = accumarray(id(mask).',h(mask).',[],@(x) {x});
groups(isempty(groups)) = [];
groups=groups(~cellfun('isempty',groups));  

find all max values

maxvals = cellfun(@(x) max(x), groups)

Upvotes: 1

Related Questions