Sampling Theorem Explorer
Copy this iframe to your website:
1 | |
Description
The sampling theorem says that a signal whose highest frequency is \(f_{max}\) can be rebuilt exactly from its samples when the sampling rate satisfies \(f_s > 2f_{max}\). This MicroSim uses a single sine wave, \(x(t) = \sin(2\pi f_{max} t)\), so \(f_{max}\) is simply the frequency of the tone. Two sliders set \(f_{max}\) (1 to 100 Hz) and \(f_s\) (1 to 200 Hz). A menu chooses how the signal is rebuilt from its samples: ideal sinc interpolation, a zero-order hold or linear interpolation. A checkbox draws the individual sinc pulses of the ideal reconstruction.
Four plots are linked, from top to bottom: the continuous signal, its samples, the reconstructed signal (with the original dashed for comparison) and the spectrum from 0 to 200 Hz. A banner under the title states whether the sampling rate is high enough or the tone is aliased, and to which frequency. Moving a slider changes all four plots at once, so you can watch a spectral copy of the tone cross the Nyquist frequency while the reconstruction changes frequency.
This simulation is used in Chapter 5: Sampling and Quantization.
The four plots
- Continuous signal \(x(t)\) over 200 ms. The small ticks along the bottom edge mark the sampling instants.
- Samples \(x[n] = x(nT_s)\) with \(T_s = 1/f_s\).
- Reconstructed signal, with the original shown dashed.
- Spectrum from 0 to 200 Hz. Sampling copies the tone to every frequency \(k f_s \pm f_{max}\) (grey lines). The shaded band from 0 to the Nyquist frequency \(f_{Nyq} = f_s/2\) is the range an ideal reconstruction filter keeps. A line's height is its amplitude relative to the original tone.
The banner under the title states which case you are in:
| Condition | Result |
|---|---|
| \(f_s > 2f_{max}\) (green) | The tone is the only spectral line below \(f_{Nyq}\), and the reconstruction equals the original |
| \(f_s < 2f_{max}\) (red) | A copy at \(\lvert f_{max} - k f_s\rvert\) falls below \(f_{Nyq}\). The reconstruction is a sine at that alias frequency |
| \(f_s = 2f_{max}\) (red) | Every sample of this sine falls on a zero crossing, so nothing is recovered. This is why the inequality is strict |
The alias frequency is \(f_a = \lvert f_{max} - k f_s\rvert\) with \(k\) the integer nearest to \(f_{max}/f_s\), which always lies in \([0, f_s/2]\). For example, a 40 Hz tone sampled at 50 Hz appears at \(\lvert 40 - 50\rvert = 10\) Hz. Because \(40 - 50\) is negative, the 10 Hz alias is drawn upside down, as \(-\sin(2\pi \cdot 10\, t)\).
Reconstruction filters
The ideal filter is sinc interpolation,
whose frequency response is 1 below \(f_{Nyq}\) and 0 above it. Tick Show sinc pulses to see the individual terms of this sum drawn over their total. For a sampled sinusoid the infinite sum is a sinusoid at the folded frequency, and that closed form is what the thick curve shows. Two practical alternatives are offered:
- Zero-order hold, the staircase output of a typical DAC, with gain \(\lvert\text{sinc}(f/f_s)\rvert\).
- Linear interpolation, with gain \(\text{sinc}^2(f/f_s)\).
Neither practical filter removes the copies above \(f_{Nyq}\) completely. The orange lines in the spectrum show how much of each copy leaks through, which is why a DAC is followed by an analog smoothing filter.
Controls
- Signal frequency fmax sets the tone frequency from 1 to 100 Hz (default 20).
- Sampling rate fs sets the sampling rate from 1 to 200 Hz (default 50).
- Reconstruction chooses ideal sinc interpolation, zero-order hold or linear interpolation.
- Show sinc pulses is available only with ideal reconstruction.
Lesson Plan
Learning Objectives
By the end of this activity, students will be able to:
- State the sampling theorem and the meaning of the Nyquist rate and the Nyquist frequency.
- Predict the alias frequency of an under-sampled tone.
- Explain aliasing as the overlap of spectral copies created by sampling.
- Compare ideal sinc reconstruction with zero-order hold and linear interpolation.
- Relate the heights of the spectral lines under a practical reconstruction to \(\lvert\text{sinc}(f/f_s)\rvert\) and \(\text{sinc}^2(f/f_s)\).
Target Audience
College undergraduates in a signals and systems or introductory digital signal processing course. Plan 20 to 25 minutes.
Prerequisites
- Sinusoids, frequency and period
- The idea of a frequency spectrum
- Discrete-time signals \(x[n]\)
Activities
Activity 1: Lower the sampling rate (5 min). Keep \(f_{max} = 20\) Hz with the ideal reconstruction. Set \(f_s\) to 100, 45, 40, 35, 30 and 25 Hz in turn, and record the banner and the frequency of the reconstructed signal.
- At 100 Hz and at 45 Hz the banner is green and the reconstruction is a 20 Hz sine.
- At 40 Hz the banner is red and every sample is zero: the reconstruction is a flat line.
- At 35, 30 and 25 Hz the banner reports aliasing and the reconstruction is a sine at 15, 10 and 5 Hz.
Activity 2: Predict, then check (10 min).
- Predict, then check, the alias frequency for these pairs (\(f_{max}\), \(f_s\)): (40, 50), (70, 50), (30, 50), (100, 30). The answers are 10, 20, 20 and 10 Hz.
- Set the tone to 40 Hz and the rate to 50 Hz. In the samples plot, trace the dots by eye. Which sine do they suggest? Two different tones produce exactly these samples: the 40 Hz tone and a 10 Hz sine drawn upside down.
- Now set \(f_{max} = 10\) Hz and keep \(f_s = 50\) Hz. The dots are the mirror image of the previous ones: the first dot after \(t = 0\) is \(+0.95\) instead of \(-0.95\). A 40 Hz tone sampled at 50 Hz is indistinguishable from \(-\sin(2\pi \cdot 10\,t)\).
Activity 3: Practical reconstruction (10 min). Set \(f_{max} = 20\) Hz and \(f_s = 60\) Hz.
- With the zero-order hold, read the heights of the coloured lines at 20, 40, 80 and 100 Hz: about 0.83, 0.41, 0.21 and 0.17. Check them against \(\lvert\text{sinc}(f/f_s)\rvert\), for example \(\lvert\text{sinc}(1/3)\rvert = 0.83\).
- Switch to linear interpolation. The heights become about 0.68, 0.17, 0.04 and 0.03. The unwanted copies shrink much more, but the wanted 20 Hz tone is attenuated more as well.
- Which reconstruction gives the best compromise between removing the copies and keeping the tone?
Activity 4: The sinc pulses (5 min). Choose the ideal reconstruction, set \(f_{max} = 10\) Hz and \(f_s = 40\) Hz and tick Show sinc pulses. Each non-zero sample contributes one pulse that is zero at every other sampling instant. At each sampling instant, how many pulses are non-zero? Then discuss: audio CDs use \(f_s = 44.1\) kHz for signals up to 20 kHz. Why not exactly 40 kHz? What must be placed before the sampler to guarantee that \(f_{max} < f_s/2\)?
Assessment
- A 70 Hz sine is sampled at 50 Hz. At what frequency does the reconstructed signal appear? Answer: 20 Hz. With \(k = 1\) the alias is \(\lvert 70 - 50\rvert = 20\) Hz, which is below \(f_s/2 = 25\) Hz.
- A 100 Hz sine is sampled at 30 Hz. What is the alias frequency? Answer: 10 Hz. The nearest multiple of \(f_s\) is \(k = 3\), so \(f_a = \lvert 100 - 90\rvert = 10\) Hz.
- Why is sampling at exactly \(2f_{max}\) not sufficient? Answer: for this zero-phase sine every sample is \(\sin(\pi n) = 0\), so the samples carry no information about the tone. The condition must be \(f_s > 2f_{max}\).
- With a zero-order hold, \(f_{max} = 20\) Hz and \(f_s = 60\) Hz, why is the 20 Hz line only 0.83 high, and what does the 0.41 line at 40 Hz represent? Answer: the hold has gain \(\lvert\text{sinc}(f/f_s)\rvert\), which is 0.83 at 20 Hz. The 40 Hz line is the spectral copy at \(f_s - f_{max}\) leaking through the hold at gain 0.41.
- Why do audio CDs use 44.1 kHz for a 20 kHz signal bandwidth instead of 40 kHz? Answer: a real anti-aliasing filter needs a transition band between 20 kHz and \(f_s/2 = 22.05\) kHz. At exactly 40 kHz it would have to be an ideal brick-wall filter.
Common Misconceptions
"Sampling at exactly twice the highest frequency always works." Set \(f_{max} = 25\) Hz and \(f_s = 50\) Hz. The banner turns red, every sample lies on a zero crossing and the reconstruction is zero.
"An under-sampled tone just disappears or turns into noise." It becomes a clean sine at another frequency. Set 40 Hz and 50 Hz: the reconstruction is a 10 Hz sine, and the grey copy at 10 Hz lies inside the shaded band.
"Aliasing can be removed by filtering after sampling." The samples of the 40 Hz tone and of the inverted 10 Hz sine are identical (Activity 2), so nothing done to the samples can tell them apart. The tone must be removed before the sampler.
References
- Nyquist–Shannon sampling theorem - Wikipedia - statement of the theorem, the reconstruction formula and the aliasing case
- Aliasing - Wikipedia - how a tone above half the sampling rate folds to a lower frequency
- Zero-order hold - Wikipedia - the staircase reconstruction and its sinc-shaped frequency response
- Sinc function - Wikipedia - the normalized sinc used by the interpolation formula
- The Scientist and Engineer's Guide to Digital Signal Processing - Steven W. Smith - free online textbook with a chapter on ADC, DAC and the sampling theorem
- Shannon, C. E. (1949). Communication in the presence of noise. Proceedings of the IRE, 37(1), 10-21. - the original statement of the sampling theorem
- Oppenheim, A. V., & Schafer, R. W. (2010). Discrete-Time Signal Processing (3rd ed.). Pearson. - Chapter 4 covers sampling of continuous-time signals