Blog / Audio Basics

How Beat Detection Actually Works: Onset Functions, Autocorrelation, and Dynamic Programming

How Beat Detection Actually Works: Onset Functions, Autocorrelation, and Dynamic Programming

When your DAW analyzes a track and places warp markers, most people assume it has found the kick drum and built a grid from there. The reality is more counterintuitive: the algorithm has no concept of kick drums, or any instrument at all. It scans for any sudden energy change across the full spectrum, converts those into a single abstract curve, and then finds the most regular pattern buried in that curve. The kick drum is just one of many events that happen to fall on the beat.

Step One: Building the Onset Detection Function

Before any beat tracking happens, the algorithm needs a simpler representation of the audio. It computes what researchers call an onset detection function (ODF) or novelty function: a 1D signal over time where peaks indicate "something started here."

The most widely used method is spectral flux:

  1. Run a short-time Fourier transform on overlapping frames. Bello et al.'s 2005 reference implementation used a 2048-sample window at 44.1 kHz (about 46 ms per frame).
  2. For each frame, compare the magnitude spectrum to the previous frame.
  3. Compute the difference at each frequency bin.
  4. Apply half-wave rectification: set negative differences to zero.
  5. Sum across all frequency bins.

The half-wave step is the key insight. Negative differences mean a frequency is fading out, and onsets are characterized by frequencies switching on, not off. By discarding the fade-outs, spectral flux becomes a clean onset indicator that spikes on drum hits, note attacks, and consonants in sung syllables.

The result is not a series of neat impulses. It is a messy curve with broad peaks, false positives from tonal vibrato, and heights that vary with the loudness of each event. The beat tracker's job is to impose structure on this mess.

Phase-Based and Complex Domain Methods

Spectral flux only measures magnitude changes, so it can miss soft onsets where a new pitch starts with minimal energy change but shifts the STFT phase relationships. The phase deviation method computes how much the phase of each bin advances between frames compared to the expected advance for a stationary sinusoid. Large phase deviations signal new events even when amplitude is constant.

A third approach, the complex domain method described in Bello et al. 2005, measures the distance between the actual complex STFT value and the value predicted from the previous two frames. This catches both magnitude and phase changes in a single calculation, making it more robust on real musical signals.

None of these methods produces a score for how likely each millisecond is to be a beat. They produce a fuzzy map of interesting moments. The tempo estimation step takes it from there.

Finding the Tempo via Autocorrelation

Once you have an ODF, you need to find its dominant period. Autocorrelation is the cleanest approach: measure how well the ODF correlates with a time-shifted copy of itself.

If beats occur every 500 ms (120 BPM), the autocorrelation function shows a peak at lag = 500 ms, another at 1000 ms, and another at 1500 ms. The smallest strong peak is the beat period.

Scheirer's 1998 paper (Journal of the Acoustical Society of America, Vol. 103) took a different route, one that predates explicit ODF computation. Rather than computing a novelty curve first, it decomposed audio into sub-bands with bandpass filters and ran each sub-band through a bank of resonant comb filters tuned to different tempo hypotheses. The comb filter that accumulated the most energy pointed to the likely tempo. This design ran causally, with no lookahead, making it suitable for real-time tempo tracking in hardware of that era.

Both approaches share the same conceptual problem: a track analyzed at 140 BPM looks nearly identical to one analyzed at 70 BPM. The autocorrelation has peaks at both periods. The algorithm must use signal strength and heuristics about typical musical tempo ranges to resolve the ambiguity, and it regularly gets this wrong.

Placing Beats: The Global Optimization Problem

Knowing the tempo period is not enough. You also need the phase: when does the first beat actually fall? And what happens when tempo drifts?

Ellis formalized beat tracking as a global optimization problem in his 2007 paper (Journal of New Music Research, 36:1). Define a scoring function over all possible beat sequence choices:

  • Fit bonus: beats that land on peaks in the ODF score higher.
  • Regularity bonus: beats with inter-beat intervals close to the estimated tempo score higher.
  • Deviation penalty: beats that stray far from the tempo estimate are penalized.

Dynamic programming then finds the globally optimal sequence: a forward pass through the track accumulates the best partial score at each step, and a backtrace from the end reconstructs the optimal beat sequence. This runs in linear time relative to track length.

Dynamic programming is necessary because greedy methods fail. Picking the locally strongest onset at each step accumulates timing drift that compounds across the track. The globally optimal path sometimes passes through a weaker onset to maintain better alignment elsewhere.

This also explains why offline warp marker analysis in DAWs takes a moment even for short tracks: the algorithm needs to read the complete audio before it can commit to a beat grid.

Where Beat Detection Fails

The failure modes are instructive.

Rubato and expressive timing: Classical piano, flamenco, and folk singing involve intentional timing variation where beats compress and expand. Dixon's BeatRoot algorithm (Journal of New Music Research, 30:1, 2001) was designed for expressive performances, using a much wider tempo tolerance window and agent-based tracking that maintains multiple competing hypotheses. It handles expressive music better than fixed-grid trackers but still struggles with large, sudden tempo changes.

Complex polyrhythm: When two equally strong rhythmic layers imply different tempos, the ODF peaks from both layers get merged. The tracker often settles on a tempo related to neither, or flips between interpretations across the track.

Syncopated rhythms: In reggae, the rhythmic emphasis is on the offbeats. Spectral flux peaks more strongly on the "ands" than on the beats. Beat trackers will routinely place the grid half a beat off, with high confidence. The algorithm is not wrong by its own objective function. It just picked the wrong phase.

Silent downbeats: A beat with no articulation, like a held note sustaining across a barline, produces no ODF peak. The tracker sees a gap and may insert a spurious micro-adjustment.

The Real-Time Case

DJ software and live performance tools need beat tracking without lookahead. Real-time trackers maintain a probabilistic tempo and phase estimate that updates with each new audio frame, using approaches related to particle filtering or Bayesian state estimation. They sometimes produce small visible corrections mid-track as evidence accumulates, which is why DJ sync locks in fully after a few beats rather than instantly.

The STFT window size creates an inherent latency: a 2048-sample window at 44.1 kHz takes 46 ms before onset information is available. Real-time trackers trade tempo accuracy for lower latency by using shorter windows, at the cost of frequency resolution.

What This Means for Your Workflow

Beat tracking works reliably on music with clear, regular onsets and consistent tempo: electronic music, most pop, quantized hip-hop. It degrades on acoustic music with expressive timing, complex polyrhythm, or sparse arrangements.

When warp markers end up half a beat off, the cause is usually the syncopation problem: strong offbeat events pulled the phase estimate. Manually nudging the first detected beat to the correct position, then locking the tempo, usually fixes the whole grid. The dynamic programming pass has already solved the rest.

The same ODF pipeline appears in automatic drum transcription, groove template extraction, and sync licensing tools that match audio to MIDI. Every time your DAW offers to quantize a drum loop "to grid," some version of spectral flux and autocorrelation ran first to find the grid.