is there a way to put the threshold line and count how many times the signal crossed the line?
8 ビュー (過去 30 日間)
古いコメントを表示
as shown in the picture for example i draw a line manually and the signal crossed the line two times. is there a way do this from matlab?
0 件のコメント
採用された回答
Mathieu NOE
2022 年 12 月 14 日
Sure
try this
% dummy data
n = 250;
x = 5*(0:n-1)/n;
y = cos(4*(x -0.5));
threshold = 0.2*max(y); % 20% of peak amplitude
t0_pos1 = find_zc(x,y,threshold);
figure(1)
plot(x,y,'b.-',t0_pos1,threshold*ones(size(t0_pos1)),'*r','linewidth',2,'markersize',12);grid on
legend('signal','signal positive slope crossing points');
function [Zx] = find_zc(x,y,threshold)
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
Zx = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end
3 件のコメント
Mathieu NOE
2022 年 12 月 14 日
yes , sure
the code provided does not have to know the origin of the data
try it and let me know
Mathieu NOE
2023 年 1 月 31 日
hello again
If my submission fullfills your request, do you mind accepting it ?
tx
その他の回答 (1 件)
Image Analyst
2023 年 1 月 31 日
You can count the number of times it's above the line using bwlabel, if you have the Image Processing Toolbox.
threshold = 0.3
[~, count] = bwlabel(y > threshold);
count is the number of times the y signal is above the threshold line. Not the total number of elements, which is just sum(y > threshold), but the number of regions. Pretty simple.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!