Frequency spectrum interpretation in Signal Processing - Time & Space Complexity
We want to understand how the time to analyze a signal's frequency spectrum changes as the signal length grows.
How does the work needed to get frequency details increase when the input signal gets longer?
Analyze the time complexity of the following code snippet.
import numpy as np
def compute_spectrum(signal):
n = len(signal)
spectrum = np.fft.fft(signal)
magnitude = np.abs(spectrum)
return magnitude
This code computes the frequency spectrum magnitude of a signal using the Fast Fourier Transform (FFT).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The FFT algorithm processes the entire signal array.
- How many times: It performs operations proportional to the signal length times the logarithm of the signal length.
As the signal length grows, the work grows a bit faster than just the length itself but much slower than the square of the length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 33 operations |
| 100 | About 664 operations |
| 1000 | About 9966 operations |
Pattern observation: The operations grow faster than the input size but much slower than if we did every pair of points.
Time Complexity: O(n log n)
This means the time to compute the frequency spectrum grows a bit faster than the signal length but stays efficient even for large signals.
[X] Wrong: "Computing the frequency spectrum takes time proportional to the square of the signal length because it compares every point to every other point."
[OK] Correct: The FFT algorithm cleverly reduces the work by breaking the problem into smaller parts, so it only grows like n times log n, not n squared.
Understanding how FFT scales helps you explain why it is widely used in signal processing and data analysis, showing your grasp of efficient algorithms.
"What if we replaced FFT with a simple discrete Fourier transform (DFT) that uses nested loops? How would the time complexity change?"
Practice
Solution
Step 1: Understand frequency spectrum meaning
The frequency spectrum shows how much of each frequency is present in the signal.Step 2: Interpret peaks in the spectrum
A peak means that frequency has a strong presence or high amplitude in the signal.Final Answer:
A strong presence of a specific frequency in the signal -> Option AQuick Check:
Peak = strong frequency presence [OK]
- Confusing time duration with frequency
- Thinking amplitude in time domain equals peak in spectrum
- Assuming noise causes peaks
Solution
Step 1: Recall numpy FFT functions
np.fft.fft() computes the Fourier Transform to get frequency spectrum.Step 2: Check other options
np.fft.ifft() computes inverse FFT, others do not exist.Final Answer:
np.fft.fft(signal) -> Option CQuick Check:
FFT = np.fft.fft() [OK]
- Using inverse FFT instead of FFT
- Using non-existent numpy functions
- Confusing frequency and time domain functions
import numpy as np signal = np.sin(2 * np.pi * 5 * np.linspace(0, 1, 100)) spectrum = np.fft.fft(signal) peak_freq_index = np.argmax(np.abs(spectrum)) print(peak_freq_index)
What is the printed output?
Solution
Step 1: Understand the signal frequency
The signal is a sine wave at 5 Hz sampled 100 times over 1 second.Step 2: Find peak frequency index in FFT
FFT peak index corresponds to frequency 5, so index 5 is max magnitude.Final Answer:
5 -> Option DQuick Check:
Peak index = frequency = 5 [OK]
- Confusing index with frequency value
- Ignoring sampling rate and length
- Mistaking negative frequency indices
import numpy as np signal = np.sin(2 * np.pi * 10 * np.linspace(0, 1, 100)) spectrum = np.fft.fft(signal) peak = np.argmax(spectrum) print(peak)
What is the error?
Solution
Step 1: Check how peak is found
np.argmax(spectrum) finds max of complex numbers, which is incorrect.Step 2: Correct method to find peak
Use np.argmax(np.abs(spectrum)) to find peak magnitude index.Final Answer:
Using np.argmax on complex spectrum instead of magnitude -> Option BQuick Check:
Peak must be on magnitude, not complex values [OK]
- Argmax on complex array directly
- Confusing FFT and IFFT
- Ignoring magnitude for peak detection
Solution
Step 1: Check sampling and signal length
Sampling at 50 Hz for 2 seconds means 100 samples, so t has 100 points.Step 2: Verify FFT and frequency calculation
Use np.fft.fft(signal) and np.fft.fftfreq with d=1/50 for correct frequency bins.Step 3: Identify two largest peaks
Sort magnitudes and pick last two indices for peaks, then print sorted absolute frequencies.Final Answer:
Code snippet D correctly identifies both frequencies -> Option AQuick Check:
Correct sampling, FFT, freq bins, and peak selection [OK]
- Using inverse FFT instead of FFT
- Wrong sample count or sampling interval
- Incorrect frequency bin calculation
Ready to prove it?
Pass the test and this pattern moves unknown
