is there a way to put the threshold line and count how many times the signal crossed the line?

7 次查看(过去 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?

回答(2 个)

Mathieu NOE
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 个评论

请先登录,再进行评论。


Image Analyst
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.

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by