# How compute frequency of signal from zero crossing? [duplicate]

I need to compute frequency of a signal from zero crossing.It's possible to compute to frequency from zero crossing of a signal. Plz anybody help me about this with matlab .

## 2 Answers

From your signal crossing threshold it is fairly straightforward to calculate its period. When you know the period then calculating of corresponding frequency is very simple (its inverse). This answer: enter here answers to similar question.

I assume you know that this will only work if you need to estimate frequencies in signals next to pure sinusoids, so for this a simple code can be used.

I create a simple sin sampled at 8000hz and frequency at 350hz, lets try get the frenquency using zero crossing method:

Fs = 8000;
f  = 350; %Frequency test
signal= 0.9*sin(2*pi*f/Fs*(0:2048));

%cross = sign(signal(:)); % you can test results using sign function too.
cross(signal==0) = 1;
index= find(diff(cross));
ZC = length(index);
F0_ZC=(ZC * Fs / (2 * (length(signal))))


The result was:

F0_ZC =

349.4388


Edit

The quick try using @jojek sugestion:

Fs = 8000;
f  = 350; %Frequency test
signal= 0.9*sin(2*pi*f/Fs*(0:2048));
ZCR = sum(abs(diff(sign(signal(:)))))/(2*length(signal));
F0_ZC=(ZCR * length(signal) * Fs) / (2 * (length(signal)))


The result was:

F0_ZC =

350.4148


Course if you compute the Zero Crossing Rate(ZCR) you can find the frequency using (ZCR * Fs) /2

• The way you calculate ZCR is not robust at all. Definitely there are better ways of doing that. For example: sum(abs(diff(sign(x))/(2*L)
– jojek
Mar 15 '15 at 18:32
• Nice tip @jojek, I try it seems nice ! Mar 15 '15 at 21:40