DSPRelated.com

Sampling

From continuous to discrete

The real world is continuous. Sound pressure changes smoothly over time. Light intensity varies continuously across space. Voltage on a wire fluctuates without jumps.

But computers are discrete. They can only store numbers, a finite set of values at specific moments in time. To process a signal digitally, we need to capture snapshots: measure the signal's value at regular intervals and store each measurement as a number.

This process is called sampling, and it raises an immediate question: how often do you need to sample to capture all the information in the signal?

Taking Samples

Sampling means measuring a continuous signal x(t) at evenly spaced points in time:

x[n] = x(n · Ts)     for n = 0, 1, 2, 3, …

  • fs = sample rate, how many samples per second (in Hz)
  • Ts = 1/fs = sample period, the time between consecutive samples
  • x[n] = the n-th sample, a discrete sequence of numbers
Try it: The gray curve is the continuous signal. The blue dots are the samples taken at rate fs. The green curve is what you'd reconstruct from those samples alone. With a high sample rate, the reconstruction is perfect. Lower the sample rate and watch: at some point, the reconstruction starts to go wrong.

The Nyquist-Shannon Sampling Theorem

In 1949, Claude Shannon proved one of the most important results in information theory:

A signal containing frequencies up to fmax Hz can be perfectly reconstructed from its samples if and only if the sample rate is greater than 2·fmax.

fs > 2 · fmax

The critical threshold 2·fmax is called the Nyquist rate. Sample faster than this, and you lose nothing. Sample slower, and information is permanently destroyed.

Application Max freq Nyquist rate Typical fs
Telephone 3.4 kHz 6.8 kHz 8 kHz
CD audio 20 kHz 40 kHz 44.1 kHz
Professional audio 20 kHz 40 kHz 48 kHz or 96 kHz
Ultrasound imaging 15 MHz 30 MHz 40–60 MHz

Notice that the actual sample rate is always a bit higher than the Nyquist rate. This margin provides a practical safety buffer.

Key Insight: The sampling theorem is remarkable because it's exact. You don't lose "a little" information. Above the Nyquist rate, you lose none. The continuous signal can be reconstructed perfectly from the samples alone. Below the Nyquist rate, the loss is catastrophic and irreversible.

Aliasing: When Sampling Goes Wrong

What happens when you sample too slowly? The high-frequency signal masquerades as a lower-frequency signal. This is called aliasing, where the signal takes on a false identity.

Think of a wagon wheel in a movie: when the camera's frame rate is too low, a wheel spinning forward can appear to spin backward. The high rotation frequency aliases to a lower (and reversed) frequency because of insufficient sampling.

Try it: Start with f = 3 Hz and fs = 10 Hz (well above Nyquist). Now slowly increase f past 5 Hz (the Nyquist frequency = fs/2). Watch the red alias frequency appear, a lower-frequency wave that passes through exactly the same sample points. At f = 7 Hz, the alias is 3 Hz. At f = 9 Hz, the alias is 1 Hz. At f = 10 Hz (= fs), the alias is 0 Hz, meaning all samples are identical!
Key Insight: Aliasing is permanent and irreversible. Once the samples are taken, there is no way to tell whether they came from the original frequency or its alias. This is why every real ADC (analog-to-digital converter) has an anti-aliasing filter, a low-pass filter that removes all frequencies above fs/2 before sampling.

Where Does the Alias Land?

There's a simple rule for computing the alias frequency. A signal at frequency f, sampled at rate fs, appears at:

falias = |f − round(f / fs) · fs|

More intuitively: the alias frequency is how far f is from the nearest integer multiple of fs. All frequencies "fold" into the range [0, fs/2], bouncing back and forth like a ball between the floor and ceiling.

Try it: This diagram shows how input frequencies (horizontal axis) map to apparent frequencies after sampling (vertical axis). Everything above fs/2 folds back down. Frequencies at exact multiples of fs/2 pile up at 0 or fs/2. The pattern repeats forever, the same zigzag, tiled across the frequency axis.

Why This Matters

Sampling is where the analog and digital worlds meet, and getting it wrong has real consequences:

  • Audio recording: A mic picks up frequencies above 22 kHz (inaudible). Without an anti-aliasing filter, these alias into the audible range and create harsh, unnatural artifacts.
  • Sensor systems: A vibration sensor on a motor must sample faster than twice the highest vibration frequency, or real faults will alias into frequency bins where they look like something else entirely.
  • Software-defined radio: Intentional aliasing (called undersampling or bandpass sampling) is sometimes used to shift a high-frequency signal down to a lower frequency, using aliasing as a feature, not a bug.
  • Video: Camera frame rates that are too low create the wagon-wheel effect (temporal aliasing). Spatial aliasing causes moiré patterns on fine textures like brick walls or striped shirts.
Key Insight: The Nyquist theorem tells you how fast to sample to avoid aliasing. The anti-aliasing filter protects you from the consequences of the frequencies you can't capture. Get these two things right, and you will have a digital signal that accurately represents the original.

Frequently Asked Questions

What is the Nyquist theorem?

The Nyquist-Shannon sampling theorem states that to faithfully capture a signal, you must sample at more than twice its highest frequency: fs > 2·fmax. The frequency fs/2 is called the Nyquist frequency. Below this rate, high-frequency content folds back into lower frequencies (aliasing) and the original signal cannot be recovered.

What is aliasing?

Aliasing occurs when a signal is sampled too slowly. Frequencies above the Nyquist limit masquerade as lower frequencies in the sampled data. A classic example: a 12 kHz tone sampled at 10 kHz appears as a 2 kHz tone. The samples are identical for both frequencies, so the original cannot be recovered. Anti-aliasing filters prevent this by removing high frequencies before sampling.

Why was CD audio set to 44.1 kHz?

Human hearing extends to roughly 20 kHz. The Nyquist theorem requires sampling above 40 kHz. The 44.1 kHz rate provides a small guard band above 40 kHz for the anti-aliasing filter to roll off, while keeping the data rate manageable. The specific number 44,100 was also chosen for compatibility with video equipment used during early digital recording.

Quick Check

Test your understanding of the key concepts from this lesson.