lab#03_ns umair zulfiqar
DESCRIPTION
DSP LabTRANSCRIPT
LAB # 03
SUBMITTED BY: NS UMAIR ZULFIQAR
a) Write a program where let x[n]={1,2,3,4,5,6,7,6,5,4,3,2,1}, the plot the following
X1(n)= 2 x(n-5)-3x(n+4)
X2(n)=x(3-n)+2 x(n-2)
x=[ 1 2 3 4 5 6 7 6 5 4 3 2 1];
t=-4:17;
x1=[zeros(1,5) x];
x2=[x zeros(1,5)];
a=2*x1;
b=3*x2;
y1=a-b;
y=[y1 zeros(1, length(t)-length(y1))];
stem(t,y)
Learning: The shift operation can be performed by zero padding. The reference point of the signal vector hence got
changed when plotted against the same time vector. To shift to right side add zeros on left and vice versa.
b) Write a MATLAB code for convolution of two signals.(without using “conv”
command)
a=[1 0 0 0 0];
h=[1 1 1 1];
a1=length(a);
h1=length(h);
y=zeros(1, a1+h1-1);
h2=[h 0 0 0 0];
a1=[a 0 0 0];
for i=1:length(y)
for j=1:length(y)
if j<i+1;
y(i)= y(i)+ a1(j)*h2(i-j+1);
end
end
end
stem(y)
Learning Convolution has three steps actually. Flipping, delaying and adding. Loop used in the code
delay after each cycle and also sum the result. Hence performing the same operation as
convolution built-in command.
c) Compare your result using built in command “conv”.
a=[1 0 0 0 0];
h=[1 1 1 1];
a1=length(a);
h1=length(h);
y=zeros(1, a1+h1-1);
h2=[h zeros(1,length(y)-length(h))];
a1=[a zeros(1,length(y)-length(a))];
for i=1:length(y)
for j=1:length(y)
if j<i+1;
y(i)= y(i)+ a1(j)*h2(i-j+1);
end
end
end
subplot(1,2,1)
stem(y)
c=conv(a,h);
subplot(1,2,2)
stem(c)
Learning
Our code and built in command has same results.
d) Find the output of the system for the impulse response and the System Input given
below
1) h[n]={1,-2,3,4,1,5,6,7,5,1,4,3,-2,1}
x[n]={1 2 3 2 1 2 3 2 1 }
2) x[0,1,2,3,4,5,6,7,0]
h[n]=[1 -1]
a=[1 2 3 2 1 2 3 2 1];
h=[1 -2 3 4 1 5 6 7 6 5 1 4 3 -2 1];
a1=length(a);
h1=length(h);
y=zeros(1, a1+h1-1);
h2=[h zeros(1,length(y)-length(h))];
a1=[a zeros(1,length(y)-length(a))];
for i=1:length(y)
for j=1:length(y)
if j<i+1;
y(i)= y(i)+ a1(j)*h2(i-j+1);
end
end
end
subplot(1,2,1)
stem(y)
c=conv(a,h);
subplot(1,2,2)
stem(c)
Published with MATLAB® 7.11
h=[0 1 2 3 4 5 6 7 0];
a=[1 -1];
a1=length(a);
h1=length(h);
y=zeros(1, a1+h1-1);
h2=[h zeros(1,length(y)-length(h))];
a1=[a zeros(1,length(y)-length(a))];
for i=1:length(y)
for j=1:length(y)
if j<i+1;
y(i)= y(i)+ a1(j)*h2(i-j+1);
end
end
end
subplot(1,2,1)
stem(y)
c=conv(a,h);
subplot(1,2,2)
stem(c)
Learning
Our code and built in command has same results.