# Find peaks not working on my data set

4 views (last 30 days)
Ashlyn McCann on 9 Mar 2021
Commented: Steve Eddins on 9 Mar 2021
I am trying to find the first 5 peaks in the PSD plot of an acoustic signal. While I do not have any bugs in my code, it is not finding actual peaks and looks more like arbitrary locations. I will paste my code below:
M = movmean((PSD), 50);
[maxpks, maxloc]=findpeaks(PSD(1:2000),'MinPeakDistance',195,'MinPeakHeight',-50,'NPeaks',5);
figure(1)
plot(fp,PSD)
hold on
%O=2*f %%convert to octaves so the slope is in dB/octave
plot((maxloc), maxpks, 'r*')
hold off
sortpks=sort(maxpks);
[j,h]=size(sortpks);
J= [sortpks(j),sortpks(j-1),sortpks(j-2), sortpks(j-3), sortpks(j-4)]; %first five peak
ML = maxloc(j-4:j); %first five peak location
ML=(ML)'; %%convert to octaves so the slope is in dB/octave
figure(2)
plot(ML,J,'r*')
Fit = polyfit(ML,J,1); %linear regression
yfit = Fit(1)*ML+Fit(2); %equation of line
hold on
plot(ML,yfit,'g-')
slope=Fit(1)%spectral tilt The picture above is a zoomed in version of figure 1 and it is just not detecting the peaks I need. Thank you in advanced for any help!

Steve Eddins on 9 Mar 2021
I suspect the issue is with the x-coordinates that you are using to plot the peaks. You are plotting the peaks against the indices returned by findpeaks, but you are plotting PSD against x-coordinates in the variable fp. So, intead of this:
plot((maxloc), maxpks, 'r*')
Try this:
plot(fp(maxloc), maxpks, 'r*')
##### 2 CommentsShowHide 1 older comment
Steve Eddins on 9 Mar 2021
Great! If this answer worked for you, can you go ahead and accept it?