Simple and efficient algorithm to detect frequency and phase of a sine signal

2.2k views Asked by At

I need an algorithm to detect frequency and phase of a pure sine signal. The frequency of the input signal changes between 0 and 100 Hz.

The value of the signal gets captured with a frequency of 20kHz (so I get 20.000 values per second) - this is given and cannot be changed. I need to detect frequency and phase of this input signal and using PWM generate MCU interrupts with the same frequency as the input signal.

Can anyone suggest what algorithm to use to do this simple and efficient? Maybe Goertzel algorithm?

1

There are 1 answers

5
MBo On

Goertzel algorithm is good for detecting predetermined frequency (or few frequences).
To find an unknown frequency of sine wave, you can use Fourier transform.

Peak with the largest magnitude corresponds to sine frequency, and phase of this harmonics - to its phase.

Phase, derived from FT result, could be susceptible to noise. More robust approach - using cross-correlation with zero-phased sine wave (with the same frequency) to get a phase shift.

There is a lot of FFT implementations on C. Fast one is fftw.org (portability to any C compiler claimed), but I doubt that you really need so complex library for microcontroller. Take any 40-lines of code good Cooley-Tukey implementation like this one

P.S. If your signal is really perfect sine with single frequency without significant noise, then zero-crossing method proposed in parallel topic, will be better.