siva
siva

Reputation: 2145

Python: Zero Crossing method for Frequency Estimation

I'm trying to understand the zero-crossing method for frequency estimation. After searching, found this code:

est_freq = round(framerate / np.mean(np.diff(zero_crossings)) / 2)

Dissecting further to learn, I wrote the code below:

import numpy as np

framerate = 1e3

a = [1, 2, 1, 1, -3, -4, 7, 8, 9, 10, -2, 1, -3, 5, 6, 7, -10]

signs = np.sign(a)
diff = np.diff(signs)
indices_of_zero_crossing = np.where(diff)[0]

print(a)
print(signs)
print(diff)
print(indices_of_zero_crossing)

total_points = np.diff(indices_of_zero_crossing)
print(total_points)

average_of_total_points = np.mean(total_points)
print(average_of_total_points)

freq = framerate/average_of_total_points/2

My question is, what is happening at line freq = framerate/average_of_total_points/2. What is the purpose of finding the mean of the differences in zero crossings and dividing by 2?

Could anyone care to explain? Thank you.

Upvotes: 0

Views: 805

Answers (1)

cvanelteren
cvanelteren

Reputation: 1703

I am not sure where you got the sampling frequency from (framerate) but in digital signal processing there is this thing called the Nyquist frequency where you cannot sample reliable more than half the sampling frequency, which may explain your factor 2. Do note that in your code the division is different from the snippet.

It should be freq = framerate/(average_of_total_points/2)

Upvotes: 1

Related Questions