Lesson 2 of 15

Sampling and the Nyquist Theorem

Sampling and the Nyquist Theorem

Digital signals are created by sampling a continuous signal at discrete time steps. The sampling rate fsf_s (in Hz) determines what frequencies can be faithfully captured.

The Nyquist–Shannon Sampling Theorem

To perfectly reconstruct a signal with highest frequency fmaxf_{\max}, the sampling rate must satisfy:

fs2fmaxf_s \geq 2 f_{\max}

The Nyquist rate is the minimum sufficient rate:

fNyquist=2fmaxf_{\text{Nyquist}} = 2 f_{\max}

Example: CD audio captures frequencies up to 22,050 Hz, so it uses fs=44,100f_s = 44,100 Hz — exactly the Nyquist rate.

Aliasing

When a signal is sampled below the Nyquist rate, aliasing occurs: high-frequency components are "folded" into lower frequencies and become indistinguishable from them.

A frequency ff sampled at rate fsf_s aliases to:

falias=fround ⁣(ffs)fsf_{\text{alias}} = \left|f - \text{round}\!\left(\frac{f}{f_s}\right) \cdot f_s\right|

Example: A 1300 Hz tone sampled at 1000 Hz aliases to 130011000=300|1300 - 1 \cdot 1000| = 300 Hz.

Your Task

Implement:

  • nyquist_rate(f_max) — returns 2fmax2 f_{\max}
  • aliased_frequency(f, fs) — returns the aliased frequency when ff is sampled at fsf_s
def nyquist_rate(f_max):
    return 2 * f_max

def aliased_frequency(f, fs):
    return abs(f - round(f / fs) * fs)

print(nyquist_rate(22050))           # 44100
print(aliased_frequency(1300, 1000)) # 300
Python runtime loading...
Loading...
Click "Run" to execute your code.