I'm having trouble trying to find the 4 dominant peaks in this graph

The signal is values are very jittery, in that they go up then down, making it hard to find the maximum value and it's index.
function [peaks, locations] = findMaxs (mag, threshold)
len = length(mag);
prev = 1;
cur = 2;
next = 3;
k = 1; %number of peaks
while next < len
if mag(cur) - mag(prev) > threshold
if mag(cur) > mag(next)
peaks(k) = mag(cur);
fprintf('peak at %d\n', cur);
k = k + 1;
end
end
prev = cur;
cur = next;
next = next + 1;
end
end
findpeaks() gave me way too many results, so I'm using this function. However, if I set the threshold too low, I get too many results, and if I set it even very slightly too high, I miss one of the dominant peaks.
How can I do this?
If your dominant peaks are seperated like in the plot you included, there is a parameter for findpeaks() that can help a whole lot. Try:
findpeaks(x, 'MINPEAKDISTANCE', dist);
with x being your magnitudes and dist being a distance you can assume to be te smallest distance between 2 peaks. This might give you a false peek in between 2 peek that are more than 2*dist from each other, if so consider adding a small threshold with 'MINPEAKHEIGHT'
Another Option is calulating your threshold dynamicly, for exsample by calulating the mean m and the standard deviation sigma and setting a threshold by only counting peaks that are n*sigma above m.
you can still use findpeaks.
for example [pks,locs] = findpeaks(data) returns the indices of the local peaks.
then you can sort data(locs) and get the top 4 amplitudes.
[a ind]=sort(data(locs,'descend')
or set a threshold, data(locs)>threshold etc...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With