Introduction

Strike a guitar string, sing a vowel, or blow across a bottle: what comes out is a periodic sound — a pressure wave that repeats the same shape over and over. That repeating chunk is the period TT, and its reciprocal f0=1/Tf_0 = 1/T is the fundamental frequency, the pitch you hear.

The question for an algorithm is: given a short burst of raw samples, what is f0f_0? This is pitch detection, and it sits at the heart of guitar tuners, voice assistants, autotune, and MIDI transcription.

The most intuitive approach is autocorrelation: slide a copy of the signal over itself, measure how well they line up at each delay τ\tau, and look for the first strong peak after zero. When the lag τ\tau equals the period TT, a periodic signal lines up with itself almost perfectly — that peak tells you the pitch.

The idea was formalized as the YIN algorithm by Alain de Cheveigné and Hideki Kawahara (2002), who added a clever normalization that suppresses the trivial peak at τ=0\tau = 0 and makes the method robust to noise. YIN is still a standard baseline in audio processing today.

Try It

Choose a waveform shape and drag the frequency slider. The top panel draws the wave; the bottom panel plots the autocorrelation function — the self-similarity of the signal at every lag. The lag of the first strong peak after zero is the estimated period, from which the algorithm reads the musical note.

<!-- {{c_html_desc}} -->
<div class="controls">
  <label>
    <span>{{lbl_wave}}</span>
    <select id="waveform">
      <option value="sine">{{opt_sine}}</option>
      <option value="sawtooth">{{opt_saw}}</option>
      <option value="square">{{opt_square}}</option>
    </select>
  </label>
  <label>
    <span>{{lbl_freq}}: <b id="freq-val">220</b> Hz</span>
    <input id="freq-slider" type="range" min="80" max="880" value="220" step="1">
  </label>
</div>
<canvas id="wave-canvas" width="560" height="120" title="{{canvas_wave_title}}"></canvas>
<div class="section-label">{{lbl_autocorr}}</div>
<canvas id="acorr-canvas" width="560" height="140" title="{{canvas_acorr_title}}"></canvas>
<div id="result" class="result" aria-live="polite"></div>
/* {{c_css_desc}} */
* { box-sizing: border-box; }
body { font-family: system-ui, sans-serif; margin: 0; color: #222; background: #fff; }
.controls {
  display: flex; gap: 1rem; flex-wrap: wrap; align-items: center;
  padding: .4rem 0 .6rem; margin-bottom: .2rem;
}
label { display: flex; flex-direction: column; gap: .2rem; font-size: .85rem; color: #444; }
select, input[type=range] { font-size: .9rem; }
select { padding: .25rem .4rem; border: 1px solid #aaa; border-radius: 6px; background: #f5f7fa; }
input[type=range] { width: 200px; accent-color: #1d3557; }
canvas { display: block; width: 100%; max-width: 560px; border-radius: 8px; background: #f0f4f8; }
.section-label {
  font-size: .75rem; font-weight: 600; letter-spacing: .05em; color: #5a7088;
  text-transform: uppercase; margin: .4rem 0 .15rem;
}
.result {
  margin-top: .5rem; font-size: 1rem; font-weight: 700; min-height: 1.5em;
  color: #1d3557;
}
// Code not found

Notice how a pure sine wave produces a smooth, clean autocorrelation, while a sawtooth or square wave (rich in harmonics) creates a more jagged curve — yet the first peak still lands at the true period. This robustness is why autocorrelation beats simply hunting for the loudest frequency in a fast Fourier transform: harmonics can easily fool a spectrum peak-picker, but they reinforce the autocorrelation peak at the fundamental.

The Real Complexity

How expensive is autocorrelation?

  • Naive implementation: for each lag τ\tau from 00 to N1N-1 you compute a dot product over NN samples — total cost O(N2)O(N^{2}) per frame. For a 44 100 Hz signal with 20 ms frames (N882N \approx 882) that is around 780 000 multiplications per frame, 44 frames per second — feasible, but tight on embedded hardware.
  • FFT acceleration: the autocorrelation of a signal equals the inverse FFT of its power spectrum (Wiener–Khinchin theorem). Two FFTs and one pointwise multiply drop the cost to O(NlogN)O(N \log N), giving a 30–50× speedup at typical frame sizes.
  • YIN's normalization: raw autocorrelation always peaks at τ=0\tau = 0 (a signal matches itself perfectly with no shift). YIN replaces it with the cumulative mean normalized difference function — a running normalization that sets the τ=0\tau = 0 value to 1 and suppresses harmonic sub-multiples of the true period, so the algorithm can confidently pick the first dip below a threshold rather than the highest peak.
  • Polyphony is hard: autocorrelation finds one period. When two notes play together, the combined autocorrelation smears across both periods. Multi-pitch estimation is an open research problem that still does not have a universally accepted solution.

The solved part — single-pitch detection — is fast, robust, and well-understood. The unsolved part — polyphony — is where the field is still active.

Where It Matters

Pitch detection is everywhere a machine needs to understand musical or vocal sound:

  • Guitar tuners and instrument apps: every chromatic tuner running on a phone uses autocorrelation (or a YIN variant) to name the note in real time.
  • Autotune and pitch correction: the algorithm first detects pitch, then shifts the signal's phase-vocoder representation to the nearest semitone.
  • Voice assistants and speech synthesis: fundamental frequency contours (prosody) carry emotion and sentence structure; extracting f0f_0 is a preprocessing step in almost every speech pipeline.
  • MIDI transcription: turning a live recording into sheet music requires knowing which note was played at every moment — autocorrelation provides the f0f_0 estimate, which is then quantized to the nearest MIDI pitch.
  • Medical acoustics: tracking the f0f_0 of a patient's voice over time can reveal vocal-fold pathologies; a deviant autocorrelation peak signals that something is wrong before a clinician can hear it.

The same self-similarity idea also appears in video (motion estimation by block matching is a 2-D autocorrelation), radar (range-Doppler processing), and any domain where "how does this signal relate to a shifted copy of itself?" is a meaningful question.

Conclusion

Pitch detection by autocorrelation rests on a beautiful fact: a periodic signal is its own best match when you shift it by exactly one period. Slide, correlate, find the first strong peak — and you have the pitch.

YIN's refinement (cumulative normalized difference, threshold at 0.1) made this robust enough for real-world noisy audio, and the FFT-acceleration made it fast enough to run in real time on modest hardware. Single-pitch detection is a solved problem.

What remains open is the polyphonic case: when multiple notes overlap, untangling their individual periods is hard in a way that autocorrelation alone cannot solve. That boundary — between what one self-similarity function can hear and what it cannot — is a microcosm of the broader story of algorithms and their limits, the theme that runs through every article on this site.

Share this article

Pick a channel — or use your device's native share sheet.

Comments

Loading comments...

https://www.kipuhub.com/en/article/pitch-detection-autocorrelation/Content licensed under CC BY-NC 4.0.