You are on page 1of 5

EXPERIMENT No.

15
P2.18)Determine and sketch the convolution y(n) of the signals analytically,i.e.
without using the “conv” command in matlab

%---------------------------------------------------------------------------------------------
% PROGRAM FOR COMPUTING CONVOLUTION ANALYTICALLY
% given x(n)=(1/3)*n; 0<=n<=6
% h(n)=1; -2<=n<=2
%--------------------------------------------------------------------------------------------

clc, clear all; close all;


nx=0:6;%given length of input sequence
nh=-2:2;%given length of impulse response
x=(1/3).*nx;
h=ones(1,length(nh));
disp('nx:');disp(nx)
disp('x(nx):');disp(x)
disp('nh:');disp(nh)
disp('h(nh):');disp(h)

subplot(3,1,1)
stem(nx,x);
xlabel('0<=n<=6'); ylabel('x(n)=(1/3)*n');
title('x(n)');
subplot(3,1,2)
stem(nh,h);
xlabel('-2<=n<=2');% ylabel('h(n)');
title('h(n)');

ny=length(nx)+length(nh)-1;
y=zeros(1,ny);
x=[x,zeros(1,length(nh))];%padding zeros
h=[h,zeros(1,length(nx))];%padding zeros
%------------- calculating convolution ---------------------------
for i=1:length(nh)+length(nx)-1
for j=1:length(nx)
if(i-j+1>0)
y(i)=y(i)+x(j)*h(i-j+1);
end
end
end
%---------------------- plotting of y(n)-------------------------------
nyb=nx(1)+nh(1);%beginning point of y(n)
nye=nx(end)+nh(end);%end point of y(n)
n=nyb:nye;
subplot(3,1,3);
stem(n,y);
title('y(n)=x(n)*h(n)')
xlabel('n'); ylabel('y(n)');
disp('n:');disp(n)
disp('y(n):');disp(y)

RESULT
nx:
0 1 2 3 4 5 6

x(nx):
0 0.3333 0.6667 1.0000 1.3333 1.6667 2.0000
nh:
-2 -1 0 1 2

h(nh):
1 1 1 1 1

n:
-2 -1 0 1 2 3 4 5 6 7 8

y(n):
Columns 1 through 10

0 0.3333 1.0000 2.0000 3.3333 5.0000 6.6667 6.0000 5.0000


3.6667

Column 11

2.0000
SKExample 2.6

Consider the following two sequences of length 5 defined for 0<=n<=4:


c(n)={ 3.2, 41, 36, -9.5, 0};
d[n]=[1.7, -0.5, 0, 0.8, 1};
Perform basic operations such as,
1. Product operation (modulation) [c (n) * d (n)].
2. Addition [c (n) + d (n)].
3. Scalar multiplication) [(7/2)*c (n)].

% -----------------------------------------------------------------------------------------------
% PROGRAM TO ANALYZE THE OPERATIONS ON SEQUENCES
% given c[n]={3.2,41,36,-9.5,0}; 0<=n<=4;
% d[n]=[1.7,-0.5,0,0.8,1}; 0<=n<=4
% ----------------------------------------------------------------------------------------------
clc,clear all;close all;
n=0:4;
c=[3.2,41,36,-9.5,0];
d=[1.7,-0.5,0,0.8,1];
fprintf(1,'n\t:');
fprintf(1,'%g\t',n);
fprintf(1,'\nc(n):');
fprintf(1,'%g\t',c);
fprintf(1,'\nd(n):');
fprintf(1,'%g\t',d);
%--------------------- basic operations ----------------------------------
% ---------------- modulation -----------------------------
% ---------------- addition -----------------------------
% ---------------- multiplication -----------------------------
% ----------------------------------------------------------------------------
w1=c.*d;
fprintf(1,'\n\n');
disp('Modulation:-w1(n)=c(n)*d(n):');disp(w1)
w2=c+d;
disp('Addition:-w2(n)=c(n)+d(n):');disp(w2)
w3=(7/2).*c;
disp('Multiplication:-w1(n)=(7/2)*c(n):');disp(w3)

RESULT

n :0 1 2 3 4
c(n):3.2 41 36 -9.5 0
d(n):1.7 -0.5 0 0.8 1

Modulation:-w1(n)=c(n)*d(n):
5.4400 -20.5000 0 -7.6000 0

Addition:-w2(n)=c(n)+d(n):
4.9000 40.5000 36.0000 -8.7000 1.0000

Multiplication:-w1(n)=(7/2)*c(n):
11.2000 143.5000 126.0000 -33.2500 0

You might also like