# Goertzel algorithm (C)

### From LiteratePrograms

The Goertzel algorithm is a digital signal processing (DSP) technique for identifying frequency components of a signal, published by Dr. Gerald Goertzel in 1958. While the general Fast Fourier transform (FFT) algorithm computes evenly across the bandwidth of the incoming signal, the Goertzel algorithm looks at specific, predetermined points. For more information see the Wikipedia article.

## Main algorithm

<<goertzel>>=floatgoertzel(floatfreq,intsize,constfloatx[]){inti;floatcoeff;floats, s_prev1 = 0.0f, s_prev2 = 0.0f; coeff = 2.0f * cosf(2.0f * M_PI * freq);for(i = 0; i < size; i++){s = x[i] + (coeff * s_prev1) - s_prev2; s_prev2 = s_prev1; s_prev1 = s;}return(s_prev1 * s_prev1) + (s_prev2 * s_prev2) - (s_prev1 * s_prev2 * coeff);}

<<goertzel.c>>=#include <math.h>goertzel

Download code |