Skip to the content ForoHackers
Log inJoin

The Goertzel algorithm: detecting one tone without a full FFT

Amateur radio and SDRMicrocontrollersSignal processing
#1106 · qrp_at_midnight · · Report

If you only need to know "is there a 700 Hz tone in this audio, and how strong?" (a CW decoder, a DTMF detector, a pilot tone), a full FFT is overkill. Goertzel computes a single DFT bin with a tiny loop, two state variables and one multiply per sample.

#include <math.h>
#include <stdint.h>

float goertzel_power(const int16_t *x, int n, float f, float fs) {
    float w = 2.0f * 3.14159265f * f / fs;
    float coeff = 2.0f * cosf(w);
    float s1 = 0.0f, s2 = 0.0f;
    for (int i = 0; i < n; i++) {
        float s0 = (float)x[i] + coeff * s1 - s2;
        s2 = s1;
        s1 = s0;
    }
    return s1 * s1 + s2 * s2 - coeff * s1 * s2;
}

Things I learned running it on a small microcontroller:

  • Block length n sets the resolution. The bin width is fs / n, so for 8000 Hz sampling and 200 samples you resolve about 40 Hz. Longer blocks are narrower but slower to react.
  • If the tone frequency is not an exact multiple of fs / n, energy leaks into neighbouring bins. Pick n so your tone lands near a bin centre, or window the block.
  • Compare the power with a threshold relative to the total block energy, not an absolute number, or your decoder will depend on the volume knob.

It runs happily on an 8-bit part at night with a cup of tea. The signal does the rest.

#1772 · eeprom_eraser · · Report

Fixed-point version is easy too if you do not have an FPU: Q15 coefficient and 32-bit accumulators, but watch for overflow on s1 and s2 with loud signals. Learned that one by looking at a decoder that went deaf every time somebody keyed up near it.

#1773 · dipole_and_donuts · · Report

For CW I do exactly this. Envelope of the Goertzel output over time, threshold, then measure dit and dah lengths. The only problem is QSB: the tone fades and your threshold must follow. Adaptive threshold or you are decoding noise.

Log in or join to reply.