Bird
Raised Fist0
MATLABdata~10 mins

FFT and frequency analysis in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - FFT and frequency analysis
Start: Signal x(t)
↓
Apply FFT: X(f) = fft(x)
↓
Calculate Magnitude: abs(X)
↓
Calculate Frequency Axis
↓
Plot Magnitude vs Frequency
↓
Analyze Peaks for Frequencies
↓
End
The signal is transformed from time to frequency using FFT, then magnitude and frequency axis are computed to analyze frequency components.
Execution Sample
MATLAB
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); % Signal
Y = fft(x); % Compute FFT
This code creates a signal with two sine waves and computes its FFT to analyze frequency content.
Execution Table
StepVariableValue/ExpressionDescription
1Fs1000Sampling frequency set to 1000 Hz
2T0.001Sampling period calculated as 1/Fs
3L1000Signal length set to 1000 samples
4t[0, 0.001, ..., 0.999]Time vector from 0 to (L-1)*T
5x0.7*sin(2*pi*50*t) + sin(2*pi*120*t)Signal composed of 50 Hz and 120 Hz sine waves
6Yfft(x)FFT computed, complex frequency components
7P2abs(Y)/LTwo-sided amplitude spectrum calculated
8P1P2(1:L/2+1)Single-sided amplitude spectrum extracted
9f(0:(L/2))*Fs/LFrequency axis vector created
10Plotplot(f,P1)Magnitude spectrum plotted vs frequency
11AnalysisIdentify peaks at 50 Hz and 120 HzFrequency components found in signal
12End-FFT and frequency analysis complete
💡 All steps executed to transform signal and analyze frequency content
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6After Step 7After Step 8After Step 9Final
Fsundefined1000100010001000100010001000
Tundefined0.0010.0010.0010.0010.0010.0010.001
Lundefined1000100010001000100010001000
tundefined[0,0.001,...0.999][0,0.001,...0.999][0,0.001,...0.999][0,0.001,...0.999][0,0.001,...0.999][0,0.001,...0.999][0,0.001,...0.999]
xundefinedundefined[signal values][signal values][signal values][signal values][signal values][signal values]
Yundefinedundefinedundefined[complex FFT values][complex FFT values][complex FFT values][complex FFT values][complex FFT values]
P2undefinedundefinedundefinedundefined[amplitude spectrum][amplitude spectrum][amplitude spectrum][amplitude spectrum]
P1undefinedundefinedundefinedundefinedundefined[single-sided spectrum][single-sided spectrum][single-sided spectrum]
fundefinedundefinedundefinedundefinedundefinedundefined[frequency vector][frequency vector]
Key Moments - 3 Insights
Why do we take only half of the FFT output for the single-sided spectrum?
Because the FFT output is symmetric for real signals, the second half is a mirror of the first half, so we only need the first half to represent all unique frequencies (see execution_table step 8).
Why do we divide the FFT output by the signal length L when calculating amplitude?
Dividing by L normalizes the FFT output so the amplitude matches the original signal's amplitude scale (see execution_table step 7).
How do we know which frequencies correspond to the FFT output indices?
We create a frequency vector 'f' that maps each FFT index to its frequency in Hz based on sampling rate and signal length (see execution_table step 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 8, what does P1 represent?
AThe single-sided amplitude spectrum
BThe two-sided amplitude spectrum
CThe time domain signal
DThe frequency vector
💡 Hint
Check execution_table step 8 description about P1
At which step is the frequency axis vector created?
AStep 6
BStep 7
CStep 9
DStep 10
💡 Hint
Look for frequency axis creation in execution_table
If the sampling frequency Fs is doubled, how does the frequency vector 'f' change?
AIt stays the same
BIt doubles all frequency values
CIt halves all frequency values
DIt becomes half the length
💡 Hint
Refer to variable_tracker for 'f' and how Fs affects it
Concept Snapshot
FFT and frequency analysis in MATLAB:
- Use fft(x) to convert time signal to frequency domain
- Normalize FFT by signal length L
- Extract single-sided spectrum for real signals
- Create frequency vector f = (0:(L/2))*Fs/L
- Plot magnitude vs frequency to find signal components
Full Transcript
This visual execution traces the steps of performing FFT and frequency analysis in MATLAB. Starting with a sampled time signal composed of two sine waves, the FFT is computed to transform the signal into frequency components. The amplitude spectrum is normalized by dividing by the signal length. Because the FFT output is symmetric for real signals, only the first half is used to represent unique frequencies. A frequency vector is created to map FFT indices to actual frequencies in Hertz. Finally, the magnitude spectrum is plotted against frequency to identify the signal's frequency components. Key points include normalization, single-sided spectrum extraction, and frequency axis calculation.

Practice

(1/5)
1. What does the MATLAB function fft() do to a signal?
easy
A. It amplifies the signal amplitude.
B. It converts the signal from time domain to frequency domain.
C. It smooths the signal to remove noise.
D. It shifts the signal in time.

Solution

  1. Step 1: Understand the purpose of FFT

    FFT stands for Fast Fourier Transform, which changes a signal from time domain to frequency domain.
  2. Step 2: Identify what fft() does in MATLAB

    In MATLAB, fft() computes the discrete Fourier transform, showing frequency components of the signal.
  3. Final Answer:

    It converts the signal from time domain to frequency domain. -> Option B
  4. Quick Check:

    FFT = frequency transform [OK]
Hint: FFT always changes time signals to frequency signals [OK]
Common Mistakes:
  • Thinking FFT smooths or filters signals
  • Confusing FFT with time shifting
  • Assuming FFT amplifies signal amplitude
2. Which of the following is the correct syntax to compute the FFT of a vector x in MATLAB?
easy
A. Y = fft x;
B. Y = fft[x];
C. Y = fft(x);
D. Y = fft{x};

Solution

  1. Step 1: Recall MATLAB function call syntax

    In MATLAB, functions are called with parentheses enclosing arguments, e.g., func(arg).
  2. 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.
  3. Final Answer:

    Y = fft(x); -> Option C
  4. Quick Check:

    Function calls use parentheses () [OK]
Hint: Use parentheses () for function calls in MATLAB [OK]
Common Mistakes:
  • Using square brackets [] instead of parentheses
  • Omitting parentheses around arguments
  • Using curly braces {} which are for cell arrays
3. Given the MATLAB code:
x = [1 1 0 0];
Y = fft(x);
absY = abs(Y);
disp(absY);

What is the output displayed?
medium
A. [1 1 1 1]
B. [2 0 2 0]
C. [4 0 0 0]
D. [2 1.4142 0 1.4142]

Solution

  1. 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.
  2. Step 2: Calculate absolute values

    Taking absolute values gives approximately [2, 1.4142, 0, 1.4142].
  3. Final Answer:

    [2 1.4142 0 1.4142] -> Option D
  4. Quick Check:

    FFT magnitude matches [2 1.4142 0 1.4142] [OK]
Hint: FFT magnitude shows signal strength at each frequency [OK]
Common Mistakes:
  • Confusing FFT output with original signal
  • Not taking absolute value before display
  • Assuming FFT output is all real numbers
4. Consider this MATLAB code snippet:
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?
medium
A. The frequency axis is missing for the plot, so frequencies are unclear.
B. The length of x is not used correctly in dividing Y.
C. The FFT function is called incorrectly without arguments.
D. The signal x is not defined properly.

Solution

  1. Step 1: Analyze the FFT and amplitude spectrum calculation

    The code correctly computes the single-sided amplitude spectrum P1 from Y.
  2. Step 2: Check the plot command

    The plot shows amplitude values but does not label or define the frequency axis, making interpretation difficult.
  3. Final Answer:

    The frequency axis is missing for the plot, so frequencies are unclear. -> Option A
  4. Quick Check:

    Plot frequency axis needed for clarity [OK]
Hint: Always plot frequency axis with amplitude spectrum [OK]
Common Mistakes:
  • Forgetting to create frequency vector for x-axis
  • Plotting amplitude without frequency labels
  • Misusing FFT input or output sizes
5. You have a noisy signal sampled at 1000 Hz stored in vector 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?
hard
A. 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');
B. Fs = 1000; Y = fft(data); plot(abs(Y)); xlabel('Frequency'); ylabel('Amplitude');
C. L = length(data); Y = fft(data); P1 = abs(Y); plot(P1);
D. Fs = 1000; t = 0:1/Fs:1; Y = fft(data); plot(Y);

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Option A code snippet -> Option A
  4. Quick Check:

    Proper FFT amplitude spectrum needs frequency axis and normalization [OK]
Hint: Normalize FFT and plot with frequency vector for correct spectrum [OK]
Common Mistakes:
  • Not normalizing FFT by signal length
  • Plotting full FFT without single-sided spectrum
  • Missing frequency axis in plot
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard