DSP Lab 1
DSP Lab 1
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING
Experiment No: 01
Introduction to MATLAB and generating Discrete Signals
Objective:
Matlab will be used extensively in all the succeeding labs. The goal of this first lab is to gain
familiarity with Matlab and build some basic skills in the Matlab language. Some specific topics
covered in this lab are:
Introduction to MATLAB
MATLAB environment
Basic Operations in MATLAB
Defining a scalar, vector, matrix
Operations on vectors and matrices
Applying Relational operators
Discrete Signals in MATLAB
1. Introduction to MATLAB
MATLAB is a commercial "MATrix LABoratory" package, by MathWorks, which operates as an
interactive programming environment with graphical output. The MATLAB programming language is
exceptionally straightforward since almost every data object is assumed to be an array. Hence, for
some areas of engineering MATLAB is displacing popular programming languages, due to its
interactive interface, reliable algorithmic foundation, fully extensible environment, and computational
speed.
2. MATLAB Environment
a. Entering and Leaving MATLAB
Double click on the MATLAB icon to launch and a command window will appear with the
prompt:
>>
You are now in MATLAB. From this point on, individual MATLAB commands may be given at
the program prompt. They will be processed when you hit the <ENTER> key.
The following figure shows the screenshot of Matlab.
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING
b. Leaving MATLAB
A MATLAB session may be terminated by simply typing
>> quit
or by typing
>> exit
c. Creating M-File
Files that contain a computer code are called the m-files. There are two kinds of m-files: the
script files and the function files. Script files do not take the input arguments or return the
output arguments. The function files may take input arguments or return output arguments.
To make the m-file
Click on File select New and click on M-File from the pull-down menu. You will be
presented with the MATLAB Editor/Debugger screen. Here you will type your code, can
make changes, etc.
Once you are done withtyping, click on File, in the MATLAB Editor/Debugger screen and
select Save as….
Chose a name for your file, e.g., myfirstprogram.m and click on Save. Make sure that your
file is saved in the directory that is in MATLAB's search path.
To open the m-file from the Command Window, type edit myfirstprogram and press Enter
or Return key.
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING
Defining a scalar:
x=1
x=
1
Defining a column vector
v = [1;2;3]
v=
1
2
3
Defining a row vector
w = [1 0 1]
w=
1 0 1
Transpose a vector
W = w’
W=
1
0
1
X = 1:.5:5
X=
Columns 1 through 9
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING
Empty vector
Y=[]
Y=
[]
Defining a matrix
M = [1 2 3; 3 2 1]
M=
1 2 3
3 2 1
Zero matrix
M = zeros (2, 3) % 1st parameter is row, 2nd parameter is column.
M=
0 0 0
0 0 0
Ones matrix
m = ones (2, 3)
m=
1 1 1
1 1 1
The identity matrix
I = eye (3)
I=
1 0 0
0 1 0
0 0 1
Define a random matrix or vector
R = rand (1, 3)
R=
0.9501 0.2311 0.6068
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING
Arithmetic operations
x= [1 2 3 4 5]
x=
1 2 3 4 5
x= 2 * x
x= x / 2
y = [1 2 3 4 5]
z=x+y
Point by point mult/div use “.“
W = x.*y
Matlab has a large number of built in functions, some operate on each point of a
Vector/matrix:
log([1 2 3 4])
round ([1.5 2; 2.2 3.1])
a=[1 4 6 3]
sum (a)
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING
mean (a)
max (a)
a = [1 2 3; 4 5 6]
mean (a) %mean of each column
max (a) %max of each column
max(max([1 2 3; 4 5 6]) ) %to obtain max of matrix
c. Relational operators
3. Signals in MATLAB
Signals are broadly classified into continuous and discrete signals. A continuous signal will be
denoted by x(t), in which the variable t can represent any physical quantity. A discrete signal will be
denoted x[n], in which the variable n is integer value. In this lab we will learn to represent and operate
on signals in MATLAB. The variables t and n are assumed to represent time. In this lab, we will only
focus on Discrete time signals.
n=
-3 -2 -1 0 1 2 3
>> x = [1 -1 0 2 1 4 6]
x=
1 -1 0 2 1 4 6
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING
>> x = -3:3
x=
-3 -2 -1 0 1 2 3
>> x = 1:0.5:4
x=
b. Plotting in MATLAB:
Continuous Signals
t=0:2*pi;
plot(t,sin(t))
t= 0:0.02:2*pi;
plot(t,sin(t))
title('CT signal plot')
xlabel('t (Seconds)')
ylabel('y(t)')
Discrete Signals
n = -10: 10;
f = n >= 0;
stem(n,f)
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING
4. Signals Plotting:
Multiple Graphs:
There are two ways to make multiple plots on a single graph.
One way is with the hold command. The command hold freezes the current graphics screen so that
subsequent plots are superimposed on it. Entering hold again releases the "hold". The commands hold
on and hold off are also available.
t = 0: 0.01: 2pi;
y1 = sin(t);
y2 = sin(2t);
y3 = sin(4t);
plot(t, y1, '-', y2, ‘*', y3, '+')
Subplot:
In-Lab Assignment:
1. Provide the snapshot of all the codes mention in Section 3.
2. Provide the snapshot of all the codes mention in Section 4.
3. Write a MATLAB program to plot 6 different continuous sin signals in one window. Provide
the snapshot of the code and the graph.
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING
[ ]
10 20 30 40 90 50
1 2 3 4 90 60
8 9 −1 5 90 70
−4 −5 −2 6 90 80
−7 −6 −3 7 90 90
8 9 5 77 90 90
1. Extract the mentioned values from the given matrix (use 2 different methods to extract these
values).
2. Write a MATLAB generic code to take matrix values as user input of any dimensional matrix.
3. Write a MATLAB program to plot 4 different discrete sin signals using subplot command.
Provide the snapshot of the code and the graph.
Comments: