Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 38 ยท Getting Started

Sampling and Aliasing

Understand how sampling turns a continuous signal into numbers, what the Nyquist rate is, and why too slow a sampling rate creates aliases. Includes Python demos.

Sampling means measuring a continuous signal at equally spaced moments. It looks harmless, but between two measurements the signal is unseen, so an important question follows: can we rebuild the original signal from the samples alone? The answer is "yes, if you sample fast enough", and what "fast enough" means is the sampling theorem. When you break the rule, a fast wave disguises itself as a slower one, called an alias.

Sampling a sine wave

A continuous sine of frequency f is x(t) = sin(2*pi*f*t). Sampling at rate fs evaluates it at t = n/fs, giving the discrete sequence x[n] = sin(2*pi*(f/fs)*n). The ratio f/fs is the normalized frequency in cycles per sample. It alone decides what the sequence looks like.

Python Sampling at different rates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

At fs = 50 each cycle is described by 10 points. At fs = 10 there are exactly 2 per cycle and the samples all land on zero crossings, so the wave vanishes completely. At fs = 6 there are fewer than 2 per cycle: the numbers still look like a wave, but a slower one that is not the original.

The sampling theorem

A signal that contains no frequencies above B hertz can be rebuilt perfectly from its samples if fs > 2B.

The limit fs / 2 is the Nyquist frequency; twice the highest frequency, 2B, is the Nyquist rate. Everything in a digital signal lives below the Nyquist frequency. Sampling at exactly 2B is not enough in general (a sine sampled exactly at its zero crossings looks like zero), so the inequality is strict.

Typical rates: telephone speech 8 kHz (voice up to 4 kHz), CD audio 44.1 kHz (people hear up to about 20 kHz), professional audio 48 kHz or 96 kHz.

Aliasing

If a component is above the Nyquist frequency, its samples are indistinguishable from the samples of a lower frequency. A wave of frequency f sampled at fs appears at

f_alias = |f - k * fs| for the integer k that brings it into 0 ... fs/2.

flowchart LR
    A["true tone<br/>f = 9 Hz"] --> S["sampler<br/>fs = 10 Hz"]
    S --> B["samples look like<br/>a 1 Hz tone"]

A 9 Hz tone sampled at 10 Hz gives the same numbers as a 1 Hz tone (with flipped sign).

Python Two different sines, identical samples

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The function alias_frequency first wraps the frequency into one sampling period, then folds it about fs/2. Fast wheels that look like they turn backwards in a film (the "wagon-wheel effect") are the same phenomenon: the frame rate is fs.

Seeing aliasing in the spectrum

A discrete spectrum only spans 0 ... fs/2, so a tone that is too high lands at the folded position. Use the FFT (explained in DFT from Scratch) to see it.

Python Where does a 35 Hz tone appear?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Once fs drops below 70 Hz (twice 35), the reported peak is no longer 35. At fs = 60 it appears at 25, at fs = 40 at 5, and at fs = 30 at 5.

Preventing aliasing

You cannot fix aliasing after sampling, because the original information is gone. You prevent it before the ADC:

  1. Sample fast enough for the highest frequency you care about.
  2. Add an analog anti-alias low-pass filter that removes everything above fs/2 before the sampler. Real filters are not brick walls, so engineers sample somewhat above 2B (CD audio: 22.05 kHz Nyquist for 20 kHz hearing).
  3. Oversample and then reduce the rate digitally: the analog filter becomes cheap and the digital filter does the precise work (the multirate chapter covers decimation).

Reconstruction in one idea

The theorem says the original can be recovered with sinc interpolation: place a scaled sinc(t/T) pulse at each sample and add them. The next demo does it for a 3 Hz wave.

Python Rebuilding a wave from samples

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The error is small but not zero because the infinite sum was cut to 61 samples. With a longer record it keeps shrinking.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A signal contains frequencies up to 3 kHz. What is the smallest sampling rate that satisfies the sampling theorem?

    The rate must exceed twice the highest frequency: more than 6 kHz.
  2. 2. A 70 Hz tone is sampled at 100 Hz. At which frequency does it appear?

    70 is above the Nyquist frequency 50, so it folds to 100 - 70 = 30 Hz.
  3. 3. Where must the anti-alias filter be placed?

    After sampling, aliases are mixed with real content and cannot be told apart, so the signal must be band-limited before sampling.
  4. 4. What is the Nyquist frequency?

    It is fs/2, the highest frequency a sampled signal can represent.

Summary

  • Sampling at rate fs evaluates a signal at t = n/fs; the normalized frequency f/fs governs the samples.
  • To avoid aliasing, fs must be greater than twice the highest frequency present.
  • Frequencies above fs/2 fold back: f_alias = |f - k*fs|.
  • Prevent aliasing with an analog low-pass filter before the sampler.
  • A band-limited signal can be rebuilt from its samples with sinc interpolation.

Next: Your DSP Toolkit in Python