- 21st August 2007, 05:45 #1

- Join Date
- Nov 2005
- Posts
- 440
- Helped
- 4 / 4
- Points
- 3,853
- Level
- 14

## 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);

- 21st August 2007, 05:45

- 21st August 2007, 10:27 #2

- Join Date
- May 2007
- Posts
- 8
- Helped
- 1 / 1
- Points
- 1,093
- Level
- 7

## matlab zero crossing

it looks like does not working to me

does it?

- 21st August 2007, 10:27

- 21st August 2007, 16:49 #3

- Join Date
- Nov 2005
- Posts
- 440
- Helped
- 4 / 4
- Points
- 3,853
- Level
- 14

## matlab zero crossing detection

Originally Posted by**aspedisca**

Code:% n=(length(s)-length(find(t)))/length(s); n = length(find(t==0));

Code:t=filter([1 1],1,s);

- 21st August 2007, 16:49

- 17th January 2010, 23:29 #4

- Join Date
- Apr 2008
- Posts
- 1
- Helped
- 0 / 0
- Points
- 840
- Level
- 6

## 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

- 14th March 2011, 21:48 #5

- Join Date
- Mar 2011
- Location
- Bozeman, MT USA
- Posts
- 1
- Helped
- 0 / 0
- Points
- 318
- Level
- 3

## 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

- 7th September 2011, 14:26 #6

- Join Date
- Sep 2011
- Posts
- 1
- Helped
- 0 / 0
- Points
- 231
- Level
- 2

## 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));

- 29th December 2011, 08:56 #7

- Join Date
- Sep 2006
- Location
- IRAN
- Posts
- 23
- Helped
- 0 / 0
- Points
- 1,510
- Level
- 8

## 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 segmentsLast edited by hd_uni_pro; 29th December 2011 at 09:12.

- 6th February 2012, 14:13 #8

- Join Date
- Feb 2012
- Posts
- 6
- Helped
- 0 / 0
- Points
- 176
- Level
- 2

## Re: Zero-crossing detection using MATLAB

plz send ur funtion completely