How to Plot Frequency Spectrum in MATLAB: Simple Guide
To plot the frequency spectrum in MATLAB, use the
fft function to compute the Fast Fourier Transform of your signal, then plot the magnitude against frequency using plot. You typically calculate the frequency axis based on the sampling rate and signal length to label the spectrum correctly.Syntax
The basic syntax to plot a frequency spectrum in MATLAB involves these steps:
Y = fft(x): Computes the Fast Fourier Transform of signalx.P2 = abs(Y/L): Calculates the two-sided amplitude spectrum, whereLis the length ofx.P1 = P2(1:L/2+1): Extracts the single-sided amplitude spectrum.f = Fs*(0:(L/2))/L: Creates the frequency axis, whereFsis the sampling frequency.plot(f, P1): Plots the frequency spectrum.
matlab
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; plot(f, P1);
Example
This example shows how to plot the frequency spectrum of a simple sine wave signal sampled at 1000 Hz.
matlab
Fs = 1000; % Sampling frequency T = 1/Fs; % Sampling period L = 1000; % Length of signal t = (0:L-1)*T; % Time vector % Create a signal with a 50 Hz sine wave x = 0.7*sin(2*pi*50*t); % Compute the FFT Y = fft(x); % Compute the two-sided spectrum P2 P2 = abs(Y/L); % Compute the single-sided spectrum P1 based on P2 and the even-valued signal length L P1 = P2(1:L/2+1); P1(2:end-1) = 2*P1(2:end-1); % Define the frequency domain f f = Fs*(0:(L/2))/L; % Plot the single-sided amplitude spectrum plot(f, P1) title('Single-Sided Amplitude Spectrum of X(t)') xlabel('Frequency (Hz)') ylabel('|P1(f)|') grid on;
Output
A plot window showing a peak at 50 Hz with amplitude about 0.7
Common Pitfalls
Common mistakes when plotting frequency spectrum in MATLAB include:
- Not normalizing the FFT output by the signal length, which leads to incorrect amplitude values.
- Plotting the two-sided spectrum instead of the single-sided spectrum for real signals, causing confusion.
- Incorrect frequency axis calculation, which mislabels the frequency peaks.
- Forgetting to multiply the single-sided spectrum values (except DC and Nyquist) by 2 to conserve signal energy.
matlab
%% Wrong way (no normalization and wrong frequency axis) Y = fft(x); plot(abs(Y)); %% Right way 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; plot(f, P1);
Quick Reference
Remember these key steps for plotting frequency spectrum in MATLAB:
- Use
fftto get frequency components. - Normalize by signal length
L. - Use single-sided spectrum for real signals.
- Calculate frequency axis with sampling rate
Fs. - Multiply amplitudes by 2 except for DC and Nyquist.
Key Takeaways
Use fft and normalize by signal length to get correct amplitude spectrum.
Plot the single-sided spectrum for real-valued signals to see meaningful frequencies.
Calculate the frequency axis using the sampling frequency and signal length.
Multiply amplitudes by 2 except for the first and last points to conserve energy.
Always label your plot axes clearly with frequency in Hz and amplitude.