我需要找出正负峰值之间的差异,其中差异大于+-3。
我使用MATLAB中的findpeaks函数来找出数据的正负峰值。在我的代码示例中:
[Ypos, Yposloc] = findpeaks(YT0);
[Yneg, Ynegloc] = findpeaks(YT0*-1);
Yneg = Yneg*-1; Yposloc和Ynegloc返回数据中正负峰值的位置。
我想根据峰值的顺序来连接Ypos和Yneg。
例如,我的峰值是
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]在本例中,Yposloc和Ynegloc都是9x1,我可以执行以下操作;
nColumns = size(Yposs,2);
YTT0 = [Yneg, Ypos]';
YTT0 = reshape(YTT0(:),nColumns,[])';
YTT0 = diff(YTT0)
YT0Change = numel(YTT0(YTT0(:)>=3 | YTT0(:)<=-3));我感兴趣的全部更改是6
但是,我需要根据它们的位置自动连接Yneg和Ypos。所以我想我需要做一条if语句来计算出我的正负峰值是先出现的吗?然后,我不确定如何解决Ypos和Yneg大小不同的问题。
我多次运行此脚本,其中数据更改和负/正峰值顺序不断变化。有没有一种简单的方法可以比较山峰的位置,或者我在这里的轨道是正确的?
发布于 2016-10-13 18:28:56
我会用前一个最大值和下一个最大值来检查每个最小值。为了做到这一点,你可以首先根据它们的顺序组合正负峰值:
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发布于 2016-10-13 18:57:19
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 offhttps://stackoverflow.com/questions/40017761
复制相似问题