TExcelTAD

Aliasing

When a signal contains frequencies above the Nyquist frequency fs/2f_s/2, sampling folds them back into the range [0,fs/2][0, f_s/2], 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 ff sampled at fsf_s produces exactly the same samples as one at f−kfsf - k f_s for any integer kk. The frequency you see is the one in [0,fs/2][0, f_s/2]:

falias=∣f−fs⋅round⁡(f/fs)∣f_\text{alias} = \left\lvert f - f_s \cdot \operatorname{round}(f / f_s) \right\rvert
python · try it yourself
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 rr revolutions per second, filmed at 24 frames per second, shows a spoke pattern repeating at 8r8r Hz. If 8r8r is just below 24, the pattern appears to turn slowly backwards:

python · try it yourself
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 fs/2f_s/2 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):

python · try it yourself
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.decimate and resample_poly filter before downsampling; plain slicing x[::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

∣7−8⋅round⁡(7/8)∣=∣7−8∣=1\lvert 7 - 8 \cdot \operatorname{round}(7/8) \rvert = \lvert 7 - 8 \rvert = 1 kHz.

Exercise 2: Which tones collide?

At fs=1000f_s = 1000 Hz, which tones between 0 and 3000 Hz alias to 100 Hz?

Answer

100, 900, 1100, 1900, 2100 and 2900 Hz.

Solution

f=k⋅1000±100f = k \cdot 1000 \pm 100 for k=0,1,2,3k = 0, 1, 2, 3, keeping values in [0,3000][0, 3000].

Exercise 3: Hear the fold (code)

Sweep a tone from 0 to 200 Hz in steps of 25 Hz with fs=100f_s = 100 Hz and print the apparent frequency.

Answer

0, 25, 50, 25, 0, 25, 50, 25, 0: a zigzag.

Solution
python · try it yourself
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 fs/2f_s/2.

Test yourself

A 900 Hz tone sampled at 1000 Hz appears as…

  1. 900 Hz
  2. 100 Hz
  3. 1900 Hz
  4. 500 Hz

How do you prevent aliasing when downsampling?

  1. sample faster afterwards
  2. low-pass filter below the new Nyquist frequency first
  3. add noise
  4. use a larger FFT

Can aliasing be removed after sampling?

  1. yes, with a high-pass filter
  2. no, the alias and a real tone have identical samples
  3. yes, with zero padding
  4. yes, by resampling