Saturday, July 2, 2011

MATLAB Programs 08: Finding the linear convolution of two sequences



In matlab conv() function is used for finding the linear convolution of sequences. The program is shown below.


%Program to find the linear convolution of two sequences

x1=input('Enter the first sequence x1(n) = ');
t1=input('Enter the starting time of first sequence t1 = ');
x2=input('Enter the second sequence x2(n) = ');
t2=input('Enter the starting time of second sequence t2 = ');
l1=length(x1);
l2=length(x2);
ln=l1+l2-1;

yn=conv(x1,x2);

a=t1+l1-1;
t=t1:a;
subplot(311);
stem(t,x1);
grid on;
xlabel('time--->');
ylabel('amplitude--->');
TITLE('First sequence');

a=t2+l2-1;
t=t2:a;
subplot(312);
stem(t,x2);
grid on;
xlabel('time--->');
ylabel('amplitude--->');
TITLE('Second sequence');

tn=t1+t2;
a=tn+ln-1;
t=tn:a;
subplot(313);
stem(t,yn);
grid on;
xlabel('time--->');
ylabel('amplitude--->');
TITLE('Convolved output');

%output

%Enter the first sequence x1(n) = [1 2 6 2 3 1 4]
%Enter the starting time of first sequence t1 = -3
%Enter the second sequence x2(n) = [3 1 4 5 2]
%Enter the starting time of second sequence t2 = -1





7 comments:

Sai Bond said...

Its works perfect bingo!...

Anonymous said...

very nice program ....thanks alot

sanath kumar bp said...

Nice that you have consider the origin.

Anonymous said...

this program really works........thanks a lot !!!!!!!!!!!!

Anonymous said...

thanks a lot (y)

Anonymous said...

what if the starting line is not the given instead the position in sequence x where n=0 is the given?

MOTHILAL said...

i want the convolution code for convolution x1=a1*sin(2*pi*f1*t) and x2=a2*sin(2*pi*f2*t) a1=1,a2=2,f1=100,f2=500,

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes