How to Use FFT in MATLAB: Syntax, Example, and Tips
In MATLAB, use the
fft function to compute the fast Fourier transform of a signal. Call Y = fft(x) where x is your input data vector, and Y is the frequency domain result.Syntax
The basic syntax for the fast Fourier transform in MATLAB is:
Y = fft(x): Computes the FFT of vectorx.Y = fft(x, n): Computes the FFT with lengthn, padding or truncatingxas needed.Y = fft(x, [], dim): Computes FFT along dimensiondimof arrayx.
Explanation: x is your input data, usually a time-domain signal. Y is the complex frequency-domain output. The optional n controls the FFT length, and dim specifies which dimension to transform for multi-dimensional data.
matlab
Y = fft(x) Y = fft(x, n) Y = fft(x, [], dim)
Example
This example shows how to compute the FFT of a simple sine wave and plot its magnitude spectrum.
matlab
Fs = 1000; % Sampling frequency in Hz T = 1/Fs; % Sampling period L = 1000; % Length of signal t = (0:L-1)*T; % Time vector f = 50; % Frequency of sine wave x = 0.7*sin(2*pi*f*t); % Generate sine wave Y = fft(x); % Compute FFT P2 = abs(Y/L); % Two-sided spectrum magnitude P1 = P2(1:L/2+1); % Single-sided spectrum P1(2:end-1) = 2*P1(2:end-1); % Adjust amplitude f_axis = Fs*(0:(L/2))/L; % Frequency axis plot(f_axis, P1) title('Single-Sided Amplitude Spectrum of x(t)') xlabel('Frequency (Hz)') ylabel('|P1(f)|')
Output
A plot window showing a peak at 50 Hz in the amplitude spectrum
Common Pitfalls
Common mistakes when using fft in MATLAB include:
- Not normalizing the FFT output, which can lead to incorrect amplitude interpretation.
- Ignoring that FFT output is complex; you usually want the magnitude
abs(Y)or power spectrum. - Not using the single-sided spectrum for real signals, which doubles the amplitude except at DC and Nyquist.
- Confusing the frequency axis scale; you must create it based on sampling frequency and FFT length.
matlab
%% Wrong: Plot raw FFT output without magnitude or normalization Y_wrong = fft(x); plot(abs(Y_wrong)) title('Incorrect FFT Plot') %% Right: Plot normalized magnitude spectrum P2 = abs(Y_wrong/L); P1 = P2(1:L/2+1); P1(2:end-1) = 2*P1(2:end-1); plot(f_axis, P1) title('Correct Single-Sided Amplitude Spectrum')
Output
First plot shows magnitude of complex values (not normalized), second plot shows correct amplitude spectrum
Quick Reference
Remember these tips when using fft in MATLAB:
- Use
abs(fft(x))to get magnitude. - Normalize by dividing by signal length for correct amplitude.
- Use single-sided spectrum for real signals.
- Generate frequency axis with
Fs*(0:(N/2))/N.
Key Takeaways
Use
fft(x) to compute the fast Fourier transform of signal x in MATLAB.Normalize the FFT output by signal length and use the magnitude
abs(Y) for correct amplitude.For real signals, use the single-sided spectrum by taking the first half of the FFT result and doubling amplitudes except at DC and Nyquist.
Create the frequency axis based on sampling frequency and FFT length to interpret the spectrum correctly.
Avoid plotting raw complex FFT output without processing, as it is not meaningful visually.