Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Zero-crossing detection using MATLAB

Status
Not open for further replies.

powersys

Advanced Member level 1
Advanced Member level 1
Joined
Nov 29, 2005
Messages
439
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Activity points
4,981
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);
 

aspedisca

Newbie level 5
Newbie level 5
Joined
May 6, 2007
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,312
matlab zero crossing

it looks like does not working to me
does it?
 

powersys

Advanced Member level 1
Advanced Member level 1
Joined
Nov 29, 2005
Messages
439
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Activity points
4,981
matlab zero crossing detection

aspedisca said:
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);
 

scofield984

Newbie level 1
Newbie level 1
Joined
Apr 6, 2008
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,285
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
 

jerryglen

Newbie level 1
Newbie level 1
Joined
Mar 14, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Bozeman, MT USA
Activity points
1,285
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
 

Mahdi_parizi

Newbie level 1
Newbie level 1
Joined
Sep 7, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,285
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));
 

hd_uni_pro

Junior Member level 2
Junior Member level 2
Joined
Sep 11, 2006
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
IRAN
Activity points
1,391
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()

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
 
Last edited:

shekolii

Newbie level 4
Newbie level 4
Joined
Feb 5, 2012
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,306
plz send ur funtion completely
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top