Skip to content

MATLAB - 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, 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).

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 constants
syms 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 decay
L2 = laplace(exp(-a*t), t, s)
% Result: 1/(a + s)
% Transform of a sine wave
L3 = laplace(sin(w*t), t, s)
% Result: w/(s^2 + w^2)

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 wave
f2 = 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.

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 Transform
FT = 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 verify
inv_FT = ifourier(FT, w, x)
% Result: exp(-x^2)

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 Generation
Fs = 1000; % Sampling frequency in Hz
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
% Create a signal with two frequencies: 50 Hz and 120 Hz, plus noise
f1 = 50;
f2 = 120;
signal = 0.7*sin(2*pi*f1*t) + sin(2*pi*f2*t) + 2*randn(size(t));
% 2. Compute the FFT
Y = fft(signal);
% 3. Compute the two-sided spectrum, then the single-sided spectrum
P2 = 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 spectrum
f = 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 range

When 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.