Sampling theorem (Nyquist-Shannon) in Signal Processing - Time & Space Complexity
We want to understand how the time needed to sample a signal changes as the signal length grows.
How does the number of operations grow when we increase the signal size?
Analyze the time complexity of the following code snippet.
# signal is a list of samples
sampling_rate = 2 * max_frequency # Nyquist rate
samples = []
step = int(1 / sampling_rate) if sampling_rate != 0 else 1
for i in range(0, len(signal), step):
samples.append(signal[i])
This code takes a continuous signal and samples it at the Nyquist rate to avoid losing information.
- Primary operation: Looping through the signal array to pick samples.
- How many times: Approximately proportional to the length of the signal divided by the sampling interval.
As the signal length grows, the number of samples taken grows roughly in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 sampling steps |
| 100 | About 100 sampling steps |
| 1000 | About 1000 sampling steps |
Pattern observation: The operations grow linearly as the signal length increases.
Time Complexity: O(n)
This means the time to sample grows directly in proportion to the signal length.
[X] Wrong: "Sampling time grows faster than the signal length because of complex calculations."
[OK] Correct: Sampling just picks points at regular intervals, so the work grows only as the signal gets longer, not more complex.
Understanding how sampling scales helps you explain signal processing tasks clearly and shows you can think about efficiency in real data work.
"What if we changed the sampling rate to be twice the Nyquist rate? How would the time complexity change?"
Practice
Solution
Step 1: Understand the sampling theorem
The Nyquist-Shannon theorem says to sample at least twice the highest frequency to capture all information.Step 2: Identify correct sampling rate
Sampling below twice the highest frequency causes aliasing and loss of information.Final Answer:
Sample at least twice the highest frequency of the signal -> Option DQuick Check:
Sampling rate ≥ 2 x highest frequency [OK]
- Thinking sampling rate can be less than highest frequency
- Confusing sampling rate with signal frequency
- Ignoring aliasing effects
fs?Solution
Step 1: Recall Nyquist frequency formula
The Nyquist frequency is half the sampling rate, so nyquist = fs / 2.Step 2: Match formula to options
Only nyquist = fs / 2 correctly divides fs by 2.Final Answer:
nyquist = fs / 2 -> Option AQuick Check:
Nyquist frequency = sampling rate / 2 [OK]
- Multiplying instead of dividing by 2
- Confusing Nyquist frequency with sampling rate
- Using addition or reciprocal incorrectly
highest_freq = 500 sampling_rate = ?
Solution
Step 1: Apply Nyquist theorem to highest frequency
Minimum sampling rate = 2 x highest frequency = 2 x 500 = 1000 Hz.Step 2: Compare with options
Only sampling_rate = 1000 matches 1000 Hz.Final Answer:
sampling_rate = 1000 -> Option AQuick Check:
Minimum sampling rate = 2 x 500 = 1000 [OK]
- Choosing sampling rate equal to highest frequency
- Selecting a rate less than twice the frequency
- Confusing frequency units
fs = 800
nyquist = fs * 0.5
print("Nyquist frequency is", nyquist)Solution
Step 1: Understand calculation of Nyquist frequency
Nyquist frequency = sampling rate divided by 2, which equals fs * 0.5.Step 2: Check code correctness
Code multiplies fs by 0.5 correctly and prints the value 400.0.Final Answer:
The code is correct and will print 400.0 -> Option CQuick Check:
fs * 0.5 = 400.0, print works fine [OK]
- Thinking multiplication by 0.5 is wrong
- Assuming variable case matters here
- Believing print syntax is incorrect in Python 3
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
Solution
Step 1: Calculate minimum sampling rate needed
Minimum sampling rate = 2 x 1200 Hz = 2400 Hz.Step 2: Compare given sampling rate with minimum
Given sampling rate is 2000 Hz, which is less than 2400 Hz, so aliasing will occur.Step 3: Understand aliasing cause
Sampling below twice the highest frequency causes signal distortion called aliasing.Final Answer:
Aliasing will occur because sampling rate is less than twice highest frequency -> Option BQuick Check:
Sampling rate < 2 x highest frequency causes aliasing [OK]
- Assuming 2000 Hz is enough for 1200 Hz signal
- Confusing aliasing cause with oversampling
- Thinking aliasing occurs when sampling rate is too high
Ready to prove it?
Pass the test and this pattern moves unknown
