MATLAB - Fourier Transform
MATLAB - Fourier Transform
Section titled “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.
1. Analytical Transform: fourier()
Section titled “1. Analytical Transform: fourier()”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.
Syntax
Section titled “Syntax”F = fourier(f) % Transform f with default variablesF = fourier(f, u) % Use u as the transform variable instead of wF = fourier(f, v, u) % Treat f as a function of v, transform to uExample: Transform of a Gaussian Function
Section titled “Example: Transform of a Gaussian Function”% Requires the Symbolic Math Toolboxsyms 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 resultdisp('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.
2. Numerical Transform: fft()
Section titled “2. Numerical Transform: fft()”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.
Syntax
Section titled “Syntax”Y = fft(X)Y = fft(X, n)Y = fft(X, n, dim)Syntax Explanation
Section titled “Syntax Explanation”Y = fft(X): Computes the DFT of the vector or matrixX. IfXis a matrix, it processes each column.Y = fft(X, n): Computes then-point DFT. The signalXis padded with zeros if it’s shorter thannor truncated if it’s longer.Y = fft(X, n, dim): Computes the DFT along the specified dimensiondim.
Mini-Project: Analyzing a Noisy Signal
Section titled “Mini-Project: Analyzing a Noisy Signal”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 SignalFs = 1000; % Sampling frequency (Hz)T = 1/Fs; % Sampling period (s)L = 1500; % Length of signalt = (0:L-1)*T; % Time vector
% Create a signal with two frequencies: 50 Hz and 120 HzS = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
% Add zero-mean random noiseX = S + 2*randn(size(t));
% Plot the noisy signal in the time domainfigure;plot(1000*t(1:100), X(1:100)); % Plot first 100mstitle('Noisy Signal in Time Domain');xlabel('t (milliseconds)');ylabel('X(t)');
%% Step 2: Compute the FFTY = 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 P2P1 = P2(1:L/2+1); % Compute the single-sided spectrum P1P1(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-axisf = 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 rangeWhen you run this code, you will get two plots:
- A plot of the noisy, messy signal over time, where the underlying sine waves are hidden.
- 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.
Common Pitfalls and Best Practices
Section titled “Common Pitfalls and Best Practices”- Frequency Axis: Don’t plot the FFT output against the sample index
1:L. Always compute and use a proper frequency vectorfas shown in the example. - Normalization: The raw output of
fftscales with the signal length. Normalize by the lengthLto 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. Thefftshiftfunction can be used to rearrange thefftoutput for this purpose.