Goertzel algorithm (C)

From LiteratePrograms

Jump to: navigation, search

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

float goertzel(float freq, int size, const float x[])
    int i;
    float coeff;
    float s, 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);
#include <math.h>
Download code
Personal tools