Bird
Raised Fist0
MATLABdata~30 mins

FFT and frequency analysis in MATLAB - Mini Project: Build & Apply

Choose your learning style10 modes available
FFT and frequency analysis
📖 Scenario: You are working with a simple sound signal recorded at a steady pace. You want to find out which frequencies are present in this signal by using the Fast Fourier Transform (FFT).
🎯 Goal: Build a MATLAB program that creates a sample signal, sets up the sampling frequency, computes the FFT of the signal, and then displays the frequency components.
📋 What You'll Learn
Create a time vector and a signal with two sine waves combined
Compute the length of the signal
Compute the FFT of the signal
Display the frequency components
💡 Why This Matters
🌍 Real World
FFT is used in audio processing, communications, and many engineering fields to analyze frequency content of signals.
💼 Career
Understanding FFT helps in roles like signal processing engineer, audio engineer, and data analyst working with time-series data.
Progress0 / 4 steps
1
Create the sample signal
Create a variable Fs and set it to 1000 to represent the sampling frequency in Hz. Then create a time vector t from 0 to 1 second with steps of 1/Fs. Finally, create a signal x that is the sum of two sine waves: one with frequency 50 Hz and one with frequency 120 Hz, both with amplitude 1.
MATLAB
Hint

Use sin(2*pi*frequency*t) to create sine waves.

2
Compute the length of the signal
Create a variable N and set it to the length of the signal x using the length function.
MATLAB
Hint

Use length(x) to find the number of samples.

3
Compute the FFT of the signal
Create a variable Y and set it to the FFT of the signal x using the fft function. Then create a variable P2 as the two-sided spectrum magnitude by taking the absolute value of Y divided by N. Next, create a variable P1 as the single-sided spectrum by taking the first half of P2 and multiplying all values except the first and last by 2.
MATLAB
Hint

Remember to normalize by dividing by N and adjust the single-sided spectrum.

4
Display the frequency components
Create a frequency vector f from 0 to Fs/2 with N/2+1 points using the colon operator and division. Then use plot(f, P1) to plot the single-sided amplitude spectrum. Finally, add title as 'Single-Sided Amplitude Spectrum of x', xlabel as 'Frequency (Hz)', and ylabel as '|P1(f)|'.
MATLAB
Hint

Use plot and label the axes clearly.

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