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

조회 수: 13 (최근 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일
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);
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));
  댓글 수: 3
Mathieu NOE
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
Mathieu NOE 2023년 1월 31일
hello again
If my submission fullfills your request, do you mind accepting it ?

댓글을 달려면 로그인하십시오.

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