MATLAB - Transforms
MATLAB - Mathematical Transforms
Section titled “MATLAB - Mathematical Transforms”Mathematical transforms are a cornerstone of science and engineering, providing a powerful way to shift a problem into a different domain where it might be easier to solve or analyze. MATLAB offers robust tools for both symbolic and numeric transforms.
For example, the Fourier transform converts a signal from the time domain to the frequency domain, revealing its frequency components. The Laplace transform converts a linear ordinary differential equation (ODE) into an algebraic equation, which is much simpler to solve.
The Laplace Transform (Symbolic)
Section titled “The Laplace Transform (Symbolic)”The Laplace transform, performed with the laplace function from the Symbolic Math Toolbox, is used for analytically solving differential equations. It transforms a function f(t) into F(s).
Example: Forward Laplace Transform
Section titled “Example: Forward Laplace Transform”Let’s compute the Laplace transform of some common functions. Note the use of syms to declare symbolic variables.
% Declare symbolic variables t (time), s (Laplace variable), and constantssyms t s a w
% Transform of a constant 'a'L1 = laplace(a, t, s) % Explicitly state transform from t to s% Result: a/s
% Transform of an exponential decayL2 = laplace(exp(-a*t), t, s)% Result: 1/(a + s)
% Transform of a sine waveL3 = laplace(sin(w*t), t, s)% Result: w/(s^2 + w^2)Example: Inverse Laplace Transform
Section titled “Example: Inverse Laplace Transform”The ilaplace function converts a function F(s) back to f(t). This is often the final step after solving an equation in the s-domain.
syms s t w
% Inverse transform of a simple pole (corresponds to exponential decay)f1 = ilaplace(1/(s + 5), s, t)% Result: exp(-5*t)
% Inverse transform of a term corresponding to a cosine wavef2 = ilaplace(s/(s^2 + w^2), s, t)% Result: cos(t*w)The Fourier Transform: Symbolic vs. Numeric
Section titled “The Fourier Transform: Symbolic vs. Numeric”The Fourier Transform has two primary applications in MATLAB: the symbolic fourier transform for analytical work, and the numeric Fast Fourier Transform (fft) for data analysis, which is far more common in practice.
Symbolic Fourier Transform (fourier)
Section titled “Symbolic Fourier Transform (fourier)”Similar to laplace, the fourier function operates on symbolic expressions. It’s useful for finding the analytical transform of a mathematical function.
syms x w
% Define a Gaussian function (bell curve)f = exp(-x^2);
% Find its Fourier TransformFT = fourier(f, x, w)% Result: pi^(1/2)*exp(-w^2/4)
% The Fourier transform of a Gaussian is another Gaussian.% We can find the inverse transform to verifyinv_FT = ifourier(FT, w, x)% Result: exp(-x^2)Numeric Fast Fourier Transform (fft)
Section titled “Numeric Fast Fourier Transform (fft)”The fft function is a highly optimized algorithm for computing the Discrete Fourier Transform of a numeric array (like a recorded audio signal or sensor data). It is one of the most important algorithms in digital signal processing.
Practical Example: Finding Frequencies in a Signal
Section titled “Practical Example: Finding Frequencies in a Signal”Imagine we have a signal composed of a 50 Hz and a 120 Hz sine wave, corrupted by noise. Let’s use fft to identify these original frequencies.
% 1. Signal GenerationFs = 1000; % Sampling frequency in HzT = 1/Fs; % Sampling periodL = 1500; % Length of signalt = (0:L-1)*T; % Time vector
% Create a signal with two frequencies: 50 Hz and 120 Hz, plus noisef1 = 50;f2 = 120;signal = 0.7*sin(2*pi*f1*t) + sin(2*pi*f2*t) + 2*randn(size(t));
% 2. Compute the FFTY = fft(signal);
% 3. Compute the two-sided spectrum, then the single-sided spectrumP2 = abs(Y/L);P1 = P2(1:L/2+1);P1(2:end-1) = 2*P1(2:end-1);
% 4. Define the frequency domain and plot the single-sided amplitude spectrumf = Fs*(0:(L/2))/L;figure;plot(f, P1);title('Single-Sided Amplitude Spectrum of the Signal');xlabel('Frequency (f) [Hz]');ylabel('|P1(f)|');axis([0 200 0 1.2]); % Focus on the relevant frequency rangeWhen you run this code, it will generate a plot of the frequency spectrum. Despite the noise in the original signal, you will see two distinct sharp peaks on the plot. One peak will be located at 50 Hz on the x-axis, and the other at 120 Hz, clearly identifying the constituent frequencies of the original signal.