Goertzel Algorithm Simulator — DTMF Single Frequency Detection
Quick answer The Goertzel algorithm evaluates only the target bin k = round(f·N/F_s) with the second-order IIR recurrence s_n = x_n + c·s_(n−1) − s_(n−2), where c = 2·cos(2πk/N), returning |X_k|² = s_(N−1)² + s_(N−2)² − c·s_(N−1)·s_(N−2). At O(N) per bin it beats an FFT when few bins are needed.
Detect the 16 DTMF keys with the Goertzel method. See in real time how a second-order IIR filter computes one DFT bin in O(N), and how only the row and column tone frequencies stand out when the target frequency is swept.
While paused, moving a slider updates the result immediately.
Results
—
Target frequency
—
Detection strength |X(k)|
—
Decision
—
Operations saved vs FFT
Input signal and Goertzel detection strength
Top: the input signal x[n] processed sample by sample (yellow = scan line). Bottom: detection strength |X(k)| as the target frequency is swept (spikes at the tone frequencies).
Theory & Key Formulas
The Goertzel algorithm computes the squared magnitude of the bin k = round(f·N/F_s) nearest to the target frequency f directly with a second-order IIR recurrence.
Angular frequency ω and coefficient c:
$$\omega = \frac{2\pi k}{N},\qquad c = 2\cos\omega$$
One bin costs O(N), M frequencies O(N·M). Compared with the O(N log N) of a full FFT, Goertzel wins the fewer bins you need.
About the Goertzel Algorithm Simulator
🙋
We already have the FFT – why do we need a separate algorithm called Goertzel at all?
🎓
Roughly speaking, Goertzel is the specialist for when you only want to know a single frequency bin. An FFT computes all N bins, but DTMF detection, for example, only needs eight frequencies. Goertzel costs O(N) per bin and is just a second-order IIR filter with three variables, which makes it a good fit for embedded microcontrollers. Change the key in the simulator and you will see that only two frequencies – one row and one column – need to be detected.
🙋
What is a "second-order IIR"? When I hear the word filter I get a little nervous…
🎓
It is not hard. You just advance the recurrence $s_n = x_n + c\,s_{n-1} - s_{n-2}$ by one sample at a time. The coefficient $c = 2\cos(2\pi k/N)$ is computed once at the start. There are only two state variables, $s_{n-1}$ and $s_{n-2}$, so three words of memory are enough. At the end $|X_k|^2 = s_{N-1}^2 + s_{N-2}^2 - c\,s_{N-1}s_{N-2}$ gives the squared magnitude. It maps perfectly onto the multiply-accumulate instruction of a DSP.
🙋
When I raise the "noise level" in the simulator, the skirt of the detection-strength curve (the noise floor) rises. How does real hardware decide?
🎓
Good question. Real devices AND several conditions together: the level of the strongest bin must exceed a threshold, the row-to-column level ratio must lie within the tolerance of the standard, the ratio to the second-strongest component (twist) must be within a certain range, and the tone must last at least 40 ms. Even if noise briefly swaps the strongest bin, the duration condition suppresses false detections. All of this is written down in the ITU-T Q.24 standard.
🙋
What changes when I increase the "frame length N"?
🎓
The larger N, the finer the frequency resolution $\Delta f = F_s/N$ and the better the separation from neighbouring frequencies – but the computation grows in proportion. At F_s=8 kHz and N=205, $\Delta f \approx 39$ Hz, which is the standard choice for DTMF. If the frame is too short, the bins drift away from the DTMF frequencies and the detection strength drops. Move N from 128 to 1024 in the simulator and you will see the width of the peaks (the selectivity) change, and the peak height change with how far the nearest bin sits from 770 Hz.
Frequently Asked Questions
It is widely used wherever only specific frequencies need to be detected: frequency monitoring in power systems (harmonic analysis at 50/60 Hz), carrier detection in industrial sensors, echo detection in ultrasonic ranging, CTCSS/DCS tone-squelch decisions in radios, pulse detection in medical devices, and so on. The common thread is the need for a few bins quickly rather than all N bins.
The basic form assumes an integer k, but an extension called the Generalized Goertzel algorithm handles any real k. It lets you estimate the magnitude precisely while suppressing spectral leakage even when the target frequency does not lie on the DFT grid. This matters in applications such as power-system frequency estimation, where the frequency does not fall on an integer bin.
The internal state of a second-order IIR filter tends to grow, and for a large frame length N the dynamic range of s_n increases sharply. Either scale the input beforehand (for example by 1/N) to prevent overflow, or use saturating arithmetic with guard bits at every sample. In Q1.15 or Q1.31 fixed point, rounding the coefficient c to Q14 to limit the error is a common implementation choice.
Yes. If the complex DFT value X_k = s_{N-1} − e^{-jω} s_{N-2} is computed in the final stage, the real and imaginary parts are available and atan2 gives the phase angle. This simulator only detects magnitude, but phase matters in AM/FM demodulation and synchronous detection, so implementations there use the complex-output version. Dropping the phase saves one multiplication – one of Goertzel's simple advantages.
Real-World Applications
DTMF decoding in telephony: The Goertzel method is the standard means of detecting in real time which key a user has pressed, in landline phones, IP phones and interactive voice response (IVR) systems. On analogue lines the DTMF tones are superimposed on the voice from the handset; the receiver runs eight Goertzel filters in parallel and extracts valid keys through level-ratio, duration and twist tests. VoIP also carries DTMF digitally, as in RFC 2833, but Goertzel decoders remain in service for interoperability.
Frequency analysis in power systems: Power-quality monitors continuously track the magnitude of 50 Hz/60 Hz and their harmonics (150/180, 250/300, 350/420 Hz and so on). Instead of running an FFT over all frequencies, only the few harmonics of interest are computed continuously with Goertzel, keeping the CPU load low while giving responsive alarm decisions. Combined with the Generalized Goertzel algorithm, the magnitude is estimated accurately even when the grid frequency drifts slightly from 50 Hz.
CTCSS/DCS tone detection in radios: Commercial and amateur radios use the CTCSS scheme, in which one of about 50 sub-audible low-frequency tones (67–250 Hz) identifies a group. The receiver continuously monitors the Goertzel output of the target tone and unmutes the loudspeaker only when it exceeds a threshold. Detecting a few low-frequency bands is exactly where Goertzel is strongest.
Carrier detection in embedded devices: Preamble detection in FSK modems, echo-strength measurement in ultrasonic sensors, heartbeat synchronisation in pulse oximeters – wherever an ARM Cortex-M class MCU has to measure a few frequencies with FFT-like accuracy, Goertzel is widely used. The CMSIS-DSP library offers Goertzel implementations built on functions such as arm_biquad_cascade_df1_q15, processing a frame in a few microseconds with the hardware multiplier.
Common Misconceptions and Pitfalls
The most common misconception is believing that "Goertzel is always faster than an FFT". There is a break-even point: once the number M of frequencies to detect exceeds roughly log₂N, the FFT becomes the better choice. For N=1024, log₂N = 10, so if you need ten or more bins a single FFT is faster. The simulator's "Operations saved vs FFT" card shows the difference between the FFT's N·log₂N and the N of a single Goertzel bin (1,792 for N=256). Make a habit of comparing N·M, with M the number of bins you need, against N·log₂N. Goertzel wins only when the bins are few and known in advance.
The next most common is underestimating how important it is to place the target frequency exactly on the DFT grid. Standard Goertzel rounds to an integer k = round(f·N/F_s), so if the target frequency f is off the bin centre a "scalloping loss" occurs and the magnitude estimate can drop by up to 3.9 dB (ΔdB ≈ 20log₁₀(sinc(0.5))). The classic DTMF recommendation F_s=8000, N=205 is a clever choice that puts all eight DTMF frequencies close to the DFT grid. In the simulator too, at N=256 the bin nearest the 770 Hz row tone is 781.25 Hz with a detection strength of 0.85, whereas at N=1024 the bin is 773.4 Hz yet the strength is 0.71 (an offset of 3.4 Hz against a bin width of 7.8 Hz): the peak height changes with N. That is the scalloping effect.
Finally, beware of jumping to the conclusion that "a large Goertzel output means a successful detection". Picking the single strongest bin on the swept curve is easily fooled by noise or voice harmonics. Real devices declare a valid key only when several conditions hold together: the strongest row and column levels exceed an absolute threshold, their ratio lies within the tolerance of the standard, the ratio to the second-strongest component (twist) is within the specified range, the tone lasts at least 40 ms and the gap between tones is at least 40 ms. Even if you raise the noise in the simulator to 1.0 (twice the tone amplitude of 0.5), the noise averages out over the N=256 samples and the two peaks remain. Note that this tool's "Present" decision uses only a peak-ratio threshold of 0.5 and checks neither duration nor twist.
Set the sampling frequency between 4 kHz and 16 kHz; the telephone-network standard of 8 kHz is recommended
Set the frame length to 128–1024 samples (steps of 128) to fix the observation window and the frequency resolution Δf=Fs/N
Select the key to test from 0–15 (16 keys); the corresponding DTMF frequency components are generated
Adjust the noise level from 0 to 1 (linear amplitude) to evaluate robustness in a real environment
In the lower swept curve, check that peaks appear at the dashed row (red) and column (blue) tone frequencies and exceed the peak-ratio threshold of 0.5, giving "Present"
Worked Example
Detecting key "5" (row 770 Hz, column 1336 Hz) at Fs=8 kHz with N=256 samples: the frequency resolution is Δf=31.25 Hz, the bin nearest the row tone is k=round(770×256/8000)=25 (781.25 Hz) and the column bin is k=43 (1343.75 Hz). On the swept curve the column peak reads 1.00 and the row peak 0.85, both above the 0.5 threshold, so both are "Present". The cost is N=256 multiplications per bin, 2,048 for 8 bins – about the same as the N·log₂N=2,048 of a 256-point FFT – but only 512 for the two row/column bins, with just two state variables per bin. Adding noise 0.15 leaves the peaks almost unchanged at 0.87/1.00.
Practical Notes
Set the public-network DTMF frequencies (rows 697, 770, 852, 941 Hz / columns 1209, 1336, 1477, 1633 Hz) within the ±1.5 % tolerance to stay compatible with legacy exchanges
The shorter the frame (e.g. N=128 at Fs=8 kHz, 16 ms) the coarser the resolution Δf=Fs/N and the smaller the margin for separating the low row tones; N=205–256 is the classic recommendation for DTMF
In noisy environments, suppress false detections with consecutive-frame agreement (e.g. 3 frames in a row) and a tone duration of at least 40 ms; avoid single-frame decisions
In real-time processing the state update (two delay elements) is cache-friendly and the memory footprint can be kept to 8×2 floats (64 bytes)