Skip to content

Sampling & aliasing

Predict where a tone lands once only its samples are kept, see why a fast tone becomes a slow one, and why a square wave does it at any note.

Before this7.1 · 3 more
Chapter 10 · Lesson 1 of 5

First, the picture

I sample a 7 kHz tone 8000 times a second. What do the samples sound like? Watch the dots, then the wave through them.

Seven kilohertz, sampled at eight

Dots every 0.125 ms on a 7 kHz tone.

A 7 kHz tone, x_c(t): seven ups and downs every millisecond.

dots taken
0
slower wave through the dots
not drawn yet
tone in
7 kHz
sample rate f_s
8 kHz
0.00 / 10.00 s
Describe this picture

One panel, 2 ms of a 7 kHz tone, with a key naming the traces as they appear: “7 kHz tone”, “samples x[n]” and “1 kHz wave through the same dots”. The first caption says “A 7 kHz tone, x_c(t): seven ups and downs every millisecond.” Seventeen dots, 0.125 ms apart on the tone, then drop onto it one after another, about every 0.2 s on screen, and the last has landed by 3.5 s: “8000 samples a second: one dot every 0.125 ms, seventeen dots in 2 ms.” The readout “dots taken” counts them from 0 to 17. Next the 7 kHz curve fades, a slower wave draws itself through the dots, and the caption says “The same dots fit a 1 kHz wave. With only the dots kept, the two cannot be told apart, and you hear 1 kHz.” The readout “slower wave through the dots” reads “not drawn yet” until the wave appears, then “1 kHz”. Two fixed readouts, “tone in” (7 kHz) and “sample rate f_s” (8 kHz), sit beside the “Hear the samples” button, which plays one second of the samples.

The same dots, two waves

First the words. The real tone, which goes on in continuous time, I will write xc(t)x_c(t). Sampling keeps its value every TsT_s seconds, so the samples are

x[n]=xc(nTs),Ts=1fs,x[n]=x_c(nT_s),\qquad T_s=\frac1{f_s},

where nn is the sample number and fsf_s is the sample rate, here 8000 samples a second, or 8 kHz. You met this in Sinusoids (3.2). The dots come every Ts=1/8000T_s=1/8000 s, which is 0.125 ms.

Press “Hear the samples” in the picture at the top of the page: one second of the samples sounds at 1 kHz.

Look at the slow wave and find a dot it misses. There is none. It passes through all seventeen. It is a clean, smooth cosine, only the wrong one: the samples are exactly those of a 1 kHz cosine, so nothing in them says “7 kHz”. That is an alias: a clean, slower, wrong tone, not noise.

A ceiling fan filmed by a phone can seem to turn slowly, for the same reason. The camera sees the blades only once per frame.

Where a tone lands

Why 1 kHz and not some other number? The rule is the one from Sinusoids (3.2), written in hertz instead of radians per sample.

The digital frequency is Ω=2πf/fs\Omega=2\pi f/f_s (3.2 §4). Adding fsf_s to ff adds 2π2\pi to Ω\Omega, and an extra 2π2\pi gives the same samples (3.2 §5). So ff and f+fsf+f_s give the same samples. A cosine is even, so ff and fs−ff_s-f give the same samples too. Every tone therefore has a twin between 0 and half the sample rate. That half is the Nyquist frequency,

fN=fs2.f_N=\frac{f_s}{2}.

For the 7 kHz tone, 3.2’s rule goes like this: Ω=2π⋅7/8=1.75π\Omega=2\pi\cdot7/8=1.75\pi rad/sample. Subtract 2π2\pi to get −0.25π-0.25\pi. Its mirror twin is 0.25π=2π⋅1/80.25\pi=2\pi\cdot1/8, which is 1 kHz at 8 kHz.

Watch the dot as the input frequency climbs. Its landing point rises with it until the tone reaches fN=4f_N=4 kHz. Then it turns back down, reaching 0 at fs=8f_s=8 kHz, and climbs again.

Where a tone lands

Sample rate 8 kHz. Each input frequency lands somewhere from 0 to 4 kHz.

Sample rate 8 kHz. Watch where each input frequency lands.

tone in
0.0 kHz
lands at
0.0 kHz
0.00 / 14.00 s
Describe this picture

Sample rate 8 kHz. The input frequency, 0 to 24 kHz, runs along the bottom, and the height shows where it lands, from 0 to 4 kHz with a little headroom above, up to 4.8. Dotted guides mark f_N, f_s, 2f_s and 3f_s. The first caption says “Sample rate 8 kHz. Watch where each input frequency lands.” and both readouts, “tone in” and “lands at”, begin at 0.0 kHz. At 12 kHz the caption reads “Past f_N = 4 kHz the landing point turns back down; past f_s = 8 kHz it climbs again. 1, 7 and 9 kHz all land on 1 kHz.” and the readouts show 12.0 and 4.0 kHz. Rings are stamped along the path at 1, 7, 9, 15, 17 and 23 kHz, joined by a dashed line at 1 kHz, and the last caption says “Six different tones, one landing point: 1, 7, 9, 15, 17 and 23 kHz all sound like 1 kHz after sampling at 8 kHz.” When the clip has finished, dragging across the plot, or the arrow keys (0.5 kHz a step, Home and End for the ends), choose the input frequency, as the hint under the readouts says. The caption then follows, for example “7 kHz, sampled at 8 kHz, lands at 1 kHz.”

Notice the rings stamped along the path: six different tones, 1, 7, 9, 15, 17 and 23 kHz, all land on 1 kHz. When the animation has finished, the instrument is yours. Drag across the plot to choose the input frequency, pick 7 and check the dot sits at 1.

The rule behind the zig-zag has two steps. First take away whole sample rates: the remainder fr=f mod fsf_r=f\bmod f_s. For example 25 mod 8=125\bmod 8=1, because 25=3⋅8+125=3\cdot8+1. Then fold the remainder back below fNf_N:

fr=f mod fs,fa=min⁡(fr, fs−fr).f_r=f\bmod f_s,\qquad f_a=\min(f_r,\,f_s-f_r).

The fold is the cosine’s two arrows again. A cosine is two arrows spinning opposite ways (see Complex exponentials & phasors (3.4)). Sampling moves each arrow’s frequency up or down by whole multiples of fsf_s. The arrow at −f-f moved up by fsf_s sits at fs−ff_s-f. The page The sampling theorem (10.2) draws the whole picture.

Now run it backwards. The dots trace a 1 kHz wave, and you know the tone was 7 kHz. The sample rate was between 5 and 10 kHz. What was it? Try fs=8f_s=8: the tone lands at min⁡(7,1)=1\min(7,1)=1 kHz. Try fs=6f_s=6: 7 mod 6=17\bmod6=1 and min⁡(1,5)=1\min(1,5)=1 kHz. So the sample rate was 8 kHz or 6 kHz.

That is the catch. Once the dots are all you have, the tone and its alias cannot be told apart, and the aliasing cannot be undone. Converters filter before they sample, which is the subject of Anti-aliasing and practical converters (10.4).

The maths behind it · null spaces

Sampling keeps a few entries of a very long list. Two different lists that agree at the kept entries, the 7 kHz and the 1 kHz waves, become the same short list. Their difference is zero at every kept entry, so it is invisible. The set of differences that a map makes invisible is called its null space.

A square wave aliases at any note

So far there was one tone. Real sounds hold many. In Signals as sums of sinusoids (7.1) you built a square wave from spinning arrows at the odd harmonics 3f,5f,7f,…3f,5f,7f,\dots, with lengths 4/(πk)4/(\pi k). They never stop.

That is the problem. However low the note, some harmonic lies above fNf_N, and it lands somewhere else. Here is a 1.5 kHz square wave sampled at 8 kHz. Watch its first four harmonics move, one at a time, into the sampled panel.

A square wave's harmonics, folded

A 1.5 kHz square wave sampled at 8 kHz; its first four harmonics.

A 1.5 kHz square wave: lines at 1.5, 4.5, 7.5 and 10.5 kHz, the first four of its odd harmonics.

last line moved
none yet
note
1.5 kHz
sample rate f_s
8 kHz
0.00 / 10.00 s
Describe this picture

Two panels, the input and the sampled spectrum, for a 1.5 kHz square wave sampled at 8 kHz. The first caption reads “A 1.5 kHz square wave: lines at 1.5, 4.5, 7.5 and 10.5 kHz, the first four of its odd harmonics.” The bars are labelled “k = 1”, “3”, “5” and “7”, with lengths 1.273, 0.424, 0.255 and 0.182, and a vertical line marks “f_N”. The bars then move one at a time into the sampled panel, each leaving a dashed outline where it was. After the second the caption says “1.5 kHz stays where it is. 4.5 kHz is above f_N = 4 kHz and lands at 3.5 kHz.” and the readout “last line moved” shows “4.5 → 3.5 kHz”. At the end it shows “10.5 → 2.5 kHz” and the caption says “After sampling: lines at 0.5, 1.5, 2.5 and 3.5 kHz. Only 1.5 kHz belongs to the note; the others are not multiples of it, so it sounds out of tune.” Fixed readouts “note” (1.5 kHz) and “sample rate f_s” (8 kHz) sit beside the “Hear the samples” button.

The first stays where it is; the other three land at 3.5, 0.5 and 2.5 kHz. Only 1.5 kHz belongs to the note, and the others are not multiples of it, so it sounds out of tune. Press “Hear the samples” to hear the sampled square wave. A cheap synthesiser whose high notes sound out of tune does exactly this.

The four lines shown carry 0.9496 of the square wave’s power (Fourier series coefficients (7.2)). The rest of the harmonics fold too. A caution: this works out of tune only when the note does not divide fsf_s evenly. A 1 kHz square wave at 8 kHz folds its harmonics onto multiples of 1 kHz. At 1.5 kHz and 8 kHz, as here, they do not.

The maths behind it · sampled time series

A time series measured at a fixed interval aliases too. A daily temperature cycle read once every 25 hours shows a fake cycle: 1/24−1/25=1/6001/24-1/25=1/600 cycles per hour, so a cycle 600 hours, or 25 days, long.

Worked example

  1. A CD chain. A 25 kHz interferer meets fs=44.1f_s=44.1 kHz, so fN=22.05f_N=22.05 kHz. The remainder is fr=25f_r=25 kHz, less than fsf_s, and above fNf_N. So fa=44.1−25=19.1f_a=44.1-25=19.1 kHz, inside the audio band.
  2. 9 kHz at 10 kHz. fr=9f_r=9 kHz, and fa=min⁡(9,1)=1f_a=\min(9,1)=1 kHz.
  3. Through 3.2. 7 kHz at 8 kHz is Ω=2π⋅7/8=1.75π\Omega=2\pi\cdot7/8=1.75\pi rad/sample. Minus 2π2\pi gives −0.25π-0.25\pi, whose twin is 0.25π=2π⋅1/80.25\pi=2\pi\cdot1/8. That is 1 kHz.
  4. The 1.5 kHz square wave at 8 kHz. The harmonics at 1.5, 4.5, 7.5, 10.5, 13.5, 16.5, 19.5 and 22.5 kHz land at 1.5, 3.5, 0.5, 2.5, 2.5, 0.5, 3.5 and 1.5 kHz.
  5. Reverse. A 7 kHz tone that shows as 1 kHz, with 5<fs<105<f_s<10 kHz, means fs=8f_s=8 or 66 kHz.
  6. Audio. To keep a 20 kHz tone below fNf_N you need fs>40f_s>40 kHz. The page The sampling theorem (10.2) explains why that is enough.

Where you’ll meet this

Every audio interface, camera and sensor samples. The next pages build on this one. The page The sampling theorem (10.2) shows the copies of the spectrum behind the fold. The page Reconstruction (10.3) rebuilds the wave from its dots. The page Anti-aliasing and practical converters (10.4) filters first, so the alias never forms.

Reference card

QuantityFormulaNotes
Samplingx[n]=xc(nTs)x[n]=x_c(nT_s)Ts=1/fsT_s=1/f_s
Nyquist frequencyfN=fs/2f_N=f_s/2half the sample rate
Digital frequencyΩ=2πf/fs\Omega=2\pi f/f_s rad/sampleSinusoids (3.2)
Twinsff, f+kfsf+kf_s and kfs−fkf_s-f give the same sampleskk a whole number
Where a tone landsfr=f mod fsf_r=f\bmod f_s, fa=min⁡(fr, fs−fr)f_a=\min(f_r,\,f_s-f_r)0≤fa≤fN0\le f_a\le f_N
Square waveharmonics 3f,5f,…3f,5f,\dots never stopsome lie above fNf_N for any note

End of lesson 10.1

Where to go next.

Phasorium
LibraryEvery lesson, in order

Parts

About Phasorium
Look