Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 38 ยท Projects and Practice

Project: A DTMF Tone Detector with Goertzel

Build a telephone keypad decoder in Python. Generate DTMF tones, implement the Goertzel algorithm from scratch, add noise, and decode a whole phone number robustly.

Every time you press a key on a phone keypad, the phone plays two tones at once. This is DTMF (dual-tone multi-frequency) signalling. A decoder must work out which two of eight frequencies are present, quickly and cheaply, even with noise. A full FFT is overkill when you only care about 8 frequencies, so decoders use the Goertzel algorithm, a recursive filter that computes a single DFT bin. In this project you build a complete decoder.

The keypad

Each key is the sum of one row tone and one column tone:

1209 Hz 1336 Hz 1477 Hz 1633 Hz
697 Hz 1 2 3 A
770 Hz 4 5 6 B
852 Hz 7 8 9 C
941 Hz * 0 # D

Decoder plan

  1. Generate
Two sines per key
  1. Measure
Goertzel power at 8 tones
  1. Decide
Strongest row and column
  1. Validate
Reject noise and speech

Eight cheap measurements instead of a full FFT.

1 / 2

Step 1: generating tones

Python DTMF generator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 2: the Goertzel algorithm

The DFT value at frequency f needs N multiplications with an FFT-less approach. Goertzel computes the same bin power with one multiplication per sample using a second-order recursion:

s[n] = x[n] + 2 cos(w) s[n-1] - s[n-2], with w = 2 pi f / fs

After the N samples, the power is s[N-1]^2 + s[N-2]^2 - 2 cos(w) s[N-1] s[N-2]. It is simply an IIR resonator evaluated once at the end. For any f (not just bin centers) it gives the power of the matching DFT-like bin.

Python Goertzel versus the DFT

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two columns agree. With N = 205 at 8 kHz the bin spacing is 39 Hz, a value chosen so that every DTMF frequency lies close to a bin center and the neighboring tones fall in different bins (the closest pair, 697 and 770 Hz, are 73 Hz apart).

Step 3: decode a block

Compute the Goertzel power at the four row and four column frequencies, pick the strongest of each group, and look up the key.

Python Decoding single keys

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All sixteen keys decode correctly even with noise as strong as the tones. The Goertzel measurement integrates over 205 samples, which averages the noise away.

Step 4: a robust decoder

Real lines have speech, music and dial tone that can contain energy at DTMF frequencies, so a production decoder adds checks:

  1. Absolute threshold: the strongest row and column must exceed a minimum power.
  2. Twist: the row and column powers must be within a few dB of each other.
  3. Dominance: the best tone in each group must be well above the second-best (for example by 8 dB) and above the total energy fraction.
  4. Duration: a key counts only when seen for two or three consecutive blocks, and a gap (silence) must follow before the next key.
Python Decoding a phone number from a stream

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The detector ignores the whistle (it has no matching pair and fails dominance), finds all eight keys and ignores the silences. The consecutive-block rule (run == 2) records each key once per press.

Extensions to try

  • Measure the false-alarm rate against recorded speech instead of synthetic noise.
  • Reduce N to find the shortest block that still works at a given noise level.
  • Implement a sliding Goertzel so the decision updates every sample.
  • Add the second-harmonic check: DTMF tones from real telephones have little energy at twice the frequency; a talking voice has plenty.
  • Compare with an FFT approach on cost: eight Goertzel filters need 8N multiplications, an N point FFT needs N log N; for 8 bins Goertzel wins up to about log2 N bins.

Related ideas appear in spectral analysis and IIR resonators: the Goertzel recursion is a resonator with its poles on the unit circle.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the Goertzel algorithm compute?

    It is a second-order recursion evaluated at the end of the block, giving the power of a chosen DFT-like bin.
  2. 2. How many tones does a DTMF key press contain?

    A key is the sum of one of four low (row) frequencies and one of four high (column) frequencies.
  3. 3. Why is a robust decoder not satisfied with simply taking the strongest bins?

    The extra checks prevent false detections from voices, music and single tones.
  4. 4. When is Goertzel cheaper than an FFT?

    Each Goertzel filter costs about N multiplications; an FFT computes all N bins at roughly N log N cost, so a handful of bins favors Goertzel.

Summary

  • DTMF keys combine one row and one column tone; eight frequencies identify 16 keys.
  • Goertzel computes the power at one frequency with a cheap second-order recursion.
  • A decoder picks the strongest row and column, then validates with thresholds, dominance, twist and duration checks.
  • The block length trades speed against frequency resolution and noise averaging.

Next: Project: Spectral Subtraction Noise Reduction