I am implementing BFSK frequency hopping communication system on a DSP processor. It was suggested by some of the forum members to use Goertzel algorithm for the demodulation of frequency hopping at specific frequncies. I have tried implementing the goertzel alorithm in C. the code is follows :

float goertzel(int numSamples,int TARGET_FREQUENCY,int SAMPLING_RATE, float* data)
int k,i;
float   floatnumSamples;
float   omega,sine,cosine,coeff,q0,q1,q2,result,real,imag;

floatnumSamples = (float) numSamples;
k = (int) (0.5 + ((floatnumSamples * TARGET_FREQUENCY) / SAMPLING_RATE));
omega = (2.0 * M_PI * k) / floatnumSamples;
sine = sin(omega);
cosine = cos(omega);
coeff = 2.0 * cosine;

for(i=0; i<numSamples; i++)
    q0 = coeff * q1 - q2 + data[i];
    q2 = q1;
    q1 = q0;
real = (q1 - q2 * cosine);
imag = (q2 * sine);
result = sqrtf(real*real + imag*imag);
return result;

When I use the function to calculate the result at specific frequencies for a given dataset, I am not getting the correct results. However, if I use the same dataset and calculate the goertzel result using MATLAB, then I get the results perfectly. I am implemented the algorithm using C, with the help of some online tutorials that I found over the internet. I just want to get the view of you guys if the function is implmenting the goertzel algorithm correctly.

Thanks, Anshu

  • 1
    $\begingroup$ Why don't you try it and see if it is working? $\endgroup$
    – Jason R
    Jul 20, 2012 at 12:44
  • $\begingroup$ Jason, I have already tried it and its now working (as I mentioned in my post). not sure why because from what I read about goertzel algorithm, I think I have done it correctly. $\endgroup$
    – anshu
    Jul 20, 2012 at 12:50
  • $\begingroup$ It would be very helpful if you could provide a example of short input, expected output and actual output by the code. $\endgroup$
    – Phonon
    Jul 20, 2012 at 12:59
  • $\begingroup$ Also, a reference to paper, article or web resource that you're using to look up the algorithm would be nice. I can't find anything that implements it the way you do. $\endgroup$
    – Phonon
    Jul 20, 2012 at 13:10
  • $\begingroup$ I vote to close this question because it is about programming, not about signal processing. $\endgroup$ Jul 23, 2012 at 12:01

2 Answers 2


It's been a while, but if anyone would like a working fixed-point implementation (no floats/doubles) in C you might take a look at: http://www.ti.com/ww/cn/uprogram/share/ppt/c6000/Chapter17.ppt‎ – Slide 14.

It looks like it will work beautifully and efficiently in even a low-powered micro-controller. (In which case some of the ‘int’ variables in that TI C code may need to become ‘longs’ to be 32-bit.)

  • $\begingroup$ The slideshow link seems to be stale. $\endgroup$ Sep 28, 2021 at 19:21

I'm guessing anshu is long gone, but it looks to me as if what's incorrect in the code given here is the last step; instead of (q1 - c q2, s q2) it should compute (c q1 - q2, s q1). The rest looks OK.

(It is easy to make this kind of mistake in this kind of code -- you want to give those variables names with 1 and 2 to mean "1 back" and "2 back", but then your brain is liable to think that 1 is "earlier" than 2...)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.