The Sampling Theorem in Practice
A vibration sensor on a pump bearing carries useful information up to . The motor drive next to it radiates a strong component into the cable. You must pick a sample rate for the logger. "Twice the highest frequency" says , but at that rate the interference lands at , right in the middle of the bearing spectrum, and nothing done to the samples afterwards can tell it apart from a real bearing fault. This lesson turns the sampling theorem into a procedure: where every input frequency ends up after sampling, how fast to sample, and when sampling below the signal frequency is allowed on purpose. The proof and the ideal reconstruction formula are in The Sampling Theorem and Aliasing.
Intuition: two sines, one set of samples
Sample a sine at . The lab shows the input, the samples and the only sine below that passes through all of them. With the defaults the readouts give a Nyquist frequency of , , an apparent frequency with (), and "phase reversed: yes". Sweep (Play) and watch the reconstructed sine fold back and forth between and . Set exactly: with every sample is zero and the amplitude is lost, which is why the theorem needs strictly.
Theory: sampling copies the spectrum
Ideal sampling multiplies by an impulse train of period . In the frequency domain that becomes a sum of shifted copies:
If is band-limited to and , the copies do not overlap and an ideal low-pass filter with cutoff returns the original exactly (Nyquist–Shannon). is the Nyquist rate (a property of the signal); is the Nyquist frequency (a property of the sampler). If the copies overlap, a component at is indistinguishable from one at
and a negative means the apparent sine has its phase reversed (for a cosine input the samples are the same; for a sine they are negated). The rule is the same "folding" picture as paper folded at multiples of .
Engineering practice. Real signals are never strictly band-limited, real anti-alias filters have a transition band (next lesson), and real reconstruction is not ideal (Reconstruction and DACs). So systems sample at about to times the highest frequency of interest: audio at or for a band ( to times), control loops often at to times the closed-loop bandwidth or more (a rule of thumb), oscilloscopes at to times their analog bandwidth (Scope acquisition).
Band-pass (under-)sampling. A signal confined to (bandwidth ) can be sampled below as long as no copy lands on another. The allowed rates are
Radio receivers use this to digitise an intermediate frequency directly. The windows get narrow as grows, so clock accuracy and filter edges matter.
Worked example: choosing a sample rate with an interferer
Given: wanted band to ; an interfering tone at that cannot be fully filtered; candidate rates , , , and .
Find: where the interference appears for each rate, and which rates keep it out of to .
Solve: with :
- : , (in band).
- : , (in band).
- : , , seen at (in band).
- : , , seen at : outside the wanted band, so a digital low-pass at removes it.
- : , no aliasing; stays at .
Check: the condition for the interferer to miss the band is . is the cheapest rate that works here, but it relies on knowing the interference frequency exactly; with a modest anti-alias filter is the robust choice.
import numpy as np
def alias(f, fs):
"""Apparent frequency (signed) of a tone f after sampling at fs."""
k = np.round(f / fs)
return f - k * fs
f_int, band = 9e3, 2e3
for fs in (4e3, 8e3, 10e3, 12e3, 20e3):
fa = alias(f_int, fs)
verdict = "IN BAND" if abs(fa) <= band else "outside the band"
print(f"fs = {fs / 1e3:4.0f} kSa/s: 9 kHz appears at {abs(fa) / 1e3:.1f} kHz (signed {fa / 1e3:+.1f}) -> {verdict}")
# The samples of 7 Hz and of -3 Hz at 10 Sa/s are identical: sin(2 pi 7 n/10) = -sin(2 pi 3 n/10)
n = np.arange(10)
x7 = np.sin(2 * np.pi * 7 * n / 10)
x3 = np.sin(2 * np.pi * 3 * n / 10)
print("max |x7 + x3| over 10 samples:", f"{np.max(np.abs(x7 + x3)):.1e}")fs = 4 kSa/s: 9 kHz appears at 1.0 kHz (signed +1.0) -> IN BAND
fs = 8 kSa/s: 9 kHz appears at 1.0 kHz (signed +1.0) -> IN BAND
fs = 10 kSa/s: 9 kHz appears at 1.0 kHz (signed -1.0) -> IN BAND
fs = 12 kSa/s: 9 kHz appears at 3.0 kHz (signed -3.0) -> outside the band
fs = 20 kSa/s: 9 kHz appears at 9.0 kHz (signed +9.0) -> outside the band
max |x7 + x3| over 10 samples: 4.3e-15
Band-pass sampling windows
import numpy as np
import matplotlib.pyplot as plt
# Valid under-sampling rates for a band from fL to fH (here 20 to 25 kHz, B = 5 kHz)
fL, fH = 20e3, 25e3
B = fH - fL
for m in range(1, int(fH // B) + 1):
lo = 2 * fH / m
hi = 2 * fL / (m - 1) if m > 1 else np.inf
if lo <= hi:
print(f"m = {m}: {lo / 1e3:6.2f} kSa/s <= fs <= {hi / 1e3 if np.isfinite(hi) else float('inf'):6.2f} kSa/s")
# Picture: 7 Hz sampled at 10 Sa/s and the 3 Hz sine through the same points
t = np.linspace(0, 1, 1000)
n = np.arange(11) / 10
plt.figure(figsize=(7, 2.6))
plt.plot(t, np.sin(2 * np.pi * 7 * t), lw=1, label="7 Hz input")
plt.plot(t, -np.sin(2 * np.pi * 3 * t), "--", label="3 Hz alias (phase reversed)")
plt.plot(n, np.sin(2 * np.pi * 7 * n), "o", color="k", label="samples, fs = 10 Sa/s")
plt.xlabel("t (s)"); plt.legend(loc="upper right", fontsize=8); plt.tight_layout()
plt.show()m = 1: 50.00 kSa/s <= fs <= inf kSa/s
m = 2: 25.00 kSa/s <= fs <= 40.00 kSa/s
m = 3: 16.67 kSa/s <= fs <= 20.00 kSa/s
m = 4: 12.50 kSa/s <= fs <= 13.33 kSa/s
m = 5: 10.00 kSa/s <= fs <= 10.00 kSa/s
The highest gives the lowest rate, for a signal, but its window has zero width: any clock error causes overlap. A practical design takes an interior point of a wider window (for example around for ) and leaves guard bands.
Guided lab: watching aliases on a slow sampler
Hand calculation. A USB data-acquisition module or a microcontroller ADC sampling at is fed a sine whose frequency you step: , , , , , . The apparent frequencies are , , , , , .
Simulation. In the lab, the same pattern at one hundredth scale: set and and read .
What you would measure. Record 1 s at each frequency and find the peak of the FFT (The DFT and FFT). The apparent frequency should match the prediction to within the FFT bin spacing ( for a 1 s record) plus the generator's frequency error, which passes straight through to the alias (a error at is only ), plus times the sampler's clock error. The amplitude stays the same: aliasing moves energy, it does not remove it.
Hazards and safe setup. Signal generator and USB- or battery-powered sampler only. Set the generator's amplitude and offset with its output off, keep the signal inside the ADC's input range (for a to input: about offset, amplitude) and add a series resistor to protect the pin.
Pitfalls
- Applying "" to the wanted signal only and forgetting interference, harmonics and noise above the band.
- Sampling exactly at : the amplitude you see depends on the phase.
- Believing a digital filter after the ADC can remove an alias (it can only remove aliases that landed outside the band you keep).
- Under-sampling without a band-pass filter in front: every other band that folds onto the same spot comes in too.
Practice
- A tone is sampled at . What frequency appears?
- What is the lowest sample rate that keeps a to signal free of aliases, in theory? Why is it not used?
- Can a signal occupying to be sampled at ?
Answer
- , : a tone (phase reversed).
- Anything just above . It needs a brick-wall anti-alias filter, which does not exist; practical rates are to or more.
- , . For : , so is at the edge of a valid window: it works with perfect filters but leaves no guard band at .
Test yourself
A 7 Hz sine is sampled at 10 Sa/s. The samples look like
- a 7 Hz sine
- a 3 Hz sine, phase reversed
- a 17 Hz sine
- DC
The Nyquist frequency of a 48 kSa/s audio converter is
- 96 kHz
- 48 kHz
- 24 kHz
- 20 kHz
Why does sampling a sine at exactly fail?
- the samples are always zero
- the amplitude seen depends on the phase and can be zero
- it aliases to
- it works perfectly
A digital low-pass filter after the ADC can remove an alias only if
- the alias landed outside the band you keep
- the filter is FIR
- the alias is weaker than the signal
- never