DSP Lab Manual
DSP Lab Manual
DSP Lab Manual
Experiment No 1
Aim : To find the total response of DT system described by the difference equation.
1. Introduction :
MATLAB is a high-performance language for technical computing. It integrates computation,
visualization, and programming in an easy-to-use environment where problems and solutions are
expressed in familiar mathematical notation.
Typical uses include Math and computation, Algorithm development, Data acquisition, Modeling,
simulation, and prototyping, Data analysis, exploration, and visualization, Scientific and engineering
graphics, Application development, including graphical user interface building.
MATLAB is an interactive system whose basic data element is an array
that does not require dimensioning. This allows you to solve many technical computing problems,
especially those with matrix and vector formulations, in a fraction of the time it would take to write
a program in a scalar non interactive language such as C or Fortran. The name MATLAB stands for
matrix laboratory.
Quitting MATLAB: To end your MATLAB session, select File > Exit MATLAB in the desktop, or
type quit in the Command Window. You can run a script file named finish.m each time MATLAB
quits that, for example, executes functions to save the workspace.
2. MATLAB desktop:
On starting MATLAB the MATLAB desktop appears, containing tools (graphical user interfaces )
for managing files , variables and applications associated with MATLAB. It contains :
(i) Command Window and Command History
Command Window : Use the Command Window to enter variables and to run functions and M-file
scripts. Press the up arrow key to recall a statement previously typed. Edit the statement as needed
and then press Enter to run it.
Command History : Statements entered in the Command Window are logged in the Command
History. From the Command History, previously run statements can be viewed, copied and
executed. M-file can be created from selected statements.
(ii) Current Directory Browser and Search Path : MATLAB file operations use the current directory
and the search path as reference points. Any file required to run must either be in the current
directory or on the search path.
(iii) Workspace Browser and Array Editor :The MATLAB workspace consists of the set of variables
(named arrays) built up during a MATLAB session and stored in memory. The variables can be
added to the workspace by using functions, running M-files, and loading saved workspaces. To
delete variables from the workspace, select the variables and select Edit > Delete.
Array Editor : Double-click a variable in the Workspace browser, or use open var variable name, to
see it in the Array Editor. Use the Array Editor to view and edit a visual representation of variables
in the workspace.
(iv) Editor/Debugger : Editor/Debugger is used to create and debug M-files, which are
programs to run MATLAB functions. The Editor/Debugger provides a graphical
user interface for text editing, as well as for M-file debugging. To create or edit an M-file use File >
New or File > Open, or use the edit function.
The important commands/ functions are as below :
1. clc (Remove items from workspace, freeing up system memory) clears all input and output
from the Command Window display, giving "clean screen." After using clc, the scroll bar
cannot be used to see the history of functions, but still the up arrow can be used to recall
statements from the command history.
2. close (Remove specified figure): close deletes the current figure or the specified figure(s). It
optionally returns the status of the close operation.
3. xlabel, ylabel, zlabel (Label x-, y-, and z-axis) : Each axes graphics object can have one label
for the x-, y-, and z-axis. The label appears beneath its respective axis in a two-dimensional plot
and to the side or beneath the axis in a three-dimensional plot.
xlabel('string') labels the x-axis of the current axes.
ylabel(...) and zlabel(...) label the y-axis and z-axis, respectively, of the current axes.
4. title( Add title to current axes) : Each axes graphics object can have one title. The title is located
at the top and in the center of the axes.
title('string') outputs the string at the top and in the center of the current axes.
5. figure (create figure graphics object) : figure creates figure graphics objects. Figure objects are
the individual windows on the screen in which MATLAB displays graphical output.
6. subplot (Create axes in tiled positions): subplot divides the current figure into rectangular panes
that are numbered row wise. Each pane contains an axes object. Subsequent plots are output to
the current pane.
h = subplot(m,n,p) or subplot(mnp) breaks the figure window into an m-by-n matrix of small
axes, selects the pth axes object for the current plot, and returns the axes handle. The axes are
counted along the top row of the figure window, then the second row, etc. For example,
subplot(2,1,1), plot(income)
subplot(2,1,2), plot(outgo) plots income on the top half of the window and outgo on the bottom
half.
7. stem (Plot discrete sequence data) : A two-dimensional stem plot displays data as lines
extending from a baseline along the x-axis. A circle (the default) or other marker whose y-
position represents the data value terminates each stem.
stem(Y) Plots the data sequence Y as stems that extend from equally spaced and automatically
generated values along the x-axis. When Y is a matrix, stem plots all elements in a row against
the same x value.
stem(X,Y) plots X versus the columns of Y. X and Y must be vectors or matrices of the same
size. Additionally, X can be a row or a column vector and Y a matrix with length(X) rows.
8. bar(Plot bar graph (vertical and horizontal)) : A bar graph displays the values in a vector or
matrix as horizontal or vertical bars.
bar(Y) draws one bar for each element in Y. If Y is a matrix, bar groups the bars produced by
the elements in each row. The x-axis scale ranges from 1 up to length(Y) when Y is a vector, and
1 to size(Y,1), which is the number of rows, when Y is a matrix.
barh(...) and h = barh(...) create horizontal bars. Y determines the bar length. The vector x is a
vector defining the y-axis intervals for horizontal bars.
9. plot ( 2-D line plot) :
plot(Y) Plots the columns of Y versus their index if Y is a real number. If Y is complex, plot(Y)
is equivalent to plot(real(Y),imag(Y)). In all other uses of plot, the imaginary component is
ignored.
plot(X1,Y1,...) Plots all lines defined by Xn versus Yn pairs. If only Xn or Yn is a matrix, the
vector is plotted versus the rows or columns of the matrix,
depending on whether the vector's row or column dimension matches the matrix. If Xn is a scalar
and Yn is a vector, disconnected line objects are created and plotted as discrete points vertically
at Xn.
10. input (Request user input) : The response to the input prompt can be any MATLAB
expression, which is evaluated using the variables in the current workspace.
user_entry = input('prompt') Displays prompt as a prompt on the screen, waits for input
from the keyboard, and returns the value entered in user_entry. user_entry = input('prompt', 's')
returns the entered string as a text variable rather than as a variable name or numerical value.
11. zeros (Create array of all zeros) :
B = zeros(n) Returns an n-by-n matrix of zeros. An error message appears if n is not a scalar.
12. ones (Create array of all ones) :
Y = ones(n) Rreturns an n-by-n matrix of 1s. An error message appears if n is not a scalar.
13. exp (Exponential) :
Y = exp(X) The exp function is an elementary function that operates element-wise on arrays.
Its domain includes complex numbers.
Y = exp(X) returns the exponential for each element of X.
P = angle(Z) returns the phase angles, in radians, for each element of complex array Z. The
angles lie between + π and – π.
34. log (Natural logarithm ) :
Y = log(X) returns the natural logarithm of the elements of X. For complex or negative z, where z
= x +y*i , the complex logarithm is returned.
.
Program:
clear all;
close all;
clc;
K = -2:16;
for m = 1:length(K)-2;
y = 0.6*Y(m+1)+0.16*Y(m);
Y = [Y y];
end
yzi=Y; % zero input response
subplot(2,2,1)
stem (K,yzi);
xlabel('n');
ylabel('yzi(n)');
title('Zero Input Response');
% % impulse response
N=19;
b=[5 0 0];
a=[1 -0.6 -0.16];
f=[1,zeros(1,N-1)];
h=filter(b,a,f); % h is the impulse response of the system
k=0:1:N-1;
subplot(2,2,2)
stem(k,h);
xlabel('k')
ylabel('h[k]')
title('Impulse Response');
%input
k1=0:4;
f=(0.25).^k1;
Yzs=conv(h,f); % zero state response
subplot(2,2,3)
k = 1:23;
stem(k,Yzs);
xlabel('n');
ylabel('yzs(n)');
Yzs=[0 0 Yzs];
m=length(yzi);
n=length(Yzs);
if m>n
Yzs=[Yzs zeros(1,m-n)];
else
yzi=[yzi zeros(1,n-m)];
end
Yt=yzi+Yzs; %Total Response
k=-2:length(Yt)-3;
subplot(2,2,4)
stem(k,Yt);
xlabel('n');
ylabel('y(n)');
title('Total Response');
disp(' yzi Yzs Yt');
disp([yzi' Yzs' Yt']);
Output:
Zero Input Response Impulse Response
8 6
6
4
yzi(n)
h[k]
4
2
2
0 0
-10 0 10 20 0 5 10 15 20
n k
Zero State Response Total Response
6 8
6
4
yzs(n)
y(n)
4
2
2
0 0
0 10 20 30 -10 0 10 20 30
n n
Experiment No. 2
Program :
clear all;
close all;
clc;
t=-10:.01:10;
T=4;
fm=1/T;
X=cos(2*pi*fm*t)
fs1=1.6*fm
fs2=2*fm
fs3=8*fm
subplot(2,2,1)
plot(t,X)
xlabel('Time in sec')
ylabel('X(t)')
Title('CT signal')
n1=-4:1:4
Xn1=cos(2*pi*n1*(fm/fs1))
subplot(2,2,2)
stem(n1,Xn1)
Hold on;
subplot(2,2,2)
plot(n1,Xn1)
xlabel('n')
ylabel('Xn1')
Title('DT signal with fs<2fm')
n2=-5:1:5;
Xn2=cos(2*pi*n2*(fm/fs2))
subplot(2,2,3)
stem(n2,Xn2)
Hold on;
subplot(2,2,3)
plot(n2,Xn2)
xlabel('n')
ylabel('Xn2')
Title('DT signal with fs=2fm')
n3=-20:1:20
Xn3=cos(2*pi*n3*(fm/fs3))
subplot(2,2,4)
stem(n3,Xn3)
Hold on;
subplot(2,2,4)
Electronics & Telecommunication Engg. Pg.no.
JES’s S.N.D.C.O.E &R C, Yeola DSP lab journal
plot(n3,Xn3)
xlabel('n')
ylabel('Xn3')
Title('DT signal with fs>2fm')
Output :
0.5 0.5
Xn1
X(t)
0 0
-0.5 -0.5
-1 -1
-10 -5 0 5 10 -4 -2 0 2 4
Time in sec n
DT signal with fs=2fm DT signal with fs>2fm
1 1
0.5 0.5
Xn2
Xn3
0 0
-0.5 -0.5
-1 -1
-5 0 5 -20 -10 0 10 20
n n
Experiment No. 3
Aim : To find DTFS for periodic and DTFT for non periodic signal.
Program :
clear all;
close all;
clc;
subplot(2,2,1);
stem(k,x);
xlabel('n');
ylabel('x(n)');
title('Single Period of a periodic Signal');
subplot(2,2,3);
stem(k,Ck);
xlabel('k');
ylabel('Ck');
title('DTFS of a periodic Signal');
%------------------------------------------------
% part B: DTFT of given aperiodic function
N =32;
k = 0:N-1;
%representing an aperiodic signal with x
x = [ ones(1,5) zeros(1,N-9) ones(1,4)];
subplot(2,2,2);
stem(k,x);
xlabel('n');
ylabel('x(n)');
title('an aperiodic Signal');
subplot(2,2,4);
w=k*(pi/32);
plot(w,Xw);
xlabel('w');
Electronics & Telecommunication Engg. Pg.no.
JES’s S.N.D.C.O.E &R C, Yeola DSP lab journal
ylabel('Xw');
title('DTFT of an aperiodic Signal');
Output :
x(n)
0.5 0.5
0 0
0 10 20 30 40 0 10 20 30 40
n n
DTFS of a periodic Signal DTFT of an aperiodic Signal
0.3 10
0.2
5
Xw
Ck
0.1
0
0
-0.1 -5
0 10 20 30 40 0 1 2 3 4
k w
Experiment No. 4
Aim : To find DFT of DT signal.
.
Program :
clear all
close all
clc
Xk=fft(x);
subplot(3,1,1)
stem(k,x)
xlabel('n')
ylabel('x(n)')
subplot(3,1,2)
stem(k,abs(Xk))
xlabel('k')
ylabel('|X(k)|')
title('Magnitude Response')
subplot(3,1,3)
stem(k,angle(Xk))
xlabel('k')
ylabel('< X(k)')
title('Phase Response');
disp(' Xk abs(Xk) angle(Xk)');
disp([Xk' abs(Xk)' angle(Xk)']);
Output :
2
x(n)
0
-2
0 5 10 15 20 25 30 35
n
Magnitude Response
10
|X(k)|
0
0 5 10 15 20 25 30 35
k
Phase Response
5
< X(k)
-5
0 5 10 15 20 25 30 35
k
Experiment No. 5
Aim : To find the response of DT system using convolution.
Program :
clear all;
close all;
clc;
x=input('enter x(n)');
h=input('enter h(n)');
%y=conv(x,h)
y=xyz_conv(x,h);
subplot(311), stem(x);
xlabel('n');
ylabel('x(n)');
title('Signal');
subplot(312), stem(h);
xlabel('n');
ylabel('h(n)');
title('System');
subplot(313), stem(y);
xlabel('n');
ylabel('y(n)');
title('Response');
Output :
enter x(n)[1 2 3 4]
enter h(n) [4 3 2 1]
Signal
4
x(n) 2
0
1 1.5 2 2.5 3 3.5 4
n
System
4
h(n)
0
1 1.5 2 2.5 3 3.5 4
n
Response
40
y(n)
20
0
1 2 3 4 5 6 7
n
Experiment No. 6
Program :
subplot(311), stem(x);
xlabel('n');
ylabel('x(n)');
title('Signal');
subplot(312), stem(h);
xlabel('n');
ylabel('h(n)');
title('System');
subplot(313), stem(y);
xlabel('n');
ylabel('y(n)');
title('Response');
stem(k,real(y))
Output:
x= 1 2 3 4 5 6 7 8 9
h= 1 2 3 4
m= 4
x= 1 2 3 4 5 6 7 8 9 0 0 0
L = 15
Electronics & Telecommunication Engg. Pg.no.
JES’s S.N.D.C.O.E &R C, Yeola DSP lab journal
k= 0 1 2 3 4 5 6 7 8 9 10 11
y=
1 4 10 20 30 40 50 60 70 70 59 36
Experiment No. 7
Aim : To find Z and inverse Z transform and draw sketch pole zero plot of Z-domain transfer function.
Program :
b =1;
[R,p,C] =residuez(b,a)
x = filter(b,a,delta)
Output :
R = 0.2500
0.5000
0.2500
p = 0.9000
0.9000
-0.9000
C = []
delta = 1 0 0 0 0 0 0 0
n= 0 1 2 3 4 5 6 7
Experiment No. 8
Aim : To solve the difference equation and find the system response using Z transform.
Program :
%Aim: Find the response y(n) of an LTID system described by the difference
%equation y(n+2) + y(n+1) + 0.16y(n) = x(n+1) + 0.32 x(n)
% for the input x(n) = (-2)^-n u(n)
clear all;
close all;
clc;
N=11;
n=0:N-1;
b=[0 1 0.32];
a=[1 1 0.16];
deltafunction= [1 zeros(1,N-1)];
h=filter(b,a,deltafunction); % Impulse response of the system
x=(-2).^(-n);
y=filter(b,a,x);% Response of the system for the input x(n)
subplot(2,2,1)
stem(n,x);
xlabel('n'), ylabel('x(n)');
title('Input');
subplot(2,2,2)
stem(n,y);
xlabel('n'), ylabel('y(n)');
title('Response of the system');
subplot(2,2,3)
stem(n,h);
xlabel('n'), ylabel('h(n)');
title('Impulse Response');
subplot(2,2,4)
y1=conv(x,h);
stem(n,y1(1:N));
xlabel('n'), ylabel('y(n)');
title('Response of the system using Convolution');
Output:
1
0.5
x(n)
y(n)
0
0
-1
-0.5 -2
0 5 10 0 5 10
n n
Impulse Response Response of the system using Convolution
1 2
0.5 1
h(n)
y(n)
0 0
-0.5 -1
-1 -2
0 5 10 0 5 10
n n
Experiment No. 9
Aim : To study interpolation and decimation.
.
Program :
subplot(3,1,1)
stem(x(1:25)) % plot original signal
xlabel('Discrete time, nT ')
ylabel('Input signal level')
subplot(3,1,2)
stem(y(1:50)) % plot interpolated signal.
xlabel('Discrete time, 4 x nT')
ylabel('Interpolated output ')
y1=resample(y,1,2);
subplot(3,1,3)
stem(y1(1:25)) % plot decimated signal.
xlabel('Discrete time, nT')
ylabel('Decimated output ')
Output :
-5
0 5 10 15 20 25
Discrete time, nT
Interpolated output
-5
0 5 10 15 20 25 30 35 40 45 50
Discrete time, 4 x nT
Decimated output
-5
0 5 10 15 20 25
Discrete time, nT
Experiment No. 10
Program :
Output:
Enter the pass band ripple 0.05
Enter the stop band ripple 0.04
Enter the pass band freq 1500
Enter the stop band freq 2000
Enter the sampling freq 9000
100
Gain in dB ------>
0
-100
-200
0 0.5 1
(a) Normalised freq ---->
Experiment No. 11
Aim : To find and plot the frequency response for the rectangular and Hamming window.
Program :
1)Hamming window
L=64;
wvtool(hamming(L))
Output
0.8 0
Magnitude (dB)
Amplitude
-20
0.6
-40
0.4
-60
0.2
-80
0 -100
10 20 30 40 50 60 0 0.2 0.4 0.6 0.8
Samples Normalized Frequency ( rad/sample)
Experiment No. 12
Aim : To find the impulse invariance IIR digital filter to realize the first order analog
Program :
Wp = 40/500; Ws = 150/500;
[n,Wn] = buttord(Wp,Ws,3,60);
% Returns n = 5; Wn=0.0810;
[b,a] = butter(n,Wn);
Output:
-100
-200
-300
-400
0 50 100 150 200 250 300 350 400 450 500
Frequency (Hz)
0
Phase (degrees)
-200
-400
-600
0 50 100 150 200 250 300 350 400 450 500
Frequency (Hz)