2. To find linear and circular convolution.
Code:
clc;
clear all;
close;
//linear convolution
x=[1,2,3,4,5]; // First Signal
h=[1,2,1,2]; // Second Signal
nx=length(x);
nh=length(h);
N=nx+nh-1;
n1=0:nx-1;
n2=0:nh-1;
n=0:nx+nh-2;
X_lin=[x,zeros(1,nh-1)];
//append zeros to make length equal to nx+nh-1
H_lin=[h,zeros(1,(nx-1))];
//append zeros to make length equal to nx+nh-1
// Formation of Toeplitz Matrix
for i=1:N
for j=1:N
if (i<j) then
X2_lin(i,j)=0;
else
X2_lin(i,j)=X_lin(i-j+1);
end
end
end
Y_lin=X2_lin*H_lin';// matrix multiplication to obtain linear convolution
disp('The first signal x(n) is ');
disp(x);
disp('The second signal h(n) is ');
disp(h);
disp('The toeplitz matrix of x(n) to calculate linear convolution is ');
disp(X2_lin);
disp('The linear convolution for x(n)and h(n) is ');
disp(Y_lin');
//Plotting of the signals
figure(1);
subplot(2,2,1);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n1,x);
a.children.children.foreground=5;
xtitle('input signal x(n)','n','input');
subplot(2,2,2);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n2,h);
a.children.children.foreground=5;
xtitle('input signal h(n)','n','input');
subplot(2,2,3);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(n,Y_lin);
a.children.children.foreground=5;
xtitle('Linearly convolved signal','n','Y(n)');
//Circular Convolution
M=max(nx,nh);
X_circ=[x,zeros(1,M-nx)];
H_circ=[h,zeros(1,M-nh)];
//Formation of toeplitz matrix
for i=1:M
for j=1:M
if (i-j+1)>0 &(i-j+1)<M+1 then
X2_circ(i,j)=X_circ(i-j+1);
elseif (i<j) then
X2_circ(i,j)=X_circ(i-j+1+M)
end
end
end
Y_circ=X2_circ*H_circ';
// Matrix multiplication to obtain circular convolution
disp('The toeplitz matrix of x(n) to calculate Circular convolution is
');
disp(X2_circ);
disp('The Circular convolution for x(n)and h(n) is ');
disp(Y_circ');
//Plotting of the signals
m=0:M-1;
subplot(2,2,4);
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(m,Y_circ);
a.children.children.foreground=5;
xtitle('Circilarly convolved signal','n','Y(n)');
Comments
Post a Comment