Lab DSP
Lab DSP
COMPUTING
Department of
ELECTRICAL AND ELECTRONICS
ENGINEERING (EEE)
EEE 472
Digital Signal Processing
LAB MANUAL
LAB-1
Page 1
%Q1
w=-10*pi:0.1:10*pi;
x=j*w/1+(j*w);
mag=abs(x);
subplot(2,1,1)
plot(w,mag)
xlabel('x')
ylabel('jw')
title('magnitude')
ph=phase(x);
subplot(2,1,2)
plot(w,ph)
xlabel('x')
ylabel('jw')
title('phase')
figure;
%Q2
t=-10:0.1:10;
x=imag(3-exp(1-j*2*pi)*t);
mag=abs(x);
subplot(2,1,1)
plot(t,mag)
xlabel('x')
ylabel('jw')
title('magnitude')
ph=phase(x);
subplot(2,1,2)
Page 2
plot(t,ph)
xlabel('x')
ylabel('jw')
title('phase')
%Q3
t=-10:0.01:10;
x1=1*sin(4*pi*t+0);
subplot(3,3,1)
plot(t,x1,'r')
xlabel('t')
ylabel('x1(t)')
title('x1')
x2=sqrt(3)*sin(4*pi*t+pi/4);
subplot(3,3,2)
plot(t,x2,'y')
xlabel('t')
ylabel('x2(t)')
title('x2')
x3=x1+x2;
subplot(3,3,3)
plot(t,x3,'b')
xlabel('t')
ylabel('x3(t)')
title('x3')
Page 3
%Q4
t=-10:0.01:10;
a1=1;
a2=1;
a3=1;
w1=4*pi;
w2=sqrt(25)*pi;
w3=sqrt(23)*pi;
q1=0;
q2=pi/4;
q3=pi/6;
x1=a1*sin(w1*t+q1)
subplot(3,3,1)
plot(t,x1,'r')
xlabel('t')
ylabel('x1(t)')
title('x1')
x2=a2*sin(w2*t+q2)
subplot(3,3,2)
plot(t,x2,'b')
xlabel('t')
ylabel('x2(t)')
title('x2')
x3=a3*sin(w3*t+q3)
subplot(3,3,3)
plot(t,x3,'g')
xlabel('t')
Page 4
ylabel('x3(t)')
title('x3')
x4=x1+x2+x3;
subplot(3,3,4)
plot(t,x4,'y')
xlabel('t')
ylabel('x4(t)')
title('x4')
%Q5
t=2:0.1:10000;
x=exp(-2*t).*((sin(3.*t)).^2);
energy=sum(x)*0.01
%Q6
t=-10:0.1:10;
sum=0;
for k=0:6
x=exp(j*(2*pi*k*t/8));
sum=sum+x;
end
r=real(sum);
subplot(3,3,1)
plot(t,r)
xlabel('time')
ylabel('r(t)')
Page 5
title('real part')
i=imag(sum);
subplot(3,3,2)
plot(t,i)
xlabel('time')
ylabel('i(t)')
title('imaginary part')
LAB-2
1)
N = 12;
n=[0:2*N-1];
M=4;
x_M=sin((2*pi*M*n)/N);
subplot(2,2,1)
stem(n,x_M,'filled');
title('x[n]=sin((2*pi*M*n)/N)')
Page 6
xlabel('n')
ylabel('x4[n]')
M=5;
x_M=sin((2*pi*M*n)/N);
subplot(2,2,2)
stem(n,x_M,'filled');
title('x[n]=sin((2*pi*M*n)/N)')
xlabel('n')
ylabel('x5[n]')
M=7;
x_M=sin((2*pi*M*n)/N);
subplot(2,2,3)
stem(n,x_M,'filled');
title('x[n]=sin((2*pi*M*n)/N)')
xlabel('n')
ylabel('x7[n]')
M=10;
x_M=sin((2*pi*M*n)/N);
subplot(2,2,4)
stem(n,x_M,'filled');
title('x[n]=sin((2*pi*M*n)/N)')
xlabel('n')
ylabel('x10[n]')
figure;
-------------------------------------------------------------------------------------------------------------------
2)
N=6;
n=[0:7*N];
x2=2*cos(2*n/N)+cos(3*n/N);
x3=cos(2*pi*n/N)+3*sin(5*(pi/2)*n/N);
subplot(2,1,1)
stem(n,x2)
title('x2[n]')
subplot(2,1,2)
stem(n,x3)
title('x3[n]')
figure;
Page 7
---------------------------------------------------------------------------------------------------------------------
3) a)
nx=[-3:7];
x=[0 0 0 2 0 1 -1 3 0 0 0];
stem(nx,x,'filled')
title('x[n]')
xlabel('n')
ylabel('x[n]')
figure;
b)
n=[-3:7];
x=[2 0 1 -1 3 0];
nx=[0 0 0 x 0 0];
ny1=n+2;
ny2=n-1;
ny3=-n;
ny4=-n+1;
subplot(2,2,1)
stem(ny1,nx,'filled')
xlabel('time')
ylabel('x[n-2]')
title('x[n-2]')
subplot(2,2,2)
stem(ny2,nx,'filled')
xlabel('time')
ylabel('x[n+1]')
title('x[n+1]')
subplot(2,2,3)
stem(ny3, nx, 'filled')
xlabel('time')
ylabel('x[-n]')
title('x[-n]')
subplot(2,2,4)
stem(ny4,nx,'filled')
xlabel('time')
ylabel('x[-n+1]')
title('x[-n+1]')
figure;
---------------------------------------------------------------------------------------------------------------------
4)
n=[0:15];
x1=sin((pi/5)*n);
subplot(2,2,1)
Page 8
stem(n,x1,'filled')
xlabel('n')
ylabel('x1[n]')
title('x1[n]')
x2=cos((3*pi/5)*n);
subplot(2,2,2)
stem(n,x2,'filled')
xlabel('n')
ylabel('x2[n]')
title('x2[n]')
n=[0:32];
x3= sin((pi/4)*n) .* cos((pi/4)*n);
subplot(2,2,3)
stem(n,x3,'filled')
xlabel('n')
ylabel('x3[n]')
title('x3[n]')
figure;
n=[-8:8];
m=[-3:3];
x=2*m+1;
nx=[0 0 0 0 0 x 0 0 0 0 0];
stem(n,nx,'filled')
xlabel('n')
ylabel('x[n]')
LAB-3
n=-10:20;
subplot(311);
u=[ zeros(1,10) 1 zeros(1,20) ];
stem(n,u,'filled');
xlabel('time index N');
ylabel('Amplitude');
Page 9
title('unit sample sequence');
axis([ -10 20 -0.2 1.2]);
grid on
subplot(312);
u=[ zeros(1,10) zeros(1,11) 1 zeros(1,20-11) ];
stem(n,u,'filled');
xlabel('time index N');
ylabel('Amplitude');
title('delayed unit sample sequence');
axis([ -10 20 -0.2 1.2]);
grid on
subplot(313);
ud=[ zeros(1,10) ones(1,21) ];
stem(n,ud,'filled');
xlabel('time index N');
ylabel('Amplitude');
title('unit step sequence');
axis([ -10 20 -0.2 1.2]);
grid on
Ex:1.2
c=-1/12+pi*i/6;
k=2;
n=0:40;
x=k*exp(c*n);
subplot(211);
stem(n,real(x),'filled');
Page 10
xlabel('time index N');
ylabel('Amplitude');
title('real part of complex exp');
grid on
%the rate of growth or decay parameter is the real
part of c
subplot(212);
stem(n,imag(x),'filled');
xlabel('time index N');
ylabel('Amplitude');
title('imag part of complex exp');
grid on
figure;
Ex:1.3
n=0:35;
a=1.2;
k=0.2;
x=k*a.^n;
stem(n,x,'filled');
xlabel('time index N');
ylabel('Amplitude');
title('real exp');
grid on
Page 11
LAB-4
Ex:2.1
n=[0:29];
x=cos(2*pi*n/10);
n1=64;
x1=fft(x,n1);
mag_x1=abs(x1);
Page 12
f1=[0:n1-1]/n1;
subplot(311);
plot(f1,mag_x1,'-x');
title('N=64');
axis([0 1 0 20])
n=[0:29];
x=cos(2*pi*n/10);
n1=128;
x1=fft(x,n1);
mag_x1=abs(x1);
f1=[0:n1-1]/n1;
subplot(312);
plot(f1,mag_x1,'-x');
title('N=128');
axis([0 1 0 20])
n=[0:29];
x=cos(2*pi*n/10);
n1=256;
x1=fft(x,n1);
mag_x1=abs(x1);
f1=[0:n1-1]/n1;
subplot(313);
plot(f1,mag_x1,'-x');
title('N=256');
axis([0 1 0 20])
Page 13
Ex:2.2
clc;
close all;
clear;
n=[0:29];
x =cos(2*pi*n/10);
xx =[x x];
xxx =[x x x];
n1=2^10;
x1=fft(x,n1);
x2=fft(xx,n1);
x3=fft(xxx,n1);
mag_x1=abs(x1);
mag_x2=abs(x2);
mag_x3=abs(x3);
f=[0:n1-1]/n1;
subplot(311);
plot(f,mag_x1);
title('"3" periods');
subplot(312);
Page 14
plot(f,mag_x2);
title('"6" periods');
subplot(313);
plot(f,mag_x3);
title('"9" periods');
LAB-5
Q1)
syms n
x1=0.5^n;
disp('z-transform of 0.5^n is');
x1=ztrans(x1)
%b
x2=1+n*(0.4)^(n-1);
disp('z-transform of 1+n*(0.4)^(n-1) is');
x2=ztrans(x2)
Page 15
Q2)
syms n
syms z
x1=1/(1-(1.5*(1/z))+(0.5*(1/z^2)));
disp('inverse z transform is');
x1=iztrans(x1,z,n)
%b
x2=1/((1-(1/z))*(1+(1/z))^2);
disp('inverse z transform is');
x2=iztrans(x2,z,n)
Q3)
syms n z
x1n=0.4^n;
Page 16
x2n=0.5^n;
x1z=ztrans(x1n);
x2z=ztrans(x2n);
x3z=x1z*x2z;
conv1=iztrans(x3z);
disp('convolution of x1z and x2z is')
simplify(conv1)
deconvx1z=x3z/x1z;
deconvx2z=iztrans(deconvx1z);
disp('deconvolution of x1z is')
simplify(deconvx2z)
deconvx3z=x3z/x2z;
deconvx4z=iztrans(deconvx3z);
disp('deconvolution of x2z is')
simplify(deconvx4z)
Q4)
H=tf('z');
Ts=0.1;
b=[3 2 1];
a=[1 -3 2];
disp('the given tf is ')
H=tf([b],[a],Ts)
[r,p,k]=residue(b,a)
disp('the num and den coefficients extracted from r,p,k,')
[b,a]=residue(r,p,k)
Page 17
Q5)
syms z
a=[1 0.8 0.8];
zeros=roots(a)
b=[1 0 0.49];
poles=roots(b)
h=tf('z');
ts=0.1;
h=tf([a],[b],ts)
zgrid on
pzmap(h)
Q6)
syms z n
h=1/(1-(0.8*(z^-
1))+(0.16*(z^2)));
h=iztrans(h);
simplify(h)
n=15;
b=[0 0 1];
a=[1 -.8 0.16];
[h,n]=impz(b,a,n);
stem(n,h)
xlabel('n')
ylabel('h(n)')
Page 18
LAB-6
Exercise
Page 19
n=0:100;
s1=cos(2*pi*.05*n);
s2=cos(2*pi*.47*n);
x=s1+s2;
m=input('Enter the length of the filter = ')
num=ones(1,m);
y=filter(num,1,x)./m;
subplot(221);
plot(n,s1);
grid on;
xlabel('time (sec)');
ylabel('s1(t)');
title('signal # 1');
subplot(222)
plot(n,s2);
grid on;
xlabel('time (sec)');
ylabel('s2(t)');
title('signal # 2');
subplot(223)
plot(n,x);
grid on;
xlabel('time (sec)');
ylabel('x(t)');
title('Input signal');
Page 20
subplot(224)
plot(n,y);
grid on;
xlabel('time (sec)');
ylabel('y(t)');
title('Output signal');
Q3.1
n=0:100;
Figure 1 M=2
s1=cos(2*pi*.155*n);
s2=cos(2*pi*.617*n);
x=s1+s2;
m=input('Enter the length of the filter = ')
num=ones(1,m);
y=filter(num,1,x)./m;
subplot(221);
plot(n,s1);
grid on;
xlabel('time (sec)');
ylabel('s1(t)');
title('signal # 1');
subplot(222)
plot(n,s2);
grid on;
xlabel('time (sec)');
ylabel('s2(t)');
Page 21
title('signal # 2');
subplot(223)
Figure 2 M=3
plot(n,x);
grid on;
xlabel('time (sec)');
ylabel('x(t)');
title('Input signal');
subplot(224)
plot(n,y);
grid on;
xlabel('time (sec)');
ylabel('y(t)');
title('Output signal');
LAB-7
Program1:
Page 22
x1=input('Enter the first sequence: ');
n1=length(x1);
y = 11 11 7 11
*************************************
y = 7 9 14 14 10
**************************************
Program2:
x1=input('Enter the first sequence: ');
n1=length(x1);
n=max(n1,n2);
x1=[ x1 zeros(1,(n-n1)) ];
Page 23
x2=[ x2 zeros(1,(n-n2)) ];
for nn=0:(n-1)
y(nn+1)=0;
for t=0:(n-1)
k=mod( nn-t ,n );
y(nn+1)=y(nn+1) + x1(t+1)*x2(k+1);
end
end
y
y = 11 11 7 11
*******************************************
y= 7 9 14 14 10
LAB-8
LOW Pass Filter
fs=8000;
Page 24
n=50;
w=1200/(fs/2);
b=fir1(n,w,'low');
freqz(b,1,128,8000);
[h,w]=freqz(b,1,128,8000);
subplot(2,1,1)
plot(w,abs(h))
title('Normalized Magnitude Plot')
grid
subplot(2,1,2)
figure(2);
zplane(b,1)
title('zplane')
Page 25
b=fir1(n,w,'high');
freqz(b,1,128,8000);
subplot(2,1,2)
figure(2);
[h,w]=freqz(b,1,128,8000);
plot(w,abs(h))
title('Magnitude Plot')
grid
figure(3);
zplane(b,1)
title('zplane')
Page 26
freqz(b,1,128,8000);figure(2);
[h,w]=freqz(b,1,128,8000);
plot(w,abs(h))
grid
figure(3);
zplane(b,1)
Page 27
freqz(b,1,128,8000);figure(2);
[h,w]=freqz(b,1,128,8000);
plot(w,abs(h))
grid
figure(3);
zplane(b,1)
Notch Filter
fs=8000;
n=40;
b=fir1(n,[1500/4000 1550/4000],'stop');
Page 28
freqz(b,1,128,8000);figure(2);
[h,w]=freqz(b,1,128,8000);
plot(w,abs(h))
grid
figure(3);
Page 29