次の定義を使用して、データの特定のベクトルで山と谷を探したいと思います。
「ピーク」は、前の谷より少なくとも x% 高い極大値であり、「谷」は、前のピークより少なくとも x% 低い極小値です。x はカットオフと呼ばれます。私が書いている論文では、カットオフはデータの標準偏差と呼ばれています。
山と谷を見つけることができる関数を書くことを考えました。私が書いた関数はこれでした。これをメインデータから呼び出します。
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
私が得ている問題は、奇妙な結果が得られていることです。たとえば、findpeaks
MATLAB のコマンドを使用すると、ピークと谷を識別することができ、それらが連続していないため、すべてがピークで、いずれも谷ではないベクトルが得られますが、これは意味がありません。
コードを微調整する方法はありますか、そうでない場合は、findpeaks またはそのアルゴリズムを使用して定義に基づいて山と谷を見つける方法はありますか?