Bird
Raised Fist0
MATLABdata~20 mins

FFT and frequency analysis in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FFT Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of FFT Magnitude Calculation
What is the output of the following MATLAB code snippet that computes the magnitude of the FFT of a simple signal?
MATLAB
Fs = 8; % Sampling frequency
T = 1/Fs; % Sampling period
L = 8; % Length of signal
t = (0:L-1)*T; % Time vector
x = cos(2*pi*1*t) + 0.5*cos(2*pi*3*t);
Y = fft(x);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
disp(P1);
A[1 0.75 0.5 0.25 0.25]
B[0 1 0 0.5 0]
C[0.75 1 0.5 0.25 0.25]
D[0.5 0.75 1 0.25 0.25]
Attempts:
2 left
💡 Hint
Remember the FFT output is symmetric and the magnitude is scaled by the length of the signal.
🧠 Conceptual
intermediate
1:30remaining
Frequency Resolution in FFT
If you have a signal sampled at 1000 Hz and you take an FFT of length 500, what is the frequency resolution of the FFT?
A2 Hz
B1000 Hz
C0.5 Hz
D500 Hz
Attempts:
2 left
💡 Hint
Frequency resolution is sampling frequency divided by FFT length.
🔧 Debug
advanced
2:00remaining
Identify the Error in FFT Frequency Vector Calculation
What error does the following MATLAB code produce when trying to create a frequency vector for an FFT of length N sampled at Fs?
MATLAB
N = 16;
Fs = 100;
f = (0:N-1)*(Fs/N);
disp(f);
ANo error; frequency vector is correct for two-sided FFT
BRuntime error: index exceeds matrix dimensions
CSyntax error due to missing semicolon
DFrequency vector exceeds Nyquist frequency, should be limited to Fs/2
Attempts:
2 left
💡 Hint
Consider the meaning of Nyquist frequency and how frequency vectors are usually constructed for FFT plots.
📝 Syntax
advanced
1:30remaining
Syntax Error in FFT Magnitude Plotting
Which option contains the correct syntax to plot the single-sided amplitude spectrum of a signal x sampled at Fs using FFT in MATLAB?
MATLAB
x = sin(2*pi*50*t) + sin(2*pi*120*t);
Fs = 1000;
L = length(x);
Y = fft(x);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
Aplot(f, P2); xlabel('Frequency (Hz)'); ylabel('Amplitude');
Bplot(f, abs(P1)); xlabel('Frequency (Hz)'); ylabel('Amplitude');
Cplot(f, P1); xlabel('Frequency (Hz)'); ylabel('Amplitude');
Dplot(f, P1); xlabel('Frequency'); ylabel('Amplitude Spectrum');
Attempts:
2 left
💡 Hint
Check that the plotted vector matches the frequency vector and labels are clear.
🚀 Application
expert
2:30remaining
Determine the Number of Frequency Components
Given a real-valued signal sampled at 2000 Hz with length 1024, after computing the FFT and taking the single-sided spectrum, how many unique frequency components are present in the single-sided amplitude spectrum?
A513
B1024
C512
D1023
Attempts:
2 left
💡 Hint
For real signals, the single-sided spectrum length is N/2 + 1.

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