plot regression line through first 5 peaks

I am trying to plot the first 5 peaks on the PSD and I am having trouble getting the peaks to plot in their correpsonding locations. I was wondering how to go about this.
[maxpks, maxloc]=findpeaks(PSD);
[s,t]=size(maxloc);
figure(1)
plot(fp,PSD)
hold on
fpml=fp(maxloc);
plot(fpml, maxpks, 'r*')
hold on
sortpks=sort(maxpks);
sortlocs=sort(maxloc,'descend');
[j,h]=size(sortpks);
J= [sortpks(j),sortpks(j-1),sortpks(j-2), sortpks(j-3), sortpks(j-4)]; %first five peak
ML=[sortlocs(j),sortlocs(j-1),sortlocs(j-2),sortlocs(j-3),sortlocs(j-4)];
ML=[maxloc(s-4),maxloc(s-3),maxloc(s-2),maxloc(s-1),maxloc(s)]
plot(J,'y*')
Fit = polyfit(ML,J,1); %linear regression
yfit = Fit(1)*ML+Fit(2); %equation of line
hold on
plot(ML,yfit,'g-')
hold off

 Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 10 Mär. 2021

0 Stimmen

hello
you can directly ask findpeaks to search only the 5 largest peaks :
[...] = findpeaks(...,'NPeaks',NP) specifies the maximum number of peaks
to be found. NP is an integer greater than zero. If not specified, all
peaks are returned. Use this parameter in conjunction with setting the
sort direction to 'descend' to return the NP largest peaks. (see
'SortStr')

4 Kommentare

Ashlyn McCann
Ashlyn McCann am 11 Mär. 2021
So this find the first 5 peaks but not the first 5 largest . I drew 5 blue marks from the peaks I need to pick out.
Mathieu NOE
Mathieu NOE am 11 Mär. 2021
try with following sorting options
[peaks,locs] = findpeaks('NPeaks',5,'SortStr','descend');
Ashlyn McCann
Ashlyn McCann am 11 Mär. 2021
It worked! Thank you!
Mathieu NOE
Mathieu NOE am 11 Mär. 2021
My pleasure !

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by