3. To find linear and circular cross convolution.

 Code:

clc;

clear all;

close;

//linear convolution

x=[1,2,3,4,5];// First Signal

y=[1,2,1,2];// Second Signal

y=mtlb_fliplr(y);

nx=length(x);

ny=length(y);

N=nx+ny-1;

n1=0:nx-1;

n2=0:ny-1;

n=0:nx+ny-2;

X_lin=[x,zeros(1,ny-1)];

//append zeros to make length equal to nx+nh-1

Y_lin=[y,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

Y2_lin(i,j)=0;

else

Y2_lin(i,j)=Y_lin(i-j+1);

end

end

end

rxy_lin=Y2_lin*X_lin';

// matrix multiplication to obtain linear correlation

disp('The first signal x(n) is ');

disp(x);

disp('The second signal y(n) is ');

disp(y);

disp('The toeplitz matrix of y(n)to calculate linear correlation is ');

disp(Y2_lin);

disp('The linear correlation for x(n)and y(n) is ');

disp(rxy_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,y);

a.children.children.foreground=5;

xtitle('input signal y(n)','n','input');

subplot(2,2,3);

a=gca();

a.x_location="origin";

a.y_location="origin";

plot2d3(n,rxy_lin);

a.children.children.foreground=5;

xtitle('Linearly correlated signal','n','rxy(l)');

//Circular Correlation

M=max(nx,ny);

if nx<M then

X_circ=[x,zeros(1,M-nx)];

else

X_circ=x;

end

if ny<M then

Y_circ=[y,zeros(1,M-ny)];

else

Y_circ=y;

end

//Formation of toeplitz matrix

for i=1:M

for j=1:M

if (i-j+1)>0 &(i-j+1)<M+1 then

Y2_circ(i,j)=Y_circ(i-j+1);

elseif (i<j) then

Y2_circ(i,j)=Y_circ(i-j+1+M)

end

end

end

rxy_circ=Y2_circ*X_circ';

// Matrix multiplication to obtain circular correlation

disp('The toeplitz matrix of y(n) to calculate Circular correlation is ');

disp(Y2_circ);

disp('The Circular correlation for x(n)and y(n) is ');

disp(rxy_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