how does the function findpeaks work in matlab?

10 visualizaciones (últimos 30 días)
yawen kang
yawen kang el 3 de Mayo de 2017
Comentada: Rik el 3 de Mayo de 2017
I use the function findpeaks to detect peak in a vector,it worked as i expected,but I want to understand how does it work,and what the principle it use.
n=0:1:10;
x = [25 8 15 5 6 10 10 3 1 20 7];
[k,v]=findpeaks(x);
plot(n,x,'b');grid;hold on
plot(n(k),v,'r.');
Can anyone can help me?I will be very appreciate for your kindness.

Respuestas (2)

Rik
Rik el 3 de Mayo de 2017
Excerpt from the documentation:
pks = findpeaks(data) returns a vector with the local maxima (peaks) of the input signal vector, data. A local peak is a data sample that is either larger than its two neighboring samples or is equal to Inf. Non- Inf signal endpoints are excluded. If a peak is flat, the function returns only the point with the lowest index.
So that's how it works.
  2 comentarios
yawen kang
yawen kang el 3 de Mayo de 2017
first,thanks for your answer.but I already know how to use it.I want to know what algorithm it use to find the peak.I want to understand what the code in erery line mean,and what principle it use to find the peak.
n=0:1:10; x = [25 8 15 5 6 10 10 3 1 20 7]; nx=length(x); x=x(:); dx=x(2:end)-x(1:end-1); r=find(dx>0); f=find(dx<0); if length(r)>0 & length(f)>0 % we must have at least one rise and one fall dr=r; dr(2:end)=r(2:end)-r(1:end-1); rc=repmat(1,nx,1); rc(r+1)=1-dr; rc(1)=0; rs=cumsum(rc); % = time since the last rise df=f; df(2:end)=f(2:end)-f(1:end-1); fc=repmat(1,nx,1); fc(f+1)=1-df; fc(1)=0; fs=cumsum(fc); % = time since the last fall rp=repmat(-1,nx,1); rp([1; r+1])=[dr-1; nx-r(end)-1]; rq=cumsum(rp); % = time to the next rise fp=repmat(-1,nx,1); fp([1; f+1])=[df-1; nx-f(end)-1]; fq=cumsum(fp); % = time to the next fall k=find((rs<fs) & (fq<rq) & (floor((fq-rs)/2)==0)); % the final term centres peaks within a plateau v=x(k); else k=[]; v=[]; end
Jan
Jan el 3 de Mayo de 2017
@yawen: It seems like you noticed, that the code is not readable. Please edit your comment and use the "{} Code" button for a proper formatting. Thanks.

Iniciar sesión para comentar.


yawen kang
yawen kang el 3 de Mayo de 2017
first,thanks for your answer.but I already know how to use it.I want to know what algorithm it use to find the peak.I want to understand what the code in erery line mean,and what principle it use to find the peak.
n=0:1:10;
x = [25 8 15 5 6 10 10 3 1 20 7];
nx=length(x);
x=x(:);
dx=x(2:end)-x(1:end-1);
r=find(dx>0);
f=find(dx<0);
if length(r)>0 & length(f)>0 % we must have at least one rise and one fall
dr=r;
dr(2:end)=r(2:end)-r(1:end-1);
rc=repmat(1,nx,1);
rc(r+1)=1-dr;
rc(1)=0;
rs=cumsum(rc); % = time since the last rise
df=f;
df(2:end)=f(2:end)-f(1:end-1);
fc=repmat(1,nx,1);
fc(f+1)=1-df;
fc(1)=0;
fs=cumsum(fc); % = time since the last fall
rp=repmat(-1,nx,1);
rp([1; r+1])=[dr-1; nx-r(end)-1];
rq=cumsum(rp); % = time to the next rise
fp=repmat(-1,nx,1);
fp([1; f+1])=[df-1; nx-f(end)-1];
fq=cumsum(fp); % = time to the next fall
k=find((rs<fs) & (fq<rq) & (floor((fq-rs)/2)==0)); % the final term centres peaks within a plateau
v=x(k);
else
k=[];
v=[];
end
  3 comentarios
Adam
Adam el 3 de Mayo de 2017
What is the relevance of that code anyway? It doesn't even include a call to findpeaks!
Rik
Rik el 3 de Mayo de 2017
Skimming this code, it looks like it aims to replicate findpeaks, although this version doesn't return the first index of a plateau, but the middle.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!