0

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.

siva
  • 2,105
  • 4
  • 20
  • 37

1 Answers1

1

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)

cvanelteren
  • 1,633
  • 9
  • 16
  • the sampling frequency is just arbitrary, set at 1kHz just for example. however, in my actual sampling, the framerate is 2kHz and the data is different. – siva Apr 22 '21 at 09:52