3

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?

jurij
  • 383
  • 3
  • 7
  • 21
  • 1
    http://dsp.stackexchange.com or http://electronics.stackexchange.com/ – user2485710 Nov 08 '14 at 18:13
  • Fast Fourier transform. – Hot Licks Nov 08 '14 at 18:38
  • 2
    @HotLicks Suggesting FFT for such a simple problem is not a good advice. It is like suggesting brute-force algorithms for other simple problems. – Nils Pipenbrinck Nov 08 '14 at 19:54
  • As noted by user2485710 below, [this question has been re-posted on dsp.SE](http://dsp.stackexchange.com/questions/19057/simple-and-efficient-algorithm-to-detect-frequency-and-phase-of-a-sine-signal). – Ilmari Karonen Nov 08 '14 at 20:17
  • @NilsPipenbrinck - Depending on the platform, FFT is fairly simple. The code is built in to iPhone, eg, and reasonably non-arcane. Other platforms it's hard to say. – Hot Licks Nov 08 '14 at 21:09

1 Answers1

3

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.

MBo
  • 77,366
  • 5
  • 53
  • 86
  • 1
    in the meantime this question got a cross post / duplicate http://dsp.stackexchange.com/questions/19057/simple-and-efficient-algorithm-to-detect-frequency-and-phase-of-a-sine-signal – user2485710 Nov 08 '14 at 18:30
  • @user2485710 When I started to write my answer, there were neither your redirection comment nor cross-duplicate question :-) – MBo Nov 08 '14 at 18:34
  • @MBo Thanks! Do you know any specific quick and simple implementation for my case? – jurij Nov 08 '14 at 18:38
  • 1
    @jurij - If you want a canned implementation you probably at least need to specify what programming language and/or hardware platform. – Hot Licks Nov 08 '14 at 18:39
  • Preferred language is C, but I can rewrite is from another language. Code will run on Texas Instruments F28069. – jurij Nov 08 '14 at 19:01