Skip to content

MATLAB - Fourier Transform

The Fourier Transform is a cornerstone of signal processing, acting like a mathematical prism that decomposes a signal (like a sound wave over time) into its constituent frequencies. This allows you to analyze what frequencies are present in the signal and at what intensity. MATLAB provides powerful tools for both analytical (symbolic) and numerical Fourier Transforms.

The fourier function is part of the Symbolic Math Toolbox. It is used to find the exact analytical Fourier Transform of a mathematical expression. This is useful for theoretical work, not for processing measured numerical data.

F = fourier(f) % Transform f with default variables
F = fourier(f, u) % Use u as the transform variable instead of w
F = fourier(f, v, u) % Treat f as a function of v, transform to u
% Requires the Symbolic Math Toolbox
syms t w a
% Define the function f(t)
f = exp(-a * t^2);
% Calculate its Fourier Transform F(w)
F = fourier(f, t, w);
% Display the result
disp('Function f(t):');
disp(f);
disp('Fourier Transform F(w):');
disp(F);
% --- Output ---
% Function f(t):
% exp(-a*t^2)
%
% Fourier Transform F(w):
% (pi^(1/2)*exp(-w^2/(4*a)))/a^(1/2)

This result shows that the Fourier transform of a Gaussian function is another Gaussian function.

The fft (Fast Fourier Transform) function is the workhorse for practical signal processing. It computes the Discrete Fourier Transform (DFT) on numerical data, such as audio signals, sensor readings, or image pixels.

Y = fft(X)
Y = fft(X, n)
Y = fft(X, n, dim)
  • Y = fft(X): Computes the DFT of the vector or matrix X. If X is a matrix, it processes each column.
  • Y = fft(X, n): Computes the n-point DFT. The signal X is padded with zeros if it’s shorter than n or truncated if it’s longer.
  • Y = fft(X, n, dim): Computes the DFT along the specified dimension dim.

Let’s find the frequency components of a signal composed of a 50 Hz and a 120 Hz sine wave, corrupted by random noise. This is a common task in communications and data analysis.

%% Step 1: Create the Signal
Fs = 1000; % Sampling frequency (Hz)
T = 1/Fs; % Sampling period (s)
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
% Create a signal with two frequencies: 50 Hz and 120 Hz
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
% Add zero-mean random noise
X = S + 2*randn(size(t));
% Plot the noisy signal in the time domain
figure;
plot(1000*t(1:100), X(1:100)); % Plot first 100ms
title('Noisy Signal in Time Domain');
xlabel('t (milliseconds)');
ylabel('X(t)');
%% Step 2: Compute the FFT
Y = fft(X);
%% Step 3: Compute the Two-Sided and Single-Sided Spectrum
% The output of fft is complex and symmetric. We need to process it.
P2 = abs(Y/L); % Compute the two-sided spectrum P2
P1 = P2(1:L/2+1); % Compute the single-sided spectrum P1
P1(2:end-1) = 2*P1(2:end-1); % Double the power for all frequencies except DC and Nyquist
%% Step 4: Define the Frequency Domain and Plot
% Create the frequency vector for the x-axis
f = Fs*(0:(L/2))/L;
figure;
plot(f, P1);
title('Single-Sided Amplitude Spectrum of X(t)');
xlabel('f (Hz)');
ylabel('|P1(f)|');
grid on;
axis([0 200 0 1.2]); % Focus on the relevant frequency range

When you run this code, you will get two plots:

  1. A plot of the noisy, messy signal over time, where the underlying sine waves are hidden.
  2. A frequency spectrum plot with two clear peaks at 50 Hz and 120 Hz, revealing the original frequencies hidden in the noise. The peak at 120 Hz is taller, corresponding to its higher amplitude in the original signal.
  • Frequency Axis: Don’t plot the FFT output against the sample index 1:L. Always compute and use a proper frequency vector f as shown in the example.
  • Normalization: The raw output of fft scales with the signal length. Normalize by the length L to get the correct amplitude.
  • Single-Sided Spectrum: For real-valued signals, the FFT is symmetric. You typically only need to analyze the first half of the output (the single-sided spectrum). Remember to multiply the amplitudes by 2 to account for the energy from the other half.
  • fftshift: For some analyses, it’s useful to have the zero-frequency (DC) component in the center of the plot. The fftshift function can be used to rearrange the fft output for this purpose.