【问题标题】:Find the difference between positive and negative peaks MATLAB找出正峰值和负峰值之间的差异 MATLAB
【发布时间】:2017-02-22 09:16:29
【问题描述】:

我需要找出差异大于 +-3 的正峰和负峰之间的差异。

我在 MATLAB 中使用findpeaks 函数来查找数据的正负峰值。 在我的代码示例中:

[Ypos, Yposloc] =  findpeaks(YT0);
[Yneg, Ynegloc] =  findpeaks(YT0*-1);
Yneg = Yneg*-1; 

YposlocYnegloc 返回数据中正峰和负峰的位置。

我想根据峰的顺序连接 YposYneg

例如,我的峰是

Ypos = [11 6 -10 -10 6 6 6 6 6 -5]
Yneg = [-12 -14 -11 -11 -11 5 5 5 -6]

YT0中的位置

Yposloc = [24 63 79 84 93 95 97 100 156]
Ynegloc = [11 51 78 81 85 94 96 99 154]

在这种情况下,YposlocYnegloc 都是 9x1,我可以执行以下操作;

nColumns = size(Yposs,2);
YTT0 = [Yneg, Ypos]';
YTT0 = reshape(YTT0(:),nColumns,[])';
YTT0 = diff(YTT0)
YT0Change = numel(YTT0(YTT0(:)>=3 | YTT0(:)<=-3));

我感兴趣的全部更改是6

但是,我需要根据它们的位置自动连接 YnegYpos。所以我认为我需要做一个if 声明来确定我的正峰值或负峰值是先出现的?然后,我不确定如何解决YposYneg大小不同的问题。

我多次运行此脚本,其中数据发生变化并且负/正峰值顺序不断变化。有没有一种简单的方法可以比较峰值位置,还是我在正确的轨道上?

【问题讨论】:

  • 我不太确定您的目标是什么。你有最小值和最大值以及相应的位置。然后你计算这些收益或损失,并想计算最小值和最大值(或最大值和最小值)之间的差异在哪里更大 3. 到目前为止我是对的吗?这就是你想要的全部吗?您想如何比较位置?它们是在同一点还是彼此之间的距离相同或其他什么?
  • 嗨 Fin,我稍微编辑了我的帖子。是的,到目前为止你是对的。基本上,我正在查看向相机展示面部的动作。我对“明显”的运动感兴趣,即从峰值到峰值的变化很大。到目前为止,我想根据先出现的位置来比较位置,即如果正位置在前,则与负位置进行比较,然后与正位置进行比较,然后再与第四位置进行比较。

标签: matlab signal-processing


【解决方案1】:

我会用前一个和下一个最大值检查每个最小值。为了做到这一点,您可以首先根据它们的顺序组合正负峰值:

Y = zeros(1, max([Yposloc, Ynegloc]));
Yloc = zeros(size(Y));
Yloc(Yposloc) = Yposloc;
Yloc(Ynegloc) = Ynegloc;
Y(Yposloc) = Ypos; % I think you inserted one extra '6' in your code!
Y(Ynegloc) = Yneg;
Y = Y(Yloc ~= 0) % this is the combined signal
Yloc = Yloc(Yloc ~= 0) % this is the combined locations

% Y =
%
%   -12   11  -14    6  -11  -10  -11  -10  -11    6    5    6    5    6    5    6   -6   -5
%
% Yloc =
%
%    11   24   51   63   78   79   81   84   85   93   94   95   96   97   99  100  154  156

然后计算差异:

diff(Y)

% ans =
%
%    23  -25   20  -17    1   -1    1   -1   17   -1    1   -1    1   -1    1  -12    1

如果您想要更改至少 6 个单位:

num = sum(abs(diff(Y)) > 6)

% num =
%
%     6

【讨论】:

  • 您好,erfan,非常感谢您。现在解决这个问题 - Y = zeroes(1,max([yposloc, ynegloc])) 返回 'error using zeroes, size inputs must be scalar.不知道如何应对? (抱歉 - matlab 的新手!)
  • 嗨乔希。我认为您的 YposlocYnegloc 与您的示例的方向不同。 size(Yposloc) 返回什么?大概max([Yposloc; Ynegloc])(带分号)会解决问题。
  • 它返回 ans = 9 1.. 我想我把它写成 1x9 让你感到困惑,对不起!
  • 在发布的数据样本中,Ypos 有 10 个值,而 Yneg 和两个 loc 只有 9 个。这可能是个问题
  • 是的,我手动更正它以获得输出。这里的问题是max 返回多个值。只需将max([Yposloc, Ynegloc]) 中的逗号替换为分号
【解决方案2】:
Ypos = [11 6 -10 -10 6 6 6 6 -5];
Yneg = [-12 -14 -11 -11 -11 5 5 5 -6];
Yposloc = [24 63 79 84 93 95 97 100 156];
Ynegloc = [11 51 78 81 85 94 96 99 154];

TOTAL=[Yposloc Ynegloc;Ypos Yneg];
%creatin a vector with positions in row 1 and values in row 2
[~,position]=sort(TOTAL(1,:));

%resort this matrix so the values are in the orginial order
TOTAL_sorted=TOTAL(:,position);
%look at the changes of the values
changes=diff(TOTAL_sorted(2,:));

if changes(1)>0
    disp('First value was a Minimum')
else
    disp('First value was a MAximum')
end
%same structure at the TOTAL matrix
%abs(changes)>3 produces a logical vector that shows where the absolute values was bigger
%than 3, in my opinon its rather intresting where the end is then were the start is
% thats why i add +1
Important_changes=TOTAL_sorted(:,find(abs(changes)>3)+1);

plot(TOTAL_sorted(1,:),TOTAL_sorted(2,:))
hold on
plot(Important_changes(1,:),Important_changes(2,:),...
    'Marker','o','MarkerSize',10, 'LineStyle','none');
hold off

【讨论】:

    猜你喜欢
    • 2014-11-28
    • 2023-03-06
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 2014-09-23
    • 2013-01-24
    相关资源
    最近更新 更多