LISTEN · MEASURE · UNDERSTAND

YIN vs Autocorrelation for Pitch Detection

Compare YIN and autocorrelation pitch estimation. See how lag, thresholds, windows and harmonic ambiguity change the results.

YIN and autocorrelation estimate the fundamental period by comparing samples at different delays. Autocorrelation searches for strong similarity. YIN calculates a squared difference and normalizes it to compare candidate minima. Both methods need enough clear signal to choose a useful period. Implementation choices can affect octave errors, coverage and timing.

Turn a Sample Lag Into Hertz

A lag is a delay between sample positions. If a waveform repeats every 147 samples at a rate of 44,100 samples per second, it repeats about 300 times per second. Dividing the sample rate by the estimated period in samples gives Hz. Real periods can fall between integers, so some methods refine the estimate around a candidate.

A detector searches delays that correspond to its intended frequency range. A lower note needs a longer delay than a higher note. The actual sample rate determines which frequency corresponds to a particular lag. Record the sample rate and range when evaluating an implementation.

What Autocorrelation Looks For

Autocorrelation compares a window with a delayed version of itself. A delay of about one period can produce a similarity peak. Other peaks can occur at multiples of the period. A normalized implementation can account for energy when evaluating the candidates. The zero-lag match simply compares a signal with itself and does not give the period.

The detector still needs a rule for choosing which peak to accept. Harmonics, uneven cycles or another source can make additional candidates plausible. If the tool selects a delay near half or twice the true period, the frequency can be off by an octave.

How YIN Finds a Candidate

YIN forms a difference function from squared differences between delayed sample pairs. A delay that lines up a repeated waveform gives a smaller difference. The original method normalizes the difference at each positive lag against the cumulative mean up to that lag. It then looks for a suitable early local minimum below a threshold and refines its period estimate.

Step Autocorrelation family YIN
Compare delayed samples Similarity Squared difference
Inspect candidate periods Peaks away from zero Minima of a normalized function
Choose a result Peak, threshold and continuity rules depend on implementation Early low candidate under a threshold with method-specific refinements
Handle weak input Used rules may reject or flag it Used rules may reject or flag it

The method label gives a starting point. Read the implementation details for its supported pitch range, threshold and which data it reports. An output called confidence can refer to different metrics across tools.

How Window and Hop Affect the Output

A 2,048-sample window at 44.1 kHz covers about 46.4 milliseconds. A 50 Hz tone completes only about 2.32 cycles in that whole duration. Comparing far-apart samples leaves less overlap in some implementations. A longer frame can give more evidence for a low note and can blur a rapid change.

The hop determines how far the detector advances between frames. A smaller hop can provide more frequent estimates without making each window shorter. See browser capture, frames and display for why update speed can differ from measurement detail.

An Octave-Ambiguous Example

A note near 200 Hz has a strong component around 400 Hz. The waveform can have prominent features about 2.5 milliseconds apart, alongside a true repeating pattern of about 5 milliseconds. A short-period candidate can look attractive. The octave error guide shows how to check whether the displayed 400 Hz came from a true pitch change.

Compare More Than the Smallest Error

Test with known tones, strong harmonics, pauses and short transitions. Use the same samples, search range and frame settings where the comparison permits. Record error in cents on accepted frames, the number of wrong octaves and how much suitable audio was tracked. See the pitch accuracy test guide to choose a benchmark.

Use the pitch identifier with one stable reference sound to examine the result fields. A good comparison includes the input and the output rules. A method that omits many difficult frames may show small error on the few it kept. Coverage helps reveal the tradeoff.

Save the Threshold and Selection

When retesting a feature, write down the processed segment and pitch bounds. If a lower threshold rejects more samples, report the lost coverage alongside any drop in mistakes. Behavior can also depend on an added level or voicing gate. Keep those details visible when comparing runs.

Further reading

Original YIN fundamental frequency estimation paper

Frequently Asked Questions

Is YIN simply the highest peak in an FFT?

YIN uses a difference function evaluated over delays. Some implementations use FFT operations to accelerate calculations. The pitch selection is different from taking the largest spectral bin.

Does autocorrelation always pick the wrong octave on a harmonic-rich note?

It can often find a useful period. The result depends on the signal, normalization and choice of candidate. Test a clear reference with strong harmonics if that input matters.

Which method should I choose for fast lines?

Test both on well-labeled transitions and compare delay, accuracy and gaps. Window length and grouping can matter as much as the method family.