The conv()
function in MATLAB is used for convolution between two sequences. Convolution is a mathematical operation that is used to represent the output of a linear time-invariant system in response to an input signal. It is commonly used in signal processing and digital filtering.
The syntax of the conv()
function in MATLAB is as follows:
y = conv(x1, x2)
where x1
and x2
are the input sequences and y
is the output sequence obtained by convolving x1
and x2
.
An example of using conv() matlab function is to calculate the output y from the knowledge of the input signal x and the numerator of the low pass filter equation.
Consider the IIR filter equation,
\( H(z) = \frac{\sum_{k=0}^{M}b_k z^{-k}}{\sum_{k=0}^{N}a_k z^{-k}} = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + ... + b_n z^{-n}}{a_0 + a_1 z^{-1} + a_2 z^{-2} + ... + a_n z^{-n}}\) ---->(1)
If the input to this filter is x and the output is y then we can calculate the output y using the coefficients \(b_0,b_1, b_2..\) and the input x.
Let the filter be a low pass filter with cutoff frequency of 1kHz and order of the filter be 4. Let the input signal be mixture of sine wave with two frequencies 500Hz and 5kHz. Then the 5kHz sine wave is blocked and the signal with frequency of 500Hz is passed through.
This is illustrated in the following Matlab code.
% Designing a low-pass filter
fc = 1000; % Cutoff frequency in Hz
fs = 8000; % Sampling frequency in Hz
order = 4; % Filter order
% Compute filter coefficients using the Butterworth filter design
[b, a] = butter(order, fc/(fs/2), 'low');
% Generate input signal
t = 0:1/fs:100/fs; % Time vector
f1 = 500; % Frequency of first sinusoid
f2 = 5000; % Frequency of second sinusoid
x = sin(2*pi*f1*t) + sin(2*pi*f2*t); % Input signal
% Apply filter using the conv function
y = conv(x, b); % Convolve input signal with filter coefficients
% Plot input and output signals
subplot(2,1,1);
plot(t,x);
title('Input Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2,1,2);
plot(y);
title('Output Signal');
xlabel('Time (s)');
ylabel('Amplitude');
In this example, we first compute the filter coefficients using the
Butterworth filter design for a fourth-order low-pass filter with a
cutoff frequency of 1000 Hz. We then generate an input signal consisting
of two sinusoids with frequencies of 50 Hz and 500 Hz. Finally, we
apply the filter to the input signal using the conv()
function and plot the input and output signals. The output signal shows
that the high-frequency component of the input signal has been
attenuated by the filter, resulting in a smoother output signal.
b
coefficients are used to design a low-pass filter using the Butterworth filter design function butter()
. The b
coefficients correspond to the numerator coefficients of the transfer function of the filter.The a
coefficients, on the other hand, correspond to the denominator coefficients of the transfer function of the filter. For a low-pass filter, the a
coefficients are typically set to 1, which means that there is no feedback in the filter.
When using the conv()
function to apply the filter to an input signal, we only need the numerator coefficients (b
) because convolution is a linear operation and can be applied independently to the numerator and denominator polynomials of the transfer function. In other words, the denominator coefficients (a
) are not needed for the convolution operation, but they are necessary for other operations such as computing the frequency response of the filter using the freqz()
function.
Reference