【问题标题】:peaks and troughs in MATLAB (but with corresponding definition of a peak and trough)MATLAB 中的波峰和波谷(但有相应的波峰和波谷定义)
【发布时间】:2012-09-07 16:16:36
【问题描述】:

我想在给定的数据向量中寻找波峰和波谷,定义如下。

“峰”是比前一个波谷至少高 x% 的局部最大值,而“波谷”是比前一个波峰低至少 x% 的局部最小值。这里的 x 称为截止值。在我写的论文中,cutoff 被称为数据的标准差。

我想写一个函数来让我找到一个高峰和一个低谷。我写的函数就是这个。我将从主要数据中调用它。

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

我遇到的问题是我得到了奇怪的结果。例如,我得到一个向量,其中所有的都是波峰,没有波谷,这是没有意义的,因为如果我使用 MATLAB 的 findpeaks 命令,我能够识别波峰和波谷,它们不是连续的。

有没有一种方法可以调整我的代码,或者如果没有,可以使用 findpeaks 或其算法根据我的定义找到峰值和谷值?

【问题讨论】:

  • 我不完全知道findtrough(t)=tempmin(t)*(cutoff); 应该做什么,但我很确定它没有做你想做的事。您似乎想使用(1 + cutoff)作为因素。无论如何,在波谷和波峰中使用相同的因子对我来说是没有意义的。
  • @dustincarr 是的,你是对的,我只是看到这是一个错误。再说一次,即使有这些更正,我仍然没有得到我想要的。
  • 还是不太对。您实际上可能希望因子为 1/cutoff 而不是 1+cutoff。那是我建议 1+cutoff 的错误。即使如此,您的比较仍然有问题。我将在下面提出一个解决方案。

标签: algorithm matlab while-loop


【解决方案1】:

事实上,您的代码实际上并不能识别波峰或波谷。我认为您可能应该从 findpeaks 开始获取候选峰和谷的列表。然后,逐步浏览此列表以测试是否每个都满足条件。

【讨论】:

  • 是的,这就是我想到的第二件事。 :) 我不确定的是如何实施测试是峰值还是谷值。我的一个想法是计算候选峰和谷之间的百分比变化,但可能会有问题。
  • 另一个想法是创建 2 个向量;第一个包含峰值指标,第二个包含低谷指标。之后,我为检查比率的数据编写了一个循环,然后实现用于检测候选峰和谷的算法。这有意义吗?
  • 问题是,除非您知道波峰或波谷在哪里,否则您无法真正做出指标。
  • 谢谢。这次我尝试使用您的建议再次编写算法,但似乎又出现了错误。我会把它作为我之前帖子的附录发布。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
相关资源
最近更新 更多