Sunday, 21 October 2012

Linear vs Circular Convolution in MATLAB



Just create a new file with extension .m and paste the code below in that .m file. Then Run it.

clear all;
clc;
x=input('enter the sequence h(n) : ');
h=input('enter the sequence x(n) : ');
%starting of circular convolution
l_x=length(x);
l_h=length(h);
l3=l_x+l_h-1;
n1=0:1:l_x-1;
n2=0:1:l_h-1;
n3=0:1:l3-1;
y=conv(x,h);
figure(1)

subplot(4,1,1);
stem(x);
xlabel('time');
ylabel('amplitude');
title('original signal x(n)');
subplot(4,1,2);
stem(h);
xlabel('time');
ylabel('amplitude');
title('original signal h(n)');
subplot(4,1,3);
stem(n3,y);
xlabel('time');
ylabel('amlpitude');
title('Linear Convoluted Signal');
Linear_Convoluted_Signal=y;
Linear_Convoluted_Signal
%starting of circular convolution
l=max(l_x,l_h);
x=[x,zeros(1,l-l_x)];
h=[h,zeros(1,l-l_h)];
j=0;
for s=1:1:l
    g(s)=0;
    j=j+1;
    for i=1:l
            if j<1
                j=l;
            end
        g(s)=g(s)+x(i)*h(j);
       j=j-1;
    end
end
Circular_Convoluted_Signal=g;
Circular_Convoluted_Signal
subplot(4,1,4);
stem(g);
xlabel('time');
ylabel('amplitude');
title('Circular Convoluted Signal');

No comments:

Post a Comment

Tech N Science © 2013