I have a signal and I only want the top two or three local maxima, I don't want the 5000 maxima that the findpeaks() function gives me. Is there a way to get this? Thanks.
Prashant Kumar answered .
2025-11-20
function[val,idx]=nthMaxima(P0,n)
%P0 is the array for searching maximus in there
%n is the number of top maximus to find
val=zeros(1,n); %defining array for reserving value of maximus
idx=zeros(1,n); %defining array for index of maximus
for i=1:n
[val(i),idx(i)]=max(P0); %taking maximu value and indexing
P0(idx(i))=-Inf; %replacing maxima index with -Inf to search for maxima again
end
%recovering the replaced maxima value
for j=1:n
P0(idx(j))=max(j);
end
%done!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
end