Pages

Thursday 21 February 2013

Linear Convolution Without Using Function (Matlab 2012a)

Program:

clc;
clear all;
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;
X1 = [x1,zeros(1,l2)];
X2 = [x2,zeros(1,l1)];
for i = 1:l2+l1-1
y(i) = 0;
for j = 1:l1
if(i-j+1>0)
y(i) = y(i)+X1(j)*X2(i-j+1);
else
end
end
end
a = t1+l1-1;
t = t1:a;
subplot(3,1,1);
stem(t,x1);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('First Sequence');
a = t2+l2-1;
t = t2:a;
subplot(3,1,2);
stem(t,x2);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Second Sequence');
tn = t1+t2;
a = tn+ln-1;
t = tn:a;
subplot(3,1,3);
disp('Convolved Sequence = ');
disp(y);
stem(t,y);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Convolved Output');


Output: 

Enter the first sequence x1(n) = [1 2 1 0]
Enter the starting time of first sequence t1 = 0
Enter the second sequence x2(n) = [1 0 1]
Enter the starting time of second sequence t2 = 0
Convolved Sequence =
     1     2     2     2     1     0


Waveforms:

1 comment: