TactivergeLite

Lite / Textbooks / Signals and Systems / Chapter 2

2. Sampling and the Nyquist rate

Version 1 · Technically checked (automated checks only) · 4 source lessons · Full interactive version

Introduction

A computer cannot store a smooth curve; it stores a list of numbers taken at regular instants. How often is often enough? Sample a sinusoid more than twice per cycle and its frequency can be recovered; sample less and the information is gone. That threshold, twice the highest frequency present, is the Nyquist rate. Engineers sample well above it, because real filters and real signals are never perfect.

Figure 2.1. Five samples per cycle (described in words)

A 10 Hz sine (dashed) sampled at 50 Hz, shown as stems and dots: 5 samples per cycle. The Nyquist rate for a 10 Hz signal is 20 Hz, and a 50 Hz sampler can represent frequencies up to 25 Hz.

Explanation

Built from 4 School lessons. Each part is the lesson's own explanation.

2.1 Sampling: how often is often enough?

In films, a fast car's wheels sometimes seem to spin backwards. What has that got to do with a temperature sensor?

Rate and period

The sample rate fs is samples per second (Hz); the sample period is the time between samples, 1 ÷ fs. A greenhouse logger sampling at 0.1 Hz takes one reading every 10 s; an audio recorder at 44,100 Hz takes one every 22.7 µs. Samples recorded = rate × duration: 50 Hz for 60 s gives 3,000 samples.

Samples per cycle

Divide the sample rate by the signal frequency to get samples per cycle. Ten or more show the wave's shape clearly. Between two and about five catch the frequency but the shape looks rough. Fewer than two per cycle cannot represent the wave at all. So a signal with frequency f needs a sample rate of more than 2f: half the sample rate is the highest frequency you can capture.

Aliasing and how to avoid it

When a signal is too fast for the sample rate, its samples line up into a slower, false wave: an alias. A 9 Hz wiggle sampled at 10 Hz pretends to be 1 Hz, and no maths afterwards can tell the difference. Engineers prevent it by sampling fast enough for every frequency present and by filtering out faster wiggles (like mains hum) before the ADC.

Common mix-ups

Open the Lite lesson: Sampling: how often is often enough?

2.2 Sampling and the Nyquist rate

A computer can't watch a signal continuously; it takes snapshots. How many snapshots a second are enough to catch a 50 Hz hum?

Samples, not signals

A sampler measures the signal every Tₛ = 1/fₛ seconds, where fₛ is the sample rate in hertz. Between samples the computer knows nothing. Any two signals that agree at every sample instant are indistinguishable afterwards, however different they were in between.

The Nyquist rate

To capture a sine wave of frequency f, you need more than two samples per cycle: fₛ > 2f. For a signal whose highest frequency is f_max, the Nyquist rate is 2·f_max. Equivalently, a given fₛ can faithfully represent frequencies only up to fₛ/2, called the Nyquist frequency.

Margins in practice

Exactly 2f is a knife edge: sampled at the wrong phase, a wave right at the limit can vanish. Engineers sample with a comfortable margin, often 5–10 times the highest frequency of interest for control and monitoring, and use an analogue filter to remove anything above fₛ/2 before sampling.

Common mix-ups

Open the Lite lesson: Sampling and the Nyquist rate

2.3 From a wave to a list of numbers

Your phone's microphone hears a smooth, endless pressure wave. Your phone stores a list of whole numbers. What happened in between?

Two independent choices: time and value

A continuous-time signal x(t) has a value at every instant. A discrete-time signal x[n] exists only at the sample instants. Separately, a value can be analog (any real number) or quantised (one of a fixed set of levels). A digital signal is both discrete in time and quantised in value. The two steps are done by one chip, an ADC, but they cause different errors: sampling can cause aliasing, quantising adds rounding noise.

x[n] = x(nT)

Uniform sampling takes one value every T seconds; the sample rate is fs = 1/T. Sample n is the value at t = nT. The list x[0], x[1], x[2], … is all a processor ever sees. Square brackets mean an integer index; round brackets mean real time. Mixing them up is the commonest source of off-by-a-factor bugs in signal code.

Energy and power

For a sequence, energy E = Σ x[n]², the sum of squared samples (for a voltage across 1 Ω this is proportional to the energy delivered). Signals that go on for ever, like a sine wave, have infinite energy, so we use average power P = mean of x². A sinusoid of amplitude A has P = A²/2, and its RMS value √P = A/√2 is the steady voltage that would deliver the same power.

Common mix-ups

Open the Lite lesson: From a wave to a list of numbers

2.4 How fast is fast enough?

Film a spinning wheel and it can appear to turn slowly backwards. Is the camera lying, or are you?

The sampling theorem

If a signal contains no frequencies at or above B, sampling at fs > 2B keeps all its information: the original can be rebuilt exactly from the samples (by ideal interpolation). fs/2 is called the Nyquist frequency; 2B is the Nyquist rate. Below that, information is lost and, worse, disguised.

Why twice?

To capture a sinusoid you need at least a high sample and a low sample in every cycle, more than two per period. At exactly two samples per cycle you might hit only the zero crossings and see nothing at all. That is why the inequality is strict.

Real systems add margin

CD audio samples at 44.1 kHz to cover hearing up to about 20 kHz: Nyquist is 22.05 kHz, leaving room for an anti-aliasing filter to roll off. Control loops often sample 10–20 times faster than the dynamics they control, because they need the signal's shape, not just its frequency.

Common mix-ups

Open the Lite lesson: How fast is fast enough?

Worked examples

Sampling: how often is often enough?: example 1

You want to measure 50 Hz mains hum on a sensor line. What is the slowest sample rate that could possibly capture it, and what would you actually choose?

Solution

More than 2 × 50 = 100 Hz is the bare minimum. To see the shape you might choose 1,000 Hz: 20 samples per cycle.

Sampling: how often is often enough?: example 2

A logger samples at 250 Hz for 30 s. How many samples?

Solution

250 × 30 = 7,500 samples.

Sampling and the Nyquist rate: example 1

A wheel encoder produces pulses at up to 300 Hz. What is the Nyquist rate, and what would you choose?

Solution

Nyquist rate = 2 × 300 = 600 Hz. A sensible choice is around 3–6 kHz so each pulse is seen several times and timing is precise.

From a wave to a list of numbers: example 1

x(t) = 3·cos(2π·2·t) is sampled at fs = 8 Hz. List x[0] to x[3].

Solution

f/fs = 1/4, so each sample advances the phase by 90°. x[0] = 3cos 0 = 3, x[1] = 3cos 90° = 0, x[2] = 3cos 180° = −3, x[3] = 3cos 270° = 0.

From a wave to a list of numbers: example 2

What is the energy of x = [1, 2, −1, 0]?

Solution

E = 1² + 2² + (−1)² + 0² = 6. Squaring makes negative samples count just as much as positive ones.

How fast is fast enough?: example 1

An accelerometer measures vibrations up to 400 Hz. Which of 500 Hz, 1 kHz or 2 kHz would you choose?

Solution

500 Hz fails (Nyquist 250 Hz). 1 kHz just works in theory (Nyquist 500 Hz) but leaves little room for a filter. 2 kHz gives a comfortable margin: choose it, unless storage or power rules it out.

Interactive figures, in words

Figure 2.2 from Sampling: how often is often enough?

Set the signal to 9 Hz and the sample rate to 10 Hz. Untick the real signal: what do the dots show? Now raise the sample rate until the readout stops warning you. How many samples per cycle did that take?

In the signal lab, a 9 Hz wave sampled 10 times a second gives dots that trace a slow 1 Hz wave: the readout warns that there are fewer than 2 samples per cycle. Raising the sample rate above 18 Hz removes the warning, and around 40–60 Hz the dots clearly follow the real wave's shape.

Figure 2.3 from How fast is fast enough?

Keep fs = 20 Hz. Raise f from 1 Hz slowly. At what f does the dashed 'what the samples show' curve stop matching the blue signal? Then set f = 20 Hz exactly and explain what you see.

In the sampler at fs = 20 Hz, the samples follow the signal faithfully until f reaches 10 Hz (the Nyquist frequency). Above that, the dots trace a slower false wave: at 12 Hz they look like 8 Hz. At exactly 20 Hz every sample has the same value, so the alias is 0 Hz.

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

Practice

Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.

  1. A signal is sampled at fs = 1 kHz from t = 0. At what time, in seconds, is sample x[250] taken?
    Answer it in “From a wave to a list of numbers”
  2. You sample a 1 Hz signal at 10 Hz. How many samples land in each cycle of the signal?
    Answer it in “Sampling: how often is often enough?”
  3. A sensor is sampled at 200 Hz. How long is the gap between samples, in milliseconds?
    Answer it in “Sampling: how often is often enough?”
  4. A data logger samples at 250 Hz. What is the highest frequency it can represent unambiguously (the Nyquist frequency), in Hz?
    Answer it in “Sampling and the Nyquist rate”
  5. A 40 Hz vibration is sampled at 1 kHz. How many samples fall in each cycle?
    Answer it in “Sampling and the Nyquist rate”
  6. A signal's highest frequency is 300 Hz. Which sample rate is the smallest of these that works?
    Answer it in “How fast is fast enough?”
  7. A control loop's fastest dynamics are at 8 Hz and the rule of thumb is to sample 20 times faster. What sample rate, in Hz?
    Answer it in “How fast is fast enough?”

Engineering applications

Audio

Human hearing tops out around 20 kHz, so CD audio samples at 44.1 kHz: a little over twice the highest frequency, with room for the filter that removes anything above it.

Battery versus bandwidth

Every sample costs energy and storage. A sensor node samples only as fast as its fastest interesting signal requires, with a margin, rather than as fast as it can.

Watch (optional)

Streaming video uses a lot of data. Everything you need is in the text.

Summary

Glossary

Sampling and aliasing
Sampling measures a signal at regular intervals. Any frequency above half the sample rate, the Nyquist frequency, is indistinguishable from a lower 'alias' frequency. Field Guide
Frequency and period
Frequency is how many cycles happen per second; the period is the time for one cycle. Each is the reciprocal of the other. Field Guide
ADC resolution
An n-bit analogue-to-digital converter splits its reference voltage into 2^n steps; one step, the least significant bit, is the smallest change it can detect. Field Guide

Project: Choose a sample rate

Pick three signals a robot might measure, such as wheel speed, motor current and a microphone. Estimate the highest frequency each contains, choose a sample rate with a stated margin, and calculate the data rate each produces in bytes per second.

Deliverables

Write it up on paper or in your own notes. Lite never stores what you write.

References

← 1. Signals and noise3. Aliasing: when samples lie →