Real FFT (rfft) in SciPy - Time & Space Complexity
We want to understand how the time it takes to compute a real FFT changes as the input size grows.
How does the work needed grow when we give the function longer signals?
Analyze the time complexity of the following code snippet.
import numpy as np
from scipy.fft import rfft
signal = np.random.rand(1024) # real input signal
fft_result = rfft(signal)
This code computes the real FFT of a signal with 1024 points, transforming it into frequency components.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The FFT algorithm repeatedly combines and processes pairs of data points through stages.
- How many times: The number of stages grows roughly with the logarithm of the input size.
As the input size doubles, the number of operations grows a bit more than double but much less than square.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 |
| 100 | About 700 |
| 1000 | About 10,000 |
Pattern observation: The operations grow roughly like n times log n, which is faster than just n but slower than n squared.
Time Complexity: O(n log n)
This means if you double the input size, the work grows a bit more than twice, but not as much as four times.
[X] Wrong: "The FFT runs in linear time, so doubling input size doubles the time exactly."
[OK] Correct: FFT uses a divide-and-conquer approach, so the time grows a bit faster than linear, because it processes multiple stages.
Understanding FFT time complexity helps you explain how signal processing scales and shows you can analyze efficient algorithms.
"What if we used a standard FFT on complex input instead of rfft on real input? How would the time complexity change?"
Practice
scipy.fft.rfft function compute when applied to a real-valued signal?Solution
Step 1: Understand the input type
rfftis designed for real-valued input signals.Step 2: Analyze the output of
It returns only the positive frequency components because the negative frequencies are mirror images for real signals.rfftFinal Answer:
The positive frequency components of the signal's Fourier transform -> Option BQuick Check:
Real input -> half spectrum output [OK]
- Confusing rfft with full fft output
- Thinking rfft returns time-domain data
- Assuming rfft returns negative frequencies
rfft from SciPy's FFT module?Solution
Step 1: Recall correct import syntax
Therfftfunction is inscipy.fft, so import it directly from there.Step 2: Check usage
Callingrfft(signal)after import is the correct usage.Final Answer:
from scipy.fft import rfft\nresult = rfft(signal) -> Option CQuick Check:
Correct import and call syntax [OK]
- Trying to import rfft from scipy directly
- Using wrong module names
- Calling rfft as a method of scipy.fft without import
import numpy as np from scipy.fft import rfft signal = np.array([1, 0, -1, 0]) result = rfft(signal) print(np.round(result, 2))
Solution
Step 1: Understand the input signal
The signal is [1, 0, -1, 0], a simple 4-point real array.Step 2: Compute rfft manually or recall pattern
rfft returns N/2+1 = 3 complex values for length 4 input. The values correspond to DC, positive frequency, and Nyquist frequency components. The result is approximately [0.+0.j, 2.+0.j, 0.+0.j].Final Answer:
[0.+0.j 2.+0.j 0.+0.j] -> Option DQuick Check:
rfft length 4 -> 3 outputs, middle value 2 [OK]
- Expecting output length same as input
- Ignoring complex number format
- Misreading the signal values
rfft and fix it:import numpy as np from scipy.fft import rfft signal = [1, 2, 3, 4] result = rfft(signal, n=2) print(result)
Solution
Step 1: Check the
Thenparameter usagenparameter sets the length of the FFT. Here,n=2is less than the signal length 4, so the signal is truncated.Step 2: Fix the parameter to match or exceed signal length
Usingn=4ensures the full signal is transformed without truncation.Final Answer:
The parameternis smaller than signal length; usen=4instead -> Option AQuick Check:
FFT length must cover signal length [OK]
- Passing list instead of array (allowed but less efficient)
- Ignoring effect of n parameter truncation
- Wrong import module for rfft
rfft. Which code snippet correctly computes this frequency?Solution
Step 1: Use rfft and rfftfreq correctly
For real signals,rfftcomputes the positive frequencies, andrfftfreqgives the corresponding frequency bins. The sampling interval is 1/100 seconds.Step 2: Find the peak frequency
Calculate the absolute values of FFT coefficients, find the index of the max, then map to frequency usingfreqs.Final Answer:
Code snippet A correctly computes the peak frequency -> Option AQuick Check:
Use rfft + rfftfreq with correct sampling interval [OK]
- Using fft and fftfreq for real signals (less efficient)
- Wrong sampling interval in rfftfreq
- Not mapping peak index to frequency
Ready to prove it?
Pass the test and this pattern moves unknown
