FFT helps us find hidden repeating patterns or frequencies in data. It turns time-based data into frequency-based data so we can understand signals better.
FFT and frequency analysis in MATLAB
Y = fft(X) % X is your input data (like a signal) % Y is the output with frequency information
The input X can be a vector or matrix of numbers.
The output Y is complex numbers showing amplitude and phase of frequencies.
Y = fft([1 2 3 4])
Y = fft(signal, 1024)P2 = abs(Y/length(Y)) P1 = P2(1:length(Y)/2+1) P1(2:end-1) = 2*P1(2:end-1);
This program creates a noisy signal with two known frequencies. It uses FFT to find the frequency components and plots the amplitude spectrum showing peaks at those frequencies.
% Sample signal: sum of two sine waves Fs = 1000; % Sampling frequency in Hz T = 1/Fs; % Sampling period L = 1500; % Length of signal t = (0:L-1)*T; % Time vector % Create a signal with two frequencies 50 Hz and 120 Hz S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); % Add some noise X = S + 2*randn(size(t)); % Compute FFT Y = fft(X); % Compute two-sided spectrum P2 P2 = abs(Y/L); % Compute single-sided spectrum P1 based on P2 and even-valued L P1 = P2(1:L/2+1); P1(2:end-1) = 2*P1(2:end-1); % Define frequency domain f f = Fs*(0:(L/2))/L; % Plot single-sided amplitude spectrum plot(f,P1) title('Single-Sided Amplitude Spectrum of X(t)') xlabel('Frequency (Hz)') ylabel('|P1(f)|')
FFT output is complex; use abs() to get amplitude.
Frequency resolution depends on sampling rate and signal length.
Plotting the single-sided spectrum helps see positive frequencies clearly.
FFT converts time signals into frequency components.
Use fft() in MATLAB to perform frequency analysis.
Amplitude spectrum shows strength of each frequency in the signal.
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
