Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 38 ยท Applications

Pitch Detection

Estimate the fundamental frequency of a sound with autocorrelation, the harmonic product spectrum and parabolic refinement. Learn the octave-error trap and test on synthetic voices.

Tuners, karaoke scorers, speech coders and music transcription all need the pitch of a sound, which is closely tied to its fundamental frequency f0. A voiced speech sound or an instrument note is not a single sine: it contains a fundamental and harmonics at 2 f0, 3 f0, .... Finding f0 is harder than finding the highest FFT peak, because sometimes the fundamental is weak or missing and a harmonic is louder. Two classic methods are autocorrelation (in the time domain) and the harmonic product spectrum (in the frequency domain).

A harmonic test signal

The next code builds notes with several harmonics of different strengths. It is used by the rest of the lesson, so it appears in every example.

Python Autocorrelation shows the period

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.

A periodic signal is similar to itself shifted by one period, so its autocorrelation has a peak at the lag T0 = fs / f0 (here 40 samples). Searching for the highest peak between plausible lags gives f0 = fs / lag.

Two views of pitch

Autocorrelation Find the repeat period
Harmonic product Find the common spacing
Octave errors Half or double the true f0

Combine methods and add sanity checks.

1 / 2

Refining and speeding up

The lag is an integer, so the frequency resolution at high pitches is coarse: at 800 Hz and fs = 8000, one sample changes the estimate by about 80 Hz. Interpolating the peak with a parabola (as in peak interpolation) gives sub-sample accuracy. The autocorrelation can also be computed quickly as the inverse FFT of the power spectrum (Wiener-Khinchin).

Python Autocorrelation pitch with interpolation

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.

Errors stay within a few hertz even at 784 Hz. The window must hold at least two periods of the lowest pitch you want to detect, here two periods of 60 Hz or 33 ms.

Octave errors and the missing fundamental

Autocorrelation has peaks at multiples of the period too (2 T0, 3 T0), and noise can make one of them taller than the first one, giving an estimate an octave below the truth. The opposite error, an octave too high, happens when the signal's second harmonic is stronger than the fundamental. A sound with no energy at f0 (the "missing fundamental" that telephones and small speakers produce) still sounds like f0 because the hearing system uses the harmonic spacing; the autocorrelation and the next method recover it.

Python Harmonic product spectrum with a missing fundamental

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 loudest peak is at 300 Hz (the second harmonic), an octave error, while the product spectrum and the autocorrelation both report about 151 Hz (the true value is 150 Hz). The product spectrum rewards frequencies at which many multiples line up.

A frame-by-frame tracker

Real sounds change pitch. Cut the signal into frames, estimate f0 in each, and apply sanity rules: reject frames with low energy or a low autocorrelation peak (unvoiced), and smooth the track with a median filter to remove isolated octave jumps.

Python Tracking a gliding pitch

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 estimates follow the glide within a few hertz, limited by integer lags at this sampling rate.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. At which lag does the autocorrelation of a periodic signal have its first strong peak (besides lag 0)?

    The signal matches itself when shifted by one period, so the autocorrelation peaks at T0 = fs / f0 samples.
  2. 2. What is an octave error?

    Peaks at multiples of the period or a strong second harmonic can make the algorithm pick f0/2 or 2 f0.
  3. 3. What does the harmonic product spectrum multiply?

    At the true f0 all downsampled copies have a peak, so their product is large only there.
  4. 4. Why interpolate the autocorrelation peak?

    A parabola through the peak and its neighbors estimates a fractional lag, improving the frequency estimate.

Summary

  • Pitch is the fundamental of a harmonic sound; the highest spectral peak may be a harmonic instead.
  • Autocorrelation finds the period, the harmonic product spectrum the common spacing; both handle a missing fundamental.
  • Refine peaks by interpolation, guard against octave errors, and median-filter the track.
  • Use frame-by-frame analysis with a voicing decision for real sounds.

Next: Project: A Tone Detector with the Goertzel Algorithm