Bird
Raised Fist0
Signal Processingdata~7 mins

Sampling theorem (Nyquist-Shannon) in Signal Processing

Choose your learning style10 modes available
Introduction

The sampling theorem tells us how to convert a continuous signal into a set of samples without losing information. It helps us understand how often we need to measure a signal to keep it accurate.

When recording sound to convert it into digital audio.
When capturing images or videos using digital cameras.
When measuring sensor data like temperature or pressure digitally.
When transmitting signals over digital communication systems.
Syntax
Signal Processing
Fs >= 2 * Fmax

where:
Fs = Sampling frequency (how often we take samples)
Fmax = Highest frequency in the signal

The sampling frequency must be at least twice the highest frequency present in the signal.

If sampling is done below this rate, the signal can be distorted (called aliasing).

Examples
We calculate the minimum sampling frequency needed to capture a signal with a max frequency of 1000 Hz.
Signal Processing
Fmax = 1000  # highest frequency in Hz
Fs = 2 * Fmax  # minimum sampling frequency
print(Fs)  # Output: 2000
Check if a sampling frequency of 1500 Hz is enough for a signal with max frequency 1000 Hz.
Signal Processing
Fs = 1500
Fmax = 1000
if Fs >= 2 * Fmax:
    print("Sampling frequency is sufficient.")
else:
    print("Sampling frequency is too low, aliasing may occur.")
Sample Program

This program shows a 5 Hz sine wave sampled at three different rates: below, at, and above the Nyquist rate (which is 10 Hz here). The plot helps visualize how undersampling causes distortion, while sampling at or above the Nyquist rate captures the signal correctly.

Signal Processing
import numpy as np
import matplotlib.pyplot as plt

# Define a continuous signal: a sine wave with frequency 5 Hz
f = 5  # frequency in Hz
T = 1  # duration in seconds

t_continuous = np.linspace(0, T, 1000)  # continuous time points
signal_continuous = np.sin(2 * np.pi * f * t_continuous)

# Sampling frequency less than Nyquist rate (undersampling)
Fs_low = 8  # sampling frequency in Hz (less than 2*f=10)
n_samples_low = int(Fs_low * T)
t_samples_low = np.linspace(0, T, n_samples_low, endpoint=False)
samples_low = np.sin(2 * np.pi * f * t_samples_low)

# Sampling frequency equal to Nyquist rate
Fs_nyquist = 2 * f  # 10 Hz
n_samples_nyquist = int(Fs_nyquist * T)
t_samples_nyquist = np.linspace(0, T, n_samples_nyquist, endpoint=False)
samples_nyquist = np.sin(2 * np.pi * f * t_samples_nyquist)

# Sampling frequency higher than Nyquist rate
Fs_high = 20  # 20 Hz
n_samples_high = int(Fs_high * T)
t_samples_high = np.linspace(0, T, n_samples_high, endpoint=False)
samples_high = np.sin(2 * np.pi * f * t_samples_high)

# Plotting
plt.figure(figsize=(10, 6))
plt.plot(t_continuous, signal_continuous, label='Continuous Signal', color='black')
plt.stem(t_samples_low, samples_low, linefmt='r-', markerfmt='ro', basefmt='r-', label='Undersampling (8 Hz)')
plt.stem(t_samples_nyquist, samples_nyquist, linefmt='g-', markerfmt='go', basefmt='g-', label='Nyquist Rate (10 Hz)')
plt.stem(t_samples_high, samples_high, linefmt='b-', markerfmt='bo', basefmt='b-', label='Oversampling (20 Hz)')
plt.title('Sampling Theorem (Nyquist-Shannon) Example')
plt.xlabel('Time (seconds)')
plt.ylabel('Amplitude')
plt.legend()
plt.grid(True)
plt.show()
OutputSuccess
Important Notes

Sampling below the Nyquist rate causes aliasing, which means the signal looks like a different frequency.

In practice, signals are often filtered to remove frequencies above half the sampling rate before sampling.

Nyquist rate is the theoretical minimum; sampling faster can improve quality and simplify processing.

Summary

The sampling theorem tells us how often to measure a signal to keep all information.

Sampling frequency must be at least twice the highest frequency in the signal.

Sampling below this rate causes errors called aliasing.

Practice

(1/5)
1. What does the Nyquist-Shannon sampling theorem state about the sampling rate?
easy
A. Sample at the same rate as the highest frequency
B. Sample at half the highest frequency of the signal
C. Sample at any random rate
D. Sample at least twice the highest frequency of the signal

Solution

  1. Step 1: Understand the sampling theorem

    The Nyquist-Shannon theorem says to sample at least twice the highest frequency to capture all information.
  2. Step 2: Identify correct sampling rate

    Sampling below twice the highest frequency causes aliasing and loss of information.
  3. Final Answer:

    Sample at least twice the highest frequency of the signal -> Option D
  4. Quick Check:

    Sampling rate ≥ 2 x highest frequency [OK]
Hint: Remember: sample rate must be double highest frequency [OK]
Common Mistakes:
  • Thinking sampling rate can be less than highest frequency
  • Confusing sampling rate with signal frequency
  • Ignoring aliasing effects
2. Which of the following is the correct Python syntax to calculate the Nyquist frequency for a signal with sampling rate fs?
easy
A. nyquist = fs / 2
B. nyquist = fs * 2
C. nyquist = 2 / fs
D. nyquist = fs + 2

Solution

  1. Step 1: Recall Nyquist frequency formula

    The Nyquist frequency is half the sampling rate, so nyquist = fs / 2.
  2. Step 2: Match formula to options

    Only nyquist = fs / 2 correctly divides fs by 2.
  3. Final Answer:

    nyquist = fs / 2 -> Option A
  4. Quick Check:

    Nyquist frequency = sampling rate / 2 [OK]
Hint: Nyquist frequency is half the sampling rate [OK]
Common Mistakes:
  • Multiplying instead of dividing by 2
  • Confusing Nyquist frequency with sampling rate
  • Using addition or reciprocal incorrectly
3. Given a signal with highest frequency 500 Hz, what is the minimum sampling rate to avoid aliasing? Consider the code:
highest_freq = 500
sampling_rate = ?
medium
A. sampling_rate = 1000
B. sampling_rate = 750
C. sampling_rate = 250
D. sampling_rate = 500

Solution

  1. Step 1: Apply Nyquist theorem to highest frequency

    Minimum sampling rate = 2 x highest frequency = 2 x 500 = 1000 Hz.
  2. Step 2: Compare with options

    Only sampling_rate = 1000 matches 1000 Hz.
  3. Final Answer:

    sampling_rate = 1000 -> Option A
  4. Quick Check:

    Minimum sampling rate = 2 x 500 = 1000 [OK]
Hint: Double the highest frequency for minimum sampling rate [OK]
Common Mistakes:
  • Choosing sampling rate equal to highest frequency
  • Selecting a rate less than twice the frequency
  • Confusing frequency units
4. Identify the error in this Python code snippet that calculates the Nyquist frequency:
fs = 800
nyquist = fs * 0.5
print("Nyquist frequency is", nyquist)
medium
A. The multiplication should be division by 2
B. The variable fs should be lowercase
C. The code is correct and will print 400.0
D. The print statement syntax is wrong

Solution

  1. Step 1: Understand calculation of Nyquist frequency

    Nyquist frequency = sampling rate divided by 2, which equals fs * 0.5.
  2. Step 2: Check code correctness

    Code multiplies fs by 0.5 correctly and prints the value 400.0.
  3. Final Answer:

    The code is correct and will print 400.0 -> Option C
  4. Quick Check:

    fs * 0.5 = 400.0, print works fine [OK]
Hint: Multiplying by 0.5 equals dividing by 2 [OK]
Common Mistakes:
  • Thinking multiplication by 0.5 is wrong
  • Assuming variable case matters here
  • Believing print syntax is incorrect in Python 3
5. You have a signal with components up to 1200 Hz. You sample it at 2000 Hz. What problem will occur and why?

Options:
A) No problem, sampling rate is sufficient
B) Aliasing will occur because sampling rate is less than twice highest frequency
C) Aliasing will occur because sampling rate is more than twice highest frequency
D) Signal will be perfectly reconstructed without aliasing
hard
A. No problem, sampling rate is sufficient
B. Aliasing will occur because sampling rate is less than twice highest frequency
C. Aliasing will occur because sampling rate is more than twice highest frequency
D. Signal will be perfectly reconstructed without aliasing

Solution

  1. Step 1: Calculate minimum sampling rate needed

    Minimum sampling rate = 2 x 1200 Hz = 2400 Hz.
  2. Step 2: Compare given sampling rate with minimum

    Given sampling rate is 2000 Hz, which is less than 2400 Hz, so aliasing will occur.
  3. Step 3: Understand aliasing cause

    Sampling below twice the highest frequency causes signal distortion called aliasing.
  4. Final Answer:

    Aliasing will occur because sampling rate is less than twice highest frequency -> Option B
  5. Quick Check:

    Sampling rate < 2 x highest frequency causes aliasing [OK]
Hint: Sampling rate must be ≥ 2 x highest frequency to avoid aliasing [OK]
Common Mistakes:
  • Assuming 2000 Hz is enough for 1200 Hz signal
  • Confusing aliasing cause with oversampling
  • Thinking aliasing occurs when sampling rate is too high
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard