Signal-to-noise ratio (SNR) in Signal Processing - Time & Space Complexity
We want to understand how the time to calculate Signal-to-noise ratio (SNR) changes as the input signal size grows.
How does the number of steps grow when we have more signal data?
Analyze the time complexity of the following code snippet.
# signal is a list of numbers representing the signal
# noise is a list of numbers representing the noise
signal_power = sum(x**2 for x in signal)
noise_power = sum(x**2 for x in noise)
snr = signal_power / noise_power
This code calculates the power of the signal and noise by summing the squares of their values, then finds the ratio.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Summing squares of each element in the signal and noise lists.
- How many times: Once for each element in signal and once for each element in noise.
As the number of elements in signal and noise grows, the steps to sum their squares grow proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 sums (10 for signal, 10 for noise) |
| 100 | About 200 sums |
| 1000 | About 2000 sums |
Pattern observation: The number of operations grows roughly in direct proportion to the input size.
Time Complexity: O(n)
This means the time to compute SNR grows linearly as the signal and noise data get larger.
[X] Wrong: "Calculating SNR takes the same time no matter how big the signal is."
[OK] Correct: Because each element must be processed, more data means more work and more time.
Understanding how SNR calculation time grows helps you explain efficiency when working with large datasets in real projects.
"What if we precomputed the squares and stored them? How would that change the time complexity?"
Practice
Solution
Step 1: Understand the definition of SNR
SNR is defined as the ratio of signal power to noise power, showing how strong the signal is compared to noise.Step 2: Compare options with definition
Only The strength of the signal compared to the background noise correctly states that SNR measures signal strength relative to noise.Final Answer:
The strength of the signal compared to the background noise -> Option AQuick Check:
SNR = Signal strength / Noise strength [OK]
- Confusing SNR with noise power alone
- Thinking SNR measures frequency
- Mixing up signal duration with SNR
Solution
Step 1: Recall the SNR formula in decibels
SNR in dB is calculated as 10 times the base-10 logarithm of the ratio of signal power to noise power.Step 2: Check each option against the formula
SNR(dB) = 10 * log10(signal power / noise power) matches the correct formula. Options A, B, and D are incorrect due to missing log, wrong multiplier, or incorrect operations.Final Answer:
SNR(dB) = 10 * log10(signal power / noise power) -> Option AQuick Check:
SNR(dB) = 10 * log10(Signal/Noise) [OK]
- Using 20 instead of 10 for power ratio
- Forgetting the logarithm
- Adding signal and noise instead of dividing
SNR(dB) = 10 * log10(signal power / noise power).Solution
Step 1: Calculate the ratio of signal power to noise power
Ratio = 50 / 2 = 25.Step 2: Calculate 10 * log10(25)
log10(25) ≈ 1.398, so SNR(dB) = 10 * 1.398 ≈ 13.98 dB.Final Answer:
10 * log10(50 / 2) ≈ 13.98 dB -> Option BQuick Check:
SNR(dB) = 10 * log10(25) ≈ 13.98 dB [OK]
- Adding signal and noise instead of dividing
- Using log base e instead of log base 10
- Confusing subtraction with division
import math signal_power = 100 noise_power = 10 snr_db = 10 * math.log(signal_power / noise_power) print(snr_db)
Solution
Step 1: Check the logarithm function used
The code uses math.log(), which is natural logarithm (base e), but SNR in dB requires log base 10.Step 2: Identify correct function for log base 10
Python's math module has math.log10() for base 10 logarithm, which should be used instead.Final Answer:
Using natural log instead of log base 10 -> Option CQuick Check:
Use math.log10() for dB calculations [OK]
- Using math.log() instead of math.log10()
- Swapping signal and noise in division
- Confusing multiplication factor (10 vs 20)
SNR(dB) = 10 * log10(signal power / noise power).Solution
Step 1: Calculate new signal and noise powers after filtering
Signal power reduced by 10%: 200 * 0.9 = 180
Noise power reduced by half: 50 / 2 = 25Step 2: Calculate new SNR in dB
SNR = 10 * log10(180 / 25) = 10 * log10(7.2) ≈ 10 * 0.8573 = 8.57 dB.Final Answer:
10 * log10(180 / 25) ≈ 8.57 dB -> Option DQuick Check:
New SNR = 10 * log10(7.2) ≈ 8.57 dB [OK]
- Not reducing signal power by 10%
- Not halving noise power
- Using original powers without changes
Ready to prove it?
Pass the test and this pattern moves unknown
