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

16 visualizzazioni (ultimi 30 giorni)
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?

Risposte (2)

Mathieu NOE
Mathieu NOE il 14 Dic 2022
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 Commenti
Mathieu NOE
Mathieu NOE il 14 Dic 2022
yes , sure
the code provided does not have to know the origin of the data
try it and let me know

Accedi per commentare.


Image Analyst
Image Analyst il 31 Gen 2023
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.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by