沿波形图的全局最大值与下一个连续最大值/最小值之间沿x轴的距离

时间:2017-07-10 11:03:27

标签: matlab

Image of Wave Pattern

我有一组波形图案(一个示例波形图案包含在x = 420附近可见的最高峰值),具有各种交替的波峰和波谷,并且能够使用{{1确定波形图案的全局最大值}}。我现在需要确定沿着这个全局最大值到下一个连续局部最小值的距离以及到下一个连续局部最大值的距离。

我使用findpeaks使用以下代码查找正峰和负峰:

max

因为否定版本应该返回最小值。 pospks = findpeaks(h_dat(end,xi(i):xf(i))); negpks = findpeaks(-((h_dat(end,xi(i):xf(i))))) 只是根据我的数据返回波形图案的代码(对应于具有不同参数的多个波形图案在'结束时间'当达到稳定状态且具有相应的稳定全局最大值时)。

我试图使用sort函数对它们进行排序,但我不确定这是否有帮助,因为它会将它们按升序/降序排列,您可以从我的图像中看到局部最大值和最小值的高度往往是任意所以按升序/降序排序它们并不能保证找到从最高峰(称为h_dat(end,xi(i):xf(i)))到下一个最大值或最小值的距离。我的想法是使用距离函数来查找hmaxhmax的下一个最接近成员的距离,然后到pospks的最近成员,以便找到相关距离但是研究过它并且仍然不确定如何做到这一点(如果所有这些都非常简单,我很抱歉,因为我对Matlab很新)。

1 个答案:

答案 0 :(得分:0)

Simply search for the first minimum that is after your global maximum, or the end of the data set if the maximum is last.

%% create the data set
x = rand(150,1);
idx = 55:85;
xx = x(idx);
xx = xx*max(x(:))/max(xx(:))*1.2; %% create a peak that will be in the middle of the data
x(idx) = xx;
x = smooth(x);
x = smooth(x);

%% find the next minimum
[pospks,locks] = findpeaks(x); %find all the maximums
[~,locksN] = findpeaks(-x);    %find only the location of all the minimums
[~,maxIdx] = max(pospks);      %find the location of the (first)global maximum
maxIdx = locks(maxIdx);
minIdxs = locksN(locksN > maxIdx); %find the indexes of all the minimums that are after the global maximum
if (isempty(minIdxs))
    minIdx = length(x); % take the last element of the dataset
else
    minIdx = minIdxs(1); % take the first min after the global max
end
plot(1:length(x),x,'green');
hold on;
plot(maxIdx,x(maxIdx),'bo');
plot(minIdx,x(minIdx),'r*');
hold off;

The result is: enter image description here