# Zero-crossing detection using MATLAB

1. ## zero crossing matlab

I found the following code to count the number of zero crossings in a signal. However, I don't understand how it works. Could someone pls explain the following code? Thanks.

Code:
```% ZC number of zero crossings in x
% [n] = zc(x) calculates the number of zero crossings in x

function [n] = zc(x)
s=sign(x);
t=filter([1 1],1,s);
n=(length(s)-length(find(t)))/length(s);```

•

2. ## matlab zero crossing

it looks like does not working to me
does it?

3. ## matlab zero crossing detection

Originally Posted by aspedisca
it looks like does not working to me
does it?
Agree... It works when I replace the commented line with the line as follows:
Code:
```% n=(length(s)-length(find(t)))/length(s);

n = length(find(t==0));```
By the way, do you know the purpose of the following code:
Code:
`t=filter([1 1],1,s);`

•

4. ## Re: Zero-crossing detection using MATLAB

here better code in case your x signal is not known ( not sine...) :

%x is nx1 vector

zeroNb=0;

for i=1:length(x)-1 %length : get x size

if ((x(i)>0 && x(i+1)<0) || (x(i)<0 && x(i+1)>0))

zeroNb=i+zeroNb;

end

end

5. ## Re: Zero-crossing detection using MATLAB

This won't quite work: It does not find values where the point is exactly == 0 and we want to add 1 to the total crossings not i. Try this:

zeroNb=0;
for i=1:length(x)-1 %length : get x size
if ((x(i)>=0 && x(i+1)<0) || (x(i)<=0 && x(i+1)>0))
zeroNb=1+zeroNb;
end
end

6. ## Re: Zero-crossing detection using MATLAB

the first code is small & effective, it uses an FIR filter to sum the sign of each sample by previous one which is equivalent by "when sign change" and count this changes
of course the last line of code shode be corrected as:
n=length(find(t==0)) + length(find(x==0));

•

7. ## Re: Zero-crossing detection using MATLAB

hi please help me for detect zero crossing in matlab ?i want khnow calculate zero croosing for ECG in matlab have function ?
for example : step()[COLOR="Silver"]

I want my account to a signal following properties:
a. Time characteristic (energy, average magnitude, zero crossing, autocorrelation)
a. Frequency characteristic (like Fourier analysis)
b. Computing signal to noise (white or colored) ratio (SNR)
c. Study on Effect of sampling rate
d. Signal visualizing (Clean and noisy signals like cross talk noise and echo)
e. Signal filtering and denoising (group delay and phase delay)
Signal segmentation (split into meaningful segments

8. ## Re: Zero-crossing detection using MATLAB

plz send ur funtion completely

--[[ ]]--