4. To perform linear convolution from circular convolution and vice versa
Code:
clc;
clear all;
close;
//input sequences
x=[1,2,0,3,4,1];
h=[2,0,1,3,1];
//find length of input sequences
nx=length(x);
nh=length(h);
N=nx+nh-1; // Length of linear convolution
M=max(nx,nh); // Length of circular convolution
Y1=conv(x,h);//Linear convolution
//Circular convolution
x_len=[x,zeros(1,M-nx)];
h_len=[h,zeros(1,M-nh)];
DFT_x=fft(x_len);
DFT_h=fft(h_len);
DFT_xh=DFT_x.*DFT_h;
circ_xh=ifft(DFT_xh);
//Circular Convolution from linear convolution
for i=1:M
if (i<=(N-M)) then
Y2 (i)=Y1(i)+Y1(i+M);
else
Y2(i)=Y1(i);
end
end
//linear cnvolution from circular convolution
X=[x,zeros(1,nh-1)];
H=[h,zeros(1,nx-1)];
x_dft=fft(X);
h_dft=fft(H);
xh_dft=x_dft.*h_dft;
Y3= ifft(xh_dft);
//Display various sequences on console window
disp(x,"Input Sequence x");
disp(h,"Input Sequence h");
disp(Y1,"Linearly Convolved sequence using conv function");
disp(circ_xh,"Circularly Convolved sequence using fft
function");
disp(Y3," Linear Convolution from Circular Convolution")
disp(Y2',"Circular Convolution from Linear Convolution")
//Plot various sequences
subplot(2,2,1);
xlabel("Time");
ylabel("Amplitude");
title("Input Sequence x(n)")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(x);
subplot(2,2,2);
xlabel("Time");
ylabel("Amplitude");
title("Input Sequence h(n)")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(h);
subplot(2,2,3);
xlabel("Time");
ylabel("Amplitude");
title("Circular Convolution from Linear Convolution")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(Y2);
subplot(2,2,4);
xlabel("Time");
ylabel("Amplitude");
title("Linear Convolution from Circular Convolution")
a=gca();
a.x_location="origin";
a.y_location="origin";
plot2d3(Y3);
Comments
Post a Comment