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

조회 수: 23(최근 30일)
tamim 2022년 12월 14일
답변: Image Analyst 2023년 1월 31일
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 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표시숨기기 이전 댓글 수: 2
Mathieu NOE 2023년 1월 31일
hello again
If my submission fullfills your request, do you mind accepting it ?
tx

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

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.

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

### 범주

Find more on Descriptive Statistics in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!