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
nsets the resolution. The bin width isfs / 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. Picknso 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.