Code covered by the BSD License  

Highlights from
detect the Peak or valley in a 1 D signal

image thumbnail

detect the Peak or valley in a 1 D signal

by

 

14 Apr 2013 (Updated )

%To detect the Peak or valley in a 1 D signal´╝Ťwith some parameter for select

ind=Peak_Valley(y,T,d,p_v)
function ind=Peak_Valley(y,T,d,p_v)
%To detect the Peak or valley in a 1 D signal
% input: y    signal ;n*1 
%        T    threshold of D-value ,which is difference between  the peak value 
%             or valley value and  bilateral value
%        d    the distance of bilateral value
%        p_v  parameter for peak or valley ;p_v=0 all;pv=1,peak only;others
%             valley only
% example [x1 x2 x3 x4 x5] d=2; T=0.5; When (x1+x5)-2x3>T ,the x3 will be
% decided as a valley
% output: ind the index of peak or valley 
ind=[];
if length(y)<d*2+1
    error('The input signal is too short to calculate');
end
mask=[1 -2 1]';

for k=d+1:length(y)-d
    tmp=y([k-d k k+d])';
    [~,ind2]=max(y(k-d:k+d));
    [ ~,ind3]=min(y(k-d:k+d));
    ind2=ind2(1);
    ind3=ind3(1);
    
    if p_v==0
       pd= (ind2==d+1)+(ind3==d+1);
    elseif p_v==1
         pd= (ind2==d+1);
    else
       pd= (ind3(1)==d+1);  
    end
    
    if abs(tmp*mask)>T&&pd
        ind=[ind k];
    end
end
figure
plot(y)
hold on
plot(ind,y(ind),'o')
hold off

Contact us