Aliasing
When a signal contains frequencies above the Nyquist frequency , sampling folds them back into the range , where they become indistinguishable from genuine low frequencies. This is aliasing: wagon wheels that turn backwards in films, moiré patterns on striped shirts, and the harsh tones of a badly downsampled recording. Once it has happened, no processing can undo it.
The folding rule
A tone at frequency sampled at produces exactly the same samples as one at for any integer . The frequency you see is the one in :
import numpy as np
fs = 100
n = np.arange(10)
for f in (30, 70, 130, 170, 230):
alias = abs(f - fs * round(f / fs))
same = np.allclose(np.cos(2 * np.pi * f * n / fs), np.cos(2 * np.pi * alias * n / fs))
print(f"{f:3d} Hz sampled at {fs} Hz looks like {alias:2d} Hz: identical samples {same}") 30 Hz sampled at 100 Hz looks like 30 Hz: identical samples True
70 Hz sampled at 100 Hz looks like 30 Hz: identical samples True
130 Hz sampled at 100 Hz looks like 30 Hz: identical samples True
170 Hz sampled at 100 Hz looks like 30 Hz: identical samples True
230 Hz sampled at 100 Hz looks like 30 Hz: identical samples True
For sines (rather than cosines) a folded alias also flips sign, but the frequency is the same.
Wheels and strobes
A wheel with 8 spokes turning at revolutions per second, filmed at 24 frames per second, shows a spoke pattern repeating at Hz. If is just below 24, the pattern appears to turn slowly backwards:
fps, spokes = 24, 8
for rps in (1.0, 2.9, 3.0, 3.1, 5.5):
f = spokes * rps # pattern frequency
alias = f - fps * round(f / fps) # signed: negative means backwards
print(f"wheel at {rps} rev/s: pattern {f:5.1f} Hz appears as {alias:+5.1f} Hz"
f" -> {alias / spokes:+.3f} rev/s")wheel at 1.0 rev/s: pattern 8.0 Hz appears as +8.0 Hz -> +1.000 rev/s
wheel at 2.9 rev/s: pattern 23.2 Hz appears as -0.8 Hz -> -0.100 rev/s
wheel at 3.0 rev/s: pattern 24.0 Hz appears as +0.0 Hz -> +0.000 rev/s
wheel at 3.1 rev/s: pattern 24.8 Hz appears as +0.8 Hz -> +0.100 rev/s
wheel at 5.5 rev/s: pattern 44.0 Hz appears as -4.0 Hz -> -0.500 rev/s
At exactly 3 rev/s the wheel seems frozen; slightly slower, it creeps backwards.
Preventing aliasing
The cure is to remove frequencies above before sampling or downsampling, with a low-pass (anti-aliasing) filter. Here a signal with a 12 Hz tone and a 45 Hz tone is decimated from 200 Hz to 50 Hz (Nyquist 25 Hz):
import numpy as np
fs = 200
t = np.arange(0, 2, 1 / fs)
x = np.sin(2 * np.pi * 12 * t) + np.sin(2 * np.pi * 45 * t)
def spectrum_peaks(y, rate):
A = 2 * np.abs(np.fft.rfft(y)) / y.size
f = np.fft.rfftfreq(y.size, 1 / rate)
return [(float(fi), round(float(a), 2)) for fi, a in zip(f, A) if a > 0.1]
naive = x[::4]
k = np.arange(-40, 41)
lowpass = np.sinc(k / 4) / 4 * np.hamming(k.size) # windowed-sinc low-pass, cutoff 25 Hz
filtered = np.convolve(x, lowpass, mode="same")[::4]
print("naive decimation: ", spectrum_peaks(naive, 50))
print("filter, then decimate:", spectrum_peaks(filtered, 50))naive decimation: [(5.0, 1.0), (12.0, 1.0)]
filter, then decimate: [(12.0, 1.0)]
Without the filter the 45 Hz tone appears as a fake 5 Hz tone. With it, only the genuine 12 Hz tone remains, at its full amplitude.
In software
scipy.signal.decimateandresample_polyfilter before downsampling; plain slicingx[::M]does not.- Image downscaling without blurring first produces moiré; graphics hardware uses mipmaps (pre-filtered smaller textures) to avoid it.
Exercises
Exercise 1: Find the alias
A 7 kHz tone is sampled at 8 kHz. What frequency do you hear on playback?
Answer
1 kHz.
Solution
kHz.
Exercise 2: Which tones collide?
At Hz, which tones between 0 and 3000 Hz alias to 100 Hz?
Answer
100, 900, 1100, 1900, 2100 and 2900 Hz.
Solution
for , keeping values in .
Exercise 3: Hear the fold (code)
Sweep a tone from 0 to 200 Hz in steps of 25 Hz with Hz and print the apparent frequency.
Answer
0, 25, 50, 25, 0, 25, 50, 25, 0: a zigzag.
Solution
fs = 100
print([abs(f - fs * round(f / fs)) for f in range(0, 201, 25)])[0, 25, 50, 25, 0, 25, 50, 25, 0]
The apparent frequency rises to the Nyquist frequency and then folds back down, like light reflecting between two mirrors at 0 and .
Test yourself
A 900 Hz tone sampled at 1000 Hz appears as…
- 900 Hz
- 100 Hz
- 1900 Hz
- 500 Hz
How do you prevent aliasing when downsampling?
- sample faster afterwards
- low-pass filter below the new Nyquist frequency first
- add noise
- use a larger FFT
Can aliasing be removed after sampling?
- yes, with a high-pass filter
- no, the alias and a real tone have identical samples
- yes, with zero padding
- yes, by resampling