Sampling
Record a sound and all you keep is a list of numbers, one per instant, with nothing at all in between. Everything the recording can ever say has to come out of that list. So the question worth asking is not what the samples look like, it is what else could have produced them. Above half the sample rate the answer is: infinitely many things, and nothing in the list says which one you had. Most demonstrations of this draw a fast wave and a slow one through the same dots and invite you to see that they meet. Meeting is not the claim. The claim is that at every instant a measurement was taken the two waves held the identical value, and that is a statement about whole numbers, so this page settles it with whole numbers and prints the working.
New to sampling? Start here
A sample is one measurement of a signal, taken at one instant. Recording sound means taking them at a steady rate and keeping nothing in between: 44,100 a second for a CD. Everything the recording can ever say about the sound has to be recoverable from that list of numbers, because the numbers are all there is.
The sample rate is how many you take per second. It is the only thing you get to choose here, and it decides which sounds survive. Frequencies below half the rate come back; frequencies above it come back as something else, wearing the disguise of a lower frequency. That impostor is called an alias.
One phrase to keep straight. This page says two waves agree at the sample instants. It does not mean they look alike, and it does not mean they are nearly equal. It means that at each moment a measurement is taken, the two waves are at the identical value, so the list of numbers is the same list.
Numbers in a fixed-size box
A number on paper can be as long as it needs to be. A number in a computer gets a fixed number of digits and no more, so there is always a largest one it can hold and always a smallest difference it can tell apart.
Almost everything surprising about computer arithmetic follows from that. What happens when you count past the largest value depends on which kind of number it is: a fixed-width integer wraps round to the smallest, and a floating-point number goes to infinity instead. A value too fine for the gap is not rejected, it is rounded to the nearest one that fits, and then the rounding is added to the next rounding. These are not faults in the machines here. They are the edges of the box, and the machines are what people built to live inside it.
The machine for this idea on its own is Two's Complement, if you would rather press it than read about it.
Machines here that come first: Interlace, Bit, Nibble, Byte.
The same samples fit two different waves
1 A cosine at a whole-number frequency, sampled at a whole-number rate you set
Everything the recording keeps is the right-hand column. Nothing exists between the instants, and that is the whole of the difficulty.
A 3 Hz cosine measured 20 times a second. Half the rate is 10 Hz, so this signal is below the limit and recoverable. The list below is everything the recording keeps.
2 A second frequency built from the first by adding the sample rate, not searched for
There are two ways to build one, and most explanations show only the first. The second is a reflection, and it is the case a listener actually meets.
7 frequencies shown that are indistinguishable from 3 Hz at this rate, and the list is cut for the screen rather than by the mathematics: it never ends. Two ways to build one, because a cosine of minus f samples like a cosine of f.
3 The two frequencies' sample values compared in integer arithmetic, at every instant
A cosine sample depends only on the frequency times the instant, taken modulo the rate. So this needs no decimals, and takes no chances with them.
f is 3 Hz and g is 17 Hz. A cosine sample depends only on the frequency times the instant, modulo the rate, so equality is decided in whole numbers. Of 512 instants compared, 0 disagreed.
4 What a reconstructor returns, and the residual double precision leaves behind
The integers say the samples are equal. Double precision does not quite agree, and the gap is printed here rather than rounded out of sight.
The wave comes back. 3 Hz is below 10 Hz, so reconstruction returns the frequency that was there.
These ran in this browser when the page loaded. Each claim, whether it held, and the number behind it.
| claim | held | measured |
|---|---|---|
| adding the sample rate to a frequency leaves every sample unchanged | yes | 3 Hz and 23 Hz differ by 20 Hz, which is 1 whole sample rates of 20 Hz, so the cosine argument advances by whole turns at every instant. This settles every n at once rather than a sample of them |
| counted a second way, per instant, in integer arithmetic | yes | 512 instants compared by residue (f*n) mod fs, which shares no line with the divisibility test above. 0 disagreed |
| a tone above half the rate samples identically to its reflection | yes | 17 Hz and 3 Hz sum to 20 Hz, a whole sample rate, so they are the same point on the turn reflected. 512 instants compared by residue, 0 disagreed, and reconstruction returns 3 Hz. This is the case a listener actually meets |
| double precision does not reproduce the exact result | yes | the integers say the samples are equal; evaluated as doubles the largest gap over 512 instants is 7.938e-13, at instant 485. Not zero, because 2*pi is not a representable number. The page prints this rather than rounding it out of sight |
| a frequency below half the rate is the one that comes back | yes | 3 Hz at 20 Hz sampling is below the halfway point of 10 Hz, and reconstruction returns 3 Hz |
| a frequency above half the rate comes back as a different one | yes | 17 Hz is above the halfway point of 10 Hz, and reconstruction returns 3 Hz. The wave is not recovered and nothing in the samples reports that |
| the family of indistinguishable frequencies is unbounded | yes | 7 members shown within 3 rates either side and every one agrees at every sample instant. The list is cut for the screen, not by the mathematics |
What is real here, and what is not
The proof is arithmetic, and the drawing is only a drawing
Two cosines agree at every sample instant when their frequencies are congruent, plus or minus, modulo the sample rate. That is decided here in whole numbers, twice, by routes that share no line: once as a statement about the two frequencies, and once instant by instant as a comparison of residues. The picture on this page is drawn from the same numbers but nothing is concluded from it. A reader who wants to check the page can do the arithmetic by hand, which is why every value on it is a small integer.
There are two branches and the first draft of this page had one
The obvious family is the signal frequency plus whole multiples of the sample rate. The other exists because a cosine is an even function, so a frequency and its negative sample identically, and the reflected family is the sample rate minus the frequency, and so on. That second branch is the ordinary audible case: seventeen hertz sampled at twenty comes back as three. The engine's first draft tested only the difference and would have called those two waves distinguishable. It was the second method that disagreed with the first, which is what a second method is for.
The floating-point residual is shown, not hidden
The exact result is that the samples are equal. Evaluated as double-precision numbers they are not: the largest gap over five hundred and twelve instants is around ten to the minus thirteen, because two pi is not a representable number and the two arguments are reduced from different sizes. The page prints that residual. It is a fact about the arithmetic and not about the waves, and rounding it out of sight would make the page's own evidence tidier than the truth.
The theorem is Shannon's, and Nyquist is credited for something narrower
This page does not use the name Nyquist theorem. Shannon states it as Theorem 1 in Communication in the Presence of Noise in January 1949, and in the same paper credits Nyquist with pointing out the importance of the one-over-two-W interval, which is a narrower contribution than stating the reconstruction theorem. Nyquist's 1928 paper is about telegraph signalling rate and does not state it. The name attached to him afterwards. Both halves of that are in the archived source this page cites.
Whole numbers are a restriction on the page, not on the world
Frequencies and rates here are integers so that congruence is exact and the page can say so without hedging. Real signals are not integers and real recordings are not either. Nothing about the mechanism depends on it: the same argument runs over the reals with the same conclusion, and the restriction only buys the ability to demonstrate it rather than assert it.
A pure tone exactly at half the rate is the case nobody should trust
The theorem asks for a signal containing no frequencies higher than half the sample rate. Exactly at the boundary a cosine can be sampled at its zero crossings every time and recorded as silence, which is why the boundary is stated as a limit approached rather than a rate that is safe to use. The page marks that case rather than pretending the rule is clean there.
No sound, deliberately
Aliasing is the one subject on this site where playing the two tones would be tempting, and it fails the test the studio applies. The claim is that the sample lists are identical, and a listener hearing two identical lists hears one thing twice, which demonstrates nothing that the equality check has not already settled. A tone that made the point audible would have to be the wave before sampling, which is exactly the thing a recording cannot contain. So the answer is no, and it is written down here so the question is not reopened.