FFT and frequency analysis in MATLAB - Time & Space Complexity
We want to understand how the time needed to perform FFT changes as the input size grows.
How does the number of steps grow when we analyze more data points?
Analyze the time complexity of the following code snippet.
N = 1024;
x = rand(1, N);
X = fft(x);
This code generates a random signal of length N and computes its FFT to analyze frequency components.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The FFT algorithm repeatedly combines pairs of data points through stages.
- How many times: The process repeats about log2(N) times, each stage processing all N points.
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 33 |
| 100 | About 664 |
| 1000 | About 9966 |
Pattern observation: The operations grow roughly like n times log n, which is slower than n squared but faster than just n.
Time Complexity: O(n log n)
This means the time grows a bit faster than the input size but much slower than checking every pair of points.
[X] Wrong: "FFT takes as long as multiplying every pair of points, so it is O(n²)."
[OK] Correct: FFT cleverly reuses calculations in stages, so it avoids doing all pairs and runs much faster.
Knowing how FFT scales helps you explain efficient signal processing and shows you understand smart algorithms that save time.
"What if we used a simple discrete Fourier transform (DFT) instead of FFT? How would the time complexity change?"
Practice
fft() do to a signal?Solution
Step 1: Understand the purpose of FFT
FFT stands for Fast Fourier Transform, which changes a signal from time domain to frequency domain.Step 2: Identify what
In MATLAB,fft()does in MATLABfft()computes the discrete Fourier transform, showing frequency components of the signal.Final Answer:
It converts the signal from time domain to frequency domain. -> Option BQuick Check:
FFT = frequency transform [OK]
- Thinking FFT smooths or filters signals
- Confusing FFT with time shifting
- Assuming FFT amplifies signal amplitude
x in MATLAB?Solution
Step 1: Recall MATLAB function call syntax
In MATLAB, functions are called with parentheses enclosing arguments, e.g.,func(arg).Step 2: Check each option's syntax
Y = fft(x); uses correct syntax:fft(x). Options A, C, and D use invalid syntax for function calls in MATLAB.Final Answer:
Y = fft(x); -> Option CQuick Check:
Function calls use parentheses () [OK]
- Using square brackets [] instead of parentheses
- Omitting parentheses around arguments
- Using curly braces {} which are for cell arrays
x = [1 1 0 0];
Y = fft(x);
absY = abs(Y);
disp(absY);
What is the output displayed?
Solution
Step 1: Compute FFT of x = [1 1 0 0]
The FFT of this sequence results in complex values whose magnitudes are [2, sqrt(2), 0, sqrt(2)] approximately.Step 2: Calculate absolute values
Taking absolute values gives approximately [2, 1.4142, 0, 1.4142].Final Answer:
[2 1.4142 0 1.4142] -> Option DQuick Check:
FFT magnitude matches [2 1.4142 0 1.4142] [OK]
- Confusing FFT output with original signal
- Not taking absolute value before display
- Assuming FFT output is all real numbers
Fs = 1000;
t = 0:1/Fs:1-1/Fs;
x = cos(2*pi*50*t) + cos(2*pi*120*t);
Y = fft(x);
P2 = abs(Y/length(x));
P1 = P2(1:length(x)/2+1);
P1(2:end-1) = 2*P1(2:end-1);
plot(P1);
What is the likely error in this code?
Solution
Step 1: Analyze the FFT and amplitude spectrum calculation
The code correctly computes the single-sided amplitude spectrum P1 from Y.Step 2: Check the plot command
The plot shows amplitude values but does not label or define the frequency axis, making interpretation difficult.Final Answer:
The frequency axis is missing for the plot, so frequencies are unclear. -> Option AQuick Check:
Plot frequency axis needed for clarity [OK]
- Forgetting to create frequency vector for x-axis
- Plotting amplitude without frequency labels
- Misusing FFT input or output sizes
data. You want to find the dominant frequency component using FFT. Which MATLAB code snippet correctly computes and plots the single-sided amplitude spectrum with frequency axis?Solution
Step 1: Understand the steps to compute single-sided amplitude spectrum
Calculate FFT, normalize by length, create frequency vector, extract half spectrum, double amplitudes except DC and Nyquist.Step 2: Check which option follows these steps correctly
L = length(data); Fs = 1000; Y = fft(data); P2 = abs(Y/L); f = Fs*(0:(L/2))/L; P1 = P2(1:L/2+1); P1(2:end-1) = 2*P1(2:end-1); plot(f,P1); xlabel('Frequency (Hz)'); ylabel('Amplitude'); correctly computes length, frequency vector, normalizes, adjusts amplitudes, and plots with labels. Others miss frequency axis or normalization.Final Answer:
Option A code snippet -> Option AQuick Check:
Proper FFT amplitude spectrum needs frequency axis and normalization [OK]
- Not normalizing FFT by signal length
- Plotting full FFT without single-sided spectrum
- Missing frequency axis in plot
Ready to prove it?
Pass the test and this pattern moves unknown
