The problem sounds impossible when stated plainly. A phone's microphone picks up a few seconds from the middle of a song. The song is playing through a poor loudspeaker, in a room with an echo, while people talk and glasses clink. The phone compresses the recording and sends it over a mobile network. Somewhere a server must decide which of perhaps a hundred million tracks it came from, and must do so in about a second.

The method that solved it was published in 2003 by Avery Wang, a co-founder of the company whose name has become a verb for doing this. His paper is eight pages long. It is still the foundation of the field, and it is worth following step by step, because each step discards information on purpose.

Step one: a spectrogram

The raw recording is a list of air-pressure values, tens of thousands a second. Two recordings of the same song share almost none of those values, so comparing them directly is hopeless.

The first move is the standard one in audio. Cut the sound into short overlapping slices and work out, for each slice, how much energy there is at each frequency. Lay the slices side by side and the result is a picture, with time across, frequency upward and brightness for loudness.

A spectrogram already makes the task easier, since the same song produces a similar picture each time. But the picture of the bar recording is smeared with noise, tilted by the loudspeaker's poor bass, and faint.

Step two: keep only the peaks

Wang's first insight was to throw nearly all of the picture away.

Look for points in the spectrogram that are louder than everything round them: local peaks. A few dozen stand out each second. Keep their positions, which is to say the time and frequency of each. Discard everything else, including how loud the peaks were.

What remains looks like a star chart, a scatter of dots. Wang called it a constellation map.

The reasoning is this. Noise adds energy across the picture, but the loudest points of the music tend to remain the loudest points. A voice talking over the track adds some new peaks of its own and hides a few, and most of the song's peaks survive. A loudspeaker that distorts the balance changes how bright the peaks are and leaves their positions alone. Compression for transmission is designed to preserve the perceptually prominent parts of the sound, which are these same peaks.

By keeping only positions, the method ignores exactly the things that the bar and the telephone corrupt.

Step three: pair the peaks

A single peak is of little use for searching. "Something at 1,200 hertz" is true of every song ever recorded.

So peaks are combined in pairs. Take one as an anchor and pair it with each of several peaks that follow it within a short window. For each pair, record three numbers: the frequency of the first, the frequency of the second, and the time between them.

Those three numbers are packed into a single value, a hash. Alongside it is stored the time at which the anchor occurred in the track.

A pair is far more distinctive than a single peak. There may be a thousand possible frequencies for each peak and a thousand possible gaps, giving about a billion possible hashes. Any one of them occurs in very few songs.

Notice also that the hash contains only a time difference. It does not depend on where in the song the listener started. That makes the fingerprint work from any point in the track.

Step four: the index

Every track in the catalogue is processed in this way in advance. A three-minute song yields some thousands of hashes. They go into an enormous table that maps each hash to a list of the tracks containing it, and the time at which it occurs in each.

This is the same structure as the index at the back of a book, or a search engine's index of words. Looking up one hash takes a negligible time, however large the catalogue.

Step five: line them up

The phone computes hashes from its few seconds and sends them. The server looks each one up. Back comes a long list of candidate matches: this hash appears in track A at 47.2 seconds, in track B at 12.8 seconds, in track C at 201.4 seconds, and so on.

Most are coincidences. With billions of entries in the table, any hash will be found in some unrelated songs.

The second insight separates the real match from the coincidences. If the sample truly comes from track A, starting 45 seconds in, then every genuine match will show the same offset. A hash that occurred 1.0 second into the sample will be at 46.0 in the track. One from 3.5 seconds will be at 48.5. Subtract the sample time from the track time and you get 45.0 every time.

Coincidental matches have random offsets.

So for each candidate track, the server makes a tally of the offsets. For the wrong tracks the tally is flat: a few matches scattered about. For the right track there is a tall spike at one value. The size of the spike is the score.

This is very robust. Suppose only one hash in twenty from the noisy sample survives intact. If twenty or thirty still agree on one offset in one track, that cannot be chance. The match is declared, and the offset reveals as a bonus exactly where in the song the sample came from.

Why it is fast

Nothing in the search compares audio with audio. It is a matter of looking up numbers in a table and counting. The cost depends on the number of hashes in the sample, a few hundred, and hardly at all on the size of the catalogue.

The original paper reported identifying tracks from a database of over a million within fractions of a second on the hardware of the day. Catalogues are now a hundred times larger and the principle is unchanged.

What it cannot do

The method recognises a specific recording. It matches the pattern of peaks in one particular performance as mastered.

So it does not recognise:

  • A live performance of a song, which has different timing and different peaks.
  • A cover version by another artist.
  • Someone humming or singing the tune.
  • A track played at a different speed. Changing the speed shifts every frequency and stretches every gap, so the hashes change. DJs who alter the tempo defeat basic fingerprinting, and later systems were adapted to cope with moderate changes.

Each of those requires a different approach. Recognising a hummed tune means matching the melody, as a contour of rising and falling pitch, regardless of key, tempo and timbre. Systems that do it use neural networks trained to place a hummed fragment and a studio recording of the same tune close together in an abstract space. Identifying covers is a research field of its own.

It is a neat illustration of a general point. Fingerprinting is extremely good at answering "is this the same recording?" and has no notion of "is this the same song?" The second question is about musical meaning, and it is much harder.

Other uses

The same technique, or close relatives, runs in several places most people never see.

Copyright matching. Video platforms fingerprint every upload and compare it with a reference database supplied by rights holders. A match can block the video, mute it or redirect its advertising revenue. The system handles an enormous volume automatically, and it is also the source of many disputed claims, as when a recording of a public-domain piece is matched to someone's commercial release of the same piece.

Broadcast monitoring. Services listen to thousands of radio and television stations and log every track and advertisement played, which is how royalties are distributed and how advertisers confirm that their spots were aired.

Second-screen and audience measurement. Some apps and smart televisions listen for the fingerprint of a programme or an advertisement in order to know what is being watched. This is a use to be aware of. An app with microphone permission can identify what is playing in a room without recording any speech, and permission requested for one purpose can serve another.

Synchronisation. Aligning separately recorded audio and video in film editing.

Why it lasted

The method has survived twenty years of progress in machine learning. Newer systems learn their fingerprints with neural networks and improve on it in difficult conditions. The original remains competitive for the basic task and is far cheaper to run.

The reason is that it was built on a precise understanding of the problem. Wang asked what survives a bar, a bad speaker and a telephone, found that the answer was the positions of the spectral peaks, and designed everything else to use only those. Most of the method consists of deciding what to ignore.

Anyone can try a version. Open-source implementations exist in a few hundred lines of Python. Building one, on a folder of a few dozen tracks, takes an afternoon and teaches the central lesson well: a good representation of the signal does most of the work, and the matching after that is lookup and counting.