TExcelTAD
Home › Signals, Systems & Digital Filters for Engineers › The Sampling Theorem in Practice

The Sampling Theorem in Practice

A vibration sensor on a pump bearing carries useful information up to 2 kHz2\ \text{kHz}. The motor drive next to it radiates a strong 9 kHz9\ \text{kHz} component into the cable. You must pick a sample rate for the logger. "Twice the highest frequency" says 4 kSa/s4\ \text{kSa/s}, but at that rate the 9 kHz9\ \text{kHz} interference lands at 1 kHz1\ \text{kHz}, 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 7 Hz7\ \text{Hz} sine at 10 Sa/s10\ \text{Sa/s}. The lab shows the input, the samples and the only sine below fs/2f_s/2 that passes through all of them. With the defaults the readouts give a Nyquist frequency of 5 Hz5\ \text{Hz}, f/fs=0.7000f/f_s = 0.7000, an apparent frequency ∣fa∣=3 Hz\lvert f_a \rvert = 3\ \text{Hz} with fa=−3 Hzf_a = -3\ \text{Hz} (k=1k = 1), and "phase reversed: yes". Sweep ff (Play) and watch the reconstructed sine fold back and forth between 00 and 5 Hz5\ \text{Hz}. Set f=5 Hzf = 5\ \text{Hz} exactly: with ϕ=0\phi = 0 every sample is zero and the amplitude is lost, which is why the theorem needs fs>2ff_s > 2f strictly.

-A0+A0.0 s0.2 s0.4 s0.6 s0.8 s1.0 sx (V)input 7.0 Hzreconstructed 3.00 Hzsamples, fs = 10.0 Hz0A0102030405060708090100f (Hz)fs/2faliasaliased: f > fs/2 = 5.00 Hz

Theory: sampling copies the spectrum

Ideal sampling multiplies x(t)x(t) by an impulse train of period Ts=1/fsT_s = 1/f_s. In the frequency domain that becomes a sum of shifted copies:

Xs(f)=fs∑k=−∞∞X(f−kfs)X_s(f) = f_s \sum_{k=-\infty}^{\infty} X(f - k f_s)
fs > 2B: copies separate, a low-pass filter recovers x(t) 0 fs -fs fs/2 f X(f) fs < 2B: copies overlap around fs/2 (aliasing) 0 fs -fs fs/2 f X(f)

If x(t)x(t) is band-limited to ∣f∣<B\lvert f \rvert < B and fs>2Bf_s > 2B, the copies do not overlap and an ideal low-pass filter with cutoff fs/2f_s/2 returns the original exactly (Nyquist–Shannon). 2B2B is the Nyquist rate (a property of the signal); fs/2f_s/2 is the Nyquist frequency (a property of the sampler). If the copies overlap, a component at ff is indistinguishable from one at

fa=f−kfs,k=round⁡(f/fs),∣fa∣≤fs/2f_a = f - k f_s, \qquad k = \operatorname{round}(f / f_s), \qquad \lvert f_a \rvert \le f_s/2

and a negative faf_a 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 fs/2f_s/2.

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 2.22.2 to 1010 times the highest frequency of interest: audio at 44.144.1 or 48 kSa/s48\ \text{kSa/s} for a 20 kHz20\ \text{kHz} band (2.22.2 to 2.42.4 times), control loops often at 1010 to 2020 times the closed-loop bandwidth or more (a rule of thumb), oscilloscopes at 2.52.5 to 55 times their analog bandwidth (Scope acquisition).

Band-pass (under-)sampling. A signal confined to fL≤f≤fHf_L \le f \le f_H (bandwidth B=fH−fLB = f_H - f_L) can be sampled below 2fH2f_H as long as no copy lands on another. The allowed rates are

2fHm≤fs≤2fLm−1,m=1,2,…,⌊fH/B⌋\frac{2 f_H}{m} \le f_s \le \frac{2 f_L}{m - 1}, \qquad m = 1, 2, \ldots, \left\lfloor f_H / B \right\rfloor

Radio receivers use this to digitise an intermediate frequency directly. The windows get narrow as mm grows, so clock accuracy and filter edges matter.

Worked example: choosing a sample rate with an interferer

Given: wanted band 00 to 2 kHz2\ \text{kHz}; an interfering tone at 9 kHz9\ \text{kHz} that cannot be fully filtered; candidate rates 44, 88, 1010, 1212 and 20 kSa/s20\ \text{kSa/s}.

Find: where the interference appears for each rate, and which rates keep it out of 00 to 2 kHz2\ \text{kHz}.

Solve: fa=9−kfsf_a = 9 - k f_s with k=round⁡(9/fs)k = \operatorname{round}(9/f_s):

  • 4 kSa/s4\ \text{kSa/s}: k=2k = 2, fa=1 kHzf_a = 1\ \text{kHz} (in band).
  • 8 kSa/s8\ \text{kSa/s}: k=1k = 1, fa=1 kHzf_a = 1\ \text{kHz} (in band).
  • 10 kSa/s10\ \text{kSa/s}: k=1k = 1, fa=−1 kHzf_a = -1\ \text{kHz}, seen at 1 kHz1\ \text{kHz} (in band).
  • 12 kSa/s12\ \text{kSa/s}: k=1k = 1, fa=−3 kHzf_a = -3\ \text{kHz}, seen at 3 kHz3\ \text{kHz}: outside the wanted band, so a digital low-pass at 2 kHz2\ \text{kHz} removes it.
  • 20 kSa/s20\ \text{kSa/s}: k=0k = 0, no aliasing; 9 kHz9\ \text{kHz} stays at 9 kHz9\ \text{kHz}.

Check: the condition for the interferer to miss the band is ∣fa∣>2 kHz\lvert f_a \rvert > 2\ \text{kHz}. 12 kSa/s12\ \text{kSa/s} is the cheapest rate that works here, but it relies on knowing the interference frequency exactly; 20 kSa/s20\ \text{kSa/s} with a modest anti-alias filter is the robust choice.

python · try it yourself
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

python · try it yourself
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 mm gives the lowest rate, 10 kSa/s10\ \text{kSa/s} for a 25 kHz25\ \text{kHz} 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 12.9 kSa/s12.9\ \text{kSa/s} for m=4m = 4) 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 1 kSa/s1\ \text{kSa/s} is fed a sine whose frequency you step: 100100, 400400, 600600, 900900, 11001100, 1900 Hz1900\ \text{Hz}. The apparent frequencies are 100100, 400400, 400400, 100100, 100100, 100 Hz100\ \text{Hz}.

Simulation. In the lab, the same pattern at one hundredth scale: set fs=10 Hzf_s = 10\ \text{Hz} and f=1,4,6,9,11,19 Hzf = 1, 4, 6, 9, 11, 19\ \text{Hz} and read ∣fa∣\lvert f_a \rvert.

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 (1 Hz1\ \text{Hz} for a 1 s record) plus the generator's frequency error, which passes straight through to the alias (a 50 ppm50\ \text{ppm} error at 1.9 kHz1.9\ \text{kHz} is only 0.1 Hz0.1\ \text{Hz}), plus kk 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 00 to 3.3 V3.3\ \text{V} input: about 1.65 V1.65\ \text{V} offset, 1 V1\ \text{V} amplitude) and add a 1 kΩ1\ \text{k}\Omega series resistor to protect the pin.

Pitfalls

  • Applying "fs>2fmaxf_s > 2f_\text{max}" to the wanted signal only and forgetting interference, harmonics and noise above the band.
  • Sampling exactly at 2f2f: 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

  1. A 13 kHz13\ \text{kHz} tone is sampled at 5 kSa/s5\ \text{kSa/s}. What frequency appears?
  2. What is the lowest sample rate that keeps a 00 to 4 kHz4\ \text{kHz} signal free of aliases, in theory? Why is it not used?
  3. Can a signal occupying 7070 to 80 MHz80\ \text{MHz} be sampled at 40 MSa/s40\ \text{MSa/s}?
Answer
  1. k=round⁡(2.6)=3k = \operatorname{round}(2.6) = 3, fa=13−15=−2 kHzf_a = 13 - 15 = -2\ \text{kHz}: a 2 kHz2\ \text{kHz} tone (phase reversed).
  2. Anything just above 8 kSa/s8\ \text{kSa/s}. It needs a brick-wall anti-alias filter, which does not exist; practical rates are 1010 to 20 kSa/s20\ \text{kSa/s} or more.
  3. B=10 MHzB = 10\ \text{MHz}, ⌊80/10⌋=8\lfloor 80/10 \rfloor = 8. For m=4m = 4: 40≤fs≤46.7 MSa/s40 \le f_s \le 46.7\ \text{MSa/s}, so 40 MSa/s40\ \text{MSa/s} is at the edge of a valid window: it works with perfect filters but leaves no guard band at 80 MHz80\ \text{MHz}.

Test yourself

A 7 Hz sine is sampled at 10 Sa/s. The samples look like

  1. a 7 Hz sine
  2. a 3 Hz sine, phase reversed
  3. a 17 Hz sine
  4. DC

The Nyquist frequency of a 48 kSa/s audio converter is

  1. 96 kHz
  2. 48 kHz
  3. 24 kHz
  4. 20 kHz

Why does sampling a sine at exactly fs=2ff_s = 2f fail?

  1. the samples are always zero
  2. the amplitude seen depends on the phase and can be zero
  3. it aliases to 2f2f
  4. it works perfectly

A digital low-pass filter after the ADC can remove an alias only if

  1. the alias landed outside the band you keep
  2. the filter is FIR
  3. the alias is weaker than the signal
  4. never