peaks and troughs in MATLAB (but with corresponding definition of a peak and trough)

送分小仙女□ 提交于 2019-12-11 15:07:58

问题


I want to look for peaks and troughs in a given vector of data, with the following definition.

A "peak" is a local maximum at least x% higher than the preceding trough, and a "trough" is a local minimum at least x% lower than the preceding peak. x here is referred to as a cutoff. In the paper I am writing, the cutoff is referred to as the standard deviation of the data.

I thought of writing a function that enables me to find a peak and a trough. The function that I wrote was this. I will call this from the main data.

function [vectpeak,vecttrough]=peaktrough(cutoff,x,lastobs,t)

% This function gives you two outputs: a vector of ones and zeros that locate PEAKS and     
% a vector of ones and zeros that locate TROUGHS. 
% To be able to get a vector of peaks and troughs, we have to give 
% four inputs.
% CUTOFF: This is what Chang and Osler [1999] use to identify if a data
% point is a peak or a trough. A PEAK is defined as "a local maximum that is
% x percent higher than the preceding trough." (Chang and Osler, 1999) 
% and a TROUGH is defined as "a local minimum that is x percent lower 
% than the preceding peak." This is a scalar.
% VECTOR: This is the vector of data that will be used for the purposes of
% the identification.
% LASTOBS: This is the last observation of the data.
% t: This specifies the time.

% Pre-allocations.
vectpeak=zeros(lastobs,1); % This is the vector of peaks.
vecttrough=zeros(lastobs,1); % This is the vector of troughs.

% Computing for the troughid's and peakid's.
temptroughid=troughid(cutoff,x,lastobs,t);
temppeakid=peakid(cutoff,x,lastobs,t);

 % Determining whether a function is a peak or a trough.
 while t<lastobs
    t=t+1;
    if x(t)>=temptroughid(t);
        vecttrough(t)=1;
        vectpeak(t)=0;
        maximum=x(t);
    elseif x(t)<=temppeakid(t);
            vecttrough(t)=0;
            vectpeak(t)=1;
            minimum=x(t);
    else
            vecttrough(t)=0;
            vectpeak(t)=0;
    end
end

function findtrough=troughid(cutoff,y,lastobs,t)
% This function computes for the TROUGHID which will be used in
% determining whether we have a trough or a peak.

% Initializations. 
findtrough=zeros(lastobs,1);
tempmin=zeros(lastobs,1);
minimum=y(1);

% This is how the function works.
while t<lastobs;
    t=t+1;
    if y(t)<minimum;
       tempmin(t)=y(t);
       minimum=y(t);
    else tempmin(t)=minimum;
    end
    findtrough(t)=tempmin(t)*(1+cutoff);
end
end

function findpeak=peakid(cutoff,y,lastobs,t)
% This function computes for the PEAKID which will be used in
% determining whether we have a peak.

% Initializations.
findpeak=zeros(lastobs,1);
tempmax=zeros(lastobs,1);
maximum=y(1);

% This is how the function works.
while t<lastobs;
     t=t+1;
     if y(t)>maximum;
       tempmax(t)=y(t);
       maximum=y(t);
    else tempmax(t)=maximum;
    end
    findpeak(t)=tempmax(t)*(1-cutoff);
end
end
end

The problem that I am getting is that I am getting weird results. For instance, I get a vector where all are peaks, and none are troughs, which does not make sense, since if I use the findpeaks command of MATLAB, I am able to identify peaks and troughs, and they are not successive.

Is there a way where I could tweak my code, or if not, to use findpeaks or its algorithm to find the peaks and troughs based on my definition?


回答1:


As it is, your code does not actually identify peaks or troughs. I think you should probably start with findpeaks to get a list of candidate peaks and troughs. Then, step through this list to test if each one satisfies the condition.



来源:https://stackoverflow.com/questions/12425527/peaks-and-troughs-in-matlab-but-with-corresponding-definition-of-a-peak-and-tro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!