Fast Fourier Transform (FFT) algorithm in Signal Processing - Time & Space Complexity
We want to understand how the time needed to run the FFT algorithm changes as the input signal gets bigger.
Specifically, how does the number of steps grow when we increase the size of the data?
Analyze the time complexity of the following code snippet.
function FFT(x) {
const N = x.length;
if (N <= 1) return x;
const even = FFT(x.filter((_, i) => i % 2 === 0));
const odd = FFT(x.filter((_, i) => i % 2 === 1));
const T = [];
for (let k = 0; k < N / 2; k++) {
const t = Math.exp(-2 * Math.PI * 1j * k / N) * odd[k];
T[k] = even[k] + t;
T[k + N / 2] = even[k] - t;
}
return T;
}
This code splits the input into even and odd parts, recursively processes them, and combines results to compute the FFT.
- Primary operation: Recursive calls splitting the input in half each time.
- How many times: The recursion happens about log2(n) times because the input size halves each call.
- Loop inside each recursion: A loop runs n/2 times to combine results.
Each time we double the input size, the number of steps grows a bit more than double but much less than square.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 steps |
| 100 | About 700 steps |
| 1000 | About 10,000 steps |
Pattern observation: The steps grow roughly like n times log n, which is faster than n squared but slower than just n.
Time Complexity: O(n log n)
This means if you double the input size, the time needed grows a bit more than double but much less than four times.
[X] Wrong: "FFT runs in linear time O(n) because it just processes the input once."
[OK] Correct: FFT uses recursion and combines results multiple times, so it does more work than just one pass over the data.
Understanding FFT's time complexity shows you can analyze algorithms that use divide-and-conquer and recursion, a useful skill in many data science and signal processing tasks.
"What if we changed the recursion to split the input into three parts instead of two? How would the time complexity change?"
Practice
Fast Fourier Transform (FFT) algorithm in signal processing?Solution
Step 1: Understand FFT role and compare options
FFT transforms time-domain signal to frequency domain. A matches exactly; B is compression, C filtering, D amplification.Final Answer:
To convert a signal from time domain to frequency domain -> Option BQuick Check:
FFT = Time to Frequency conversion [OK]
- Confusing FFT with noise filtering
- Thinking FFT compresses data
- Assuming FFT amplifies signals
Solution
Step 1: Recall and match Python import syntax
Correct for specific function: 'from numpy.fft import fft' (C). A imports submodule, B aliases submodule (requires fft.fft()), D invalid.Final Answer:
from numpy.fft import fft -> Option DQuick Check:
Correct import syntax = from numpy.fft import fft [OK]
- Using 'import fft from numpy' which is invalid syntax
- Importing whole numpy.fft as fft causing confusion
- Trying 'from numpy import fft' which imports the module, not function
import numpy as np signal = np.array([1, 0, -1, 0]) fft_result = np.fft.fft(signal) print(np.round(fft_result, 2))
What is the output?
Solution
Step 1: Compute and verify FFT output
np.fft.fft([1,0,-1,0]) corresponds to a sine wave component, resulting in imaginary parts at k=1 and k=3. The output is [0.+0.j 0.+2.j 0.+0.j 0.-2.j], matching [0.+0.j 0.+2.j 0.+0.j 0.-2.j].Final Answer:
[0.+0.j 0.+2.j 0.+0.j 0.-2.j] -> Option AQuick Check:
FFT sine wave = imaginary components at k=1,3 [OK]
- Confusing cosine signal with sine (imaginary output)
- Confusing zeros with non-zero frequency components
- Rounding incorrectly or not at all
import numpy as np signal = [1, 2, 3, 4] fft_result = fft(signal) print(fft_result.real)
It raises an error. What is the most likely cause?
Solution
Step 1: Trace code execution for error
fft(signal) raises NameError: 'fft' undefined. Import is numpy as np (use np.fft.fft); no direct fft import (e.g., from numpy.fft import fft). A, B, D unrelated.Final Answer:
Missing import statement for fft -> Option CQuick Check:
Undefined fft = missing import [OK]
- Not importing the fft function when using fft()
- Thinking .real is invalid on FFT output
- Confusing print syntax errors
data. You want to remove frequencies above 100 Hz using FFT. Which approach correctly achieves this?Solution
Step 1: Outline correct FFT low-pass filtering
FFT to freq domain (use fftfreq for bins), zero coeffs where |freq| > 100 (fs=1000, cutoff index ~100/500=0.2*N), ifft back. Matches B precisely; A ignores freq mapping, C/D ineffective.Final Answer:
Compute FFT, zero out components with frequency > 100 Hz, then inverse FFT -> Option AQuick Check:
Zero high freq + ifft = low-pass filter [OK]
- Confusing component count with frequency
- Scaling FFT values instead of zeroing
- Misusing FFT to subtract frequencies directly
Ready to prove it?
Pass the test and this pattern moves unknown
