In this post, we implement Linear Convolution and Circular Convolution in MATLAB and plot both results side by side. Convolution is a fundamental operation in signal processing used to find the output of a Linear Time-Invariant (LTI) system when an input signal is applied to it.
Linear convolution computes the full convolution of two sequences, producing an output of length M + N - 1. Circular convolution (also called cyclic convolution) wraps around the result and is the basis of fast frequency-domain filtering using the DFT.
MATLAB Code
% Linear and Circular Convolution in MATLAB
% Reads two sequences from the user, computes both convolutions,
% displays the numerical results and plots all four signals.
clc; % Clear the command window
clear all; % Clear all workspace variables
% --- Input ---
x = input('Enter first sequence : '); % e.g. [1 1 2 2]
y = input('Enter second sequence : '); % e.g. [1 2 3 4]
% --- Linear Convolution ---
% conv() returns a vector of length length(x)+length(y)-1
z = conv(x, y);
disp('Linear convolution result:');
disp(z);
% --- Circular Convolution ---
% cconv(x, y) uses the length of the longer sequence by default
c = cconv(x, y);
disp('Circular convolution result:');
disp(c);
% --- Plot all four signals ---
figure;
subplot(4, 1, 1);
stem(x);
title('First Input Sequence (x)');
xlabel('Sample Index');
ylabel('Amplitude');
subplot(4, 1, 2);
stem(y);
title('Second Input Sequence (y)');
xlabel('Sample Index');
ylabel('Amplitude');
subplot(4, 1, 3);
stem(z);
title('Linear Convolution (z = x * y)');
xlabel('Sample Index');
ylabel('Amplitude');
subplot(4, 1, 4);
stem(c);
title('Circular Convolution (c = x ⊛ y)');
xlabel('Sample Index');
ylabel('Amplitude');