Sampling rate and aliasing
What happens between samples: the Nyquist limit, how fast signals masquerade as slow ones, and how to choose a sample rate and an anti-aliasing filter.
Lesson 4 of 6 · 30 min · Self-paced · Free
A program only sees snapshots
A microcontroller does not watch a sensor continuously. It takes a sample at regular intervals and knows nothing about what happens in between. The number of samples per second is the sample rate, fs, measured in hertz (Hz).
This raises a practical question for every sensor in a robot: how often should you read it? Too slowly, and you miss events or, worse, see things that are not there. Too quickly, and you waste processor time, power and memory. This lesson gives you the tools to choose.
The wagon-wheel effect
You may have seen a film where a car's wheels appear to spin slowly backwards while the car speeds forwards. The camera samples at a fixed frame rate. If a wheel spoke moves almost a full spoke-spacing between frames, it looks as if it moved a tiny bit backwards. A fast motion has disguised itself as a slow one.
The same thing happens to sensor signals. It is called aliasing, because a fast signal appears under a false identity, an alias.
Seeing an alias in numbers
Sample a 9 Hz signal at 10 Hz, and compare it with a 1 Hz signal sampled at the same moments:
import math
fs = 10.0 # samples per second
for n in range(6):
t = n / fs
fast = math.cos(2 * math.pi * 9 * t) # a 9 Hz signal
slow = math.cos(2 * math.pi * 1 * t) # a 1 Hz signal
print(f"t = {t:.1f} s 9 Hz: {fast:+.3f} 1 Hz: {slow:+.3f}")Output:
t = 0.0 s 9 Hz: +1.000 1 Hz: +1.000
t = 0.1 s 9 Hz: +0.809 1 Hz: +0.809
t = 0.2 s 9 Hz: +0.309 1 Hz: +0.309
t = 0.3 s 9 Hz: -0.309 1 Hz: -0.309
t = 0.4 s 9 Hz: -0.809 1 Hz: -0.809
t = 0.5 s 9 Hz: -1.000 1 Hz: -1.000The samples are identical. Once sampled, there is no way to tell them apart: the information that would distinguish them is simply gone. No clever software afterwards can undo this, which is the most important point in this lesson.
The Nyquist limit
The rule that tells you when this happens is usually called the Nyquist–Shannon sampling theorem. In plain words:
To capture a signal faithfully, you must sample at more than twice its highest frequency.
Half the sample rate, fs / 2, is called the Nyquist frequency. Anything in the signal below it is captured correctly. Anything above it folds back and appears as a lower frequency. For a single sine wave of frequency f, the apparent frequency is the distance from f to the nearest whole multiple of fs:
def alias_frequency(f, fs):
"""Frequency that a sine of f Hz appears to have when sampled at fs Hz."""
k = round(f / fs) # nearest whole multiple of fs
return abs(f - k * fs)
for f, fs in [(9, 10), (3, 10), (50, 40), (60, 50), (100, 1000)]:
print(f"{f:4d} Hz sampled at {fs:4d} Hz looks like {alias_frequency(f, fs):g} Hz")Output:
9 Hz sampled at 10 Hz looks like 1 Hz
3 Hz sampled at 10 Hz looks like 3 Hz
50 Hz sampled at 40 Hz looks like 10 Hz
60 Hz sampled at 50 Hz looks like 10 Hz
100 Hz sampled at 1000 Hz looks like 100 HzThe 3 Hz and 100 Hz signals are below their Nyquist frequencies (5 Hz and 500 Hz) and come through honestly. The others do not.
A realistic trap: mains hum
Mains electricity alternates at 50 Hz or 60 Hz depending on the country, and long sensor wires near mains cables or motors can pick up a little of it. Suppose you read a temperature sensor at 40 Hz in a building with 50 Hz mains. The hum folds down to 10 Hz, and your temperature appears to wobble ten times a second. Worse, a hum frequency very close to a multiple of your sample rate produces an alias of a fraction of a hertz, which looks just like a slow, real change in temperature. An alias does not look like noise; it looks like data.
Preventing aliasing
There are two defences, and good designs use both.
- Sample fast enough for the frequencies you care about.
- Remove what you do not care about before sampling, using an anti-aliasing filter: a low-pass filter in the analogue circuit, in front of the ADC. The simplest is a resistor and capacitor (an RC filter), with cutoff frequency:
fc = 1 / (2π · R · C)With R = 10 kΩ and C = 1 µF, fc = 1 / (2π × 10,000 × 0.000001) ≈ 15.9 Hz. Signals well above that are strongly reduced before the ADC ever sees them. A single RC stage rolls off gently, so it reduces rather than eliminates high frequencies; demanding applications use steeper filters. Note that a digital filter in firmware, like the moving average, cannot fix aliasing, because by the time the data reaches it the damage is done.
Choosing R also matters for the ADC: many ADCs need a low enough source resistance to charge their internal sampling capacitor in time. Check your microcontroller's datasheet for its recommended maximum.
Choosing a sample rate
Nyquist gives a hard minimum, but practical systems sample well above it.
- For control loops, a common rule of thumb is to sample at least 10 times faster than the fastest behaviour you are trying to control. A joint that settles in about 0.1 s has important behaviour up to roughly 10 Hz, suggesting a loop rate of 100 Hz or more.
- For monitoring slow quantities like battery voltage or temperature, a few samples per second may be plenty, as long as an anti-aliasing filter blocks faster interference.
- For signals with sharp events, like a bump switch or a force spike when a gripper touches an object, sample fast enough that the event lasts several samples, or use an interrupt.
- Oversampling and averaging: sampling faster than needed and averaging groups of samples reduces noise, and is a cheap trick on microcontrollers with spare ADC time.
Always check the cost side too: every sample uses processor time and, if logged, storage. At 1 kHz, a 2-byte reading fills 2 KB per second, which is over 7 MB per hour.
Recap
A sampled signal only contains information below half the sample rate. Anything faster folds down and impersonates a slower signal, and no software can separate the two afterwards. Prevent aliasing by sampling comfortably fast and by low-pass filtering in hardware before the ADC. Choose rates from the speed of the behaviour you care about, typically ten times faster for control, and remember that a digital filter cannot undo aliasing.
Project
Python project: an alias hunter
- Extend the alias example: sample a 7 Hz cosine at fs = 8, 10, 14, 15 and 50 Hz. For each, print the first 10 samples and predict the alias frequency with
alias_frequencybefore you run it. - For fs = 10 Hz, print the apparent frequency of every whole-number input frequency from 0 to 30 Hz. Describe the zig-zag pattern you see, and explain where it turns around.
- Calculate the RC cutoff frequency for three resistor and capacitor combinations of your choice, and say which would be a sensible anti-aliasing filter for a sensor read at 100 Hz.
This is a paper-and-Python activity. No hardware is required.
Deliverables
- Python output with your predictions alongside
- A short description of the folding pattern for fs = 10 Hz
- Three RC cutoff calculations with a recommendation
Adult supervision requiredThis is a calculation-only activity. If you go on to build an RC filter on a breadboard, use only low-voltage battery power, check component ratings, and work with an adult supervising.
Knowledge check
Check your understanding
Go further
- Nyquist–Shannon sampling theorem — Wikipedia ↗
The theorem stated precisely, with its history and assumptions.
- Aliasing — Wikipedia ↗
Visual examples of aliasing in signals and images, including the folding of frequencies.
- Anti-aliasing filter — Wikipedia ↗
Why filters go before the converter, and how steep they need to be.
Lesson materials · free to download
Sign in to track your progress. Reading is free without an account.