Lab 01
Lab 01
Lab 01
1. Objectives:
(a). Learn MATLAB basic operations, constructs
(b). Generate basic signals in MATLAB
2. Time Required: 3 hrs
3. Programming Language: MATLAB
4. Software Required:
(a). Windows/Linux OS
(b). MATLAB 7 or above
5. MATLAB is the most popular tool used for Digital Signal Processing. It provides one of the
strongest environments for study and simulation of the real-world problems and their solutions,
especially in the field of engineering. For Signal Processing, it has a very comprehensive and
easy-to-use toolbox with lots of DSP functions implemented. Moreover, with the aid of Simulink,
we can create much more complex situations very easily, and solve them.
6. If you don’t know anything about MATLAB, then MATLAB help is the best way to learn about
something. In the command window of MATLAB simply write ‘help’;
>>help
It will display list of all toolboxes included in MATLAB. Then by investigating the name of
toolbox or the name of a function, which you would like to learn how to use, use the ‘help’
command:
>>helpfunctionname
This command displays a description of the function and generally also includes a list of related
functions. If you cannot remember the name of the function, use the ‘lookfor’ command and the
name of some keyword associated with the function:
>>lookfor keyword
This command will display a list of functions that include the keyword in their descriptions. Other
help commands that you may find useful are ‘info’, ‘what’, and ‘which’. Descriptions of these
commands can be found by using the help command. MATLAB also contains a variety of demos
that can be with the ‘demo’ command.
Wide tutorials are available on internet for help on any function or how to use it.
When command is executed the very next will display the output of the command. If a semicolon
is typed at the end of command the output is not displayed (called suppress the output).
Typing % or %{ and }%
When the symbol % (percent symbol) is type in the beginning of a line, the line is designated as a
comment, which is called single line comments.
When any number of statements written between symbols %{ and %} is treated comments, which
is called multi-line comments or blocks commenting.
Workspace All variables used in the current MATLAB session are saved in the Workspace and
2|Page Digital Signal Processing
A = [1 2; 3 4]
B=A*A
C=A.*A
Operations *,-,+,/,.*,./ etc
Logical Operations: <,>, <=,~= etc
Math Functions:
sin Sine
cos Cosine
tan Tangent
asin Inverse sine
acos inverse cosine
atan inverse tangent
exp exponential
log natural logarithm
log10 common logarithm
sqrt Square root
abs Absolute value
sign signum
Logical Operations
x=5
x = =2 Is x equal to 2?
x ~= 2 Is x not equal to 2?
8. Lab Questions 8
Problem #1 Run the MATLAB help desk by typing helpdesk. The help desk provides a
hypertext interface to the MATLAB documentation.
Observation:
Problem #4 Complex numbers are natural in MATLAB. The basic operations are supported.
Try the following:
z = 3 + 4i, w = -3 + 4j
real(z), imag(z)
abs([z,w]) %<-- Vector constructor
conj(z+w)
angle(z)
exp( j*pi )
exp(j*[ pi/4, 0, -pi/4 ])
Observation:
4|Page Digital Signal Processing
Problem #5 Make sure that you understand the colon notation. In particular, explain in words
what the following MATLAB code will produce.
jkl = 0 : 6
jkl = 2 : 4 : 17
jkl = 99 : -1 : 88
ttt = 2 : (1/9) : 4
tpi = pi * [ 0:0.1:2 ];
Observation:
Problem #6 Extracting and/or inserting numbers into a vector is very easy to do. Consider the
following definition of xx:
xx = [ zeros(1,3), linspace(0,1,5), ones(1,4) ]
xx(4:6)
size(xx)
length(xx)
xx(2:2:length(xx))
Explain the results echoed from the last four lines of the above code.
Observation:
A=
[ 26 34 ] [ 16 62 ]
,B=
9. Matrices Handling:
size (A), size(x), A’,
ones (2, 3), zeros (2,3),eye (3), diag (d),
fliplr Flip matrices left-right
flipud Flip matrices up-down
repmat Replicate and tile an array
5|Page Digital Signal Processing
subplot:
SUBPLOT Create axes in tiled positions.
H = SUBPLOT(m,n,p), or SUBPLOT(mnp), breaks the Figure window into an m-by-n matrix of
small axes, selects the p-th axes for the current plot, and returns the axis handle. The axes are
counted along the top row of the Figure window, then the second row, etc.
POLAR PLOTS:
Plotting in 3-D: There are also ways to plot in multiple dimensions in Matlab*. One type of 3-D plot that
may be useful is a surface plot, which requires you to generate some kind of x-y plane and then apply a
3rd function as the z dimension.
Example: clear all
close all
[x,y] = meshgrid([-2:.2:2]); % set up 2-D plane
6|Page Digital Signal Processing
stem:
STEM Discrete sequence or "stem" plot.
STEM(Y) plots the data sequence Y as stems from the x axis terminated with circles for the data
value.
STEM(X,Y) plots the data sequence Y at the values specified in X.
title:
TITLE Graph title.
TITLE('text') adds text at the top of the current axis.
xlabel:
XLABEL X-axis label.
XLABEL('text') adds text beside the X-axis on the current axis.
ylabel:
YLABEL Y-axis label.
YLABEL('text') adds text beside the Y-axis on the current axis.
10 Matlab Constructs:
. For, While, If Then,
» x = 1:10;
» for i = 1:10
» y (i) = exp (x (i))
11 Script Files:
How to make and run it
How to make a functions
12 Lab Questions 5
.
Problem # 1 Generate a Truth Table for XOR gate using relational and logical operators.
Observation:
Problem # 2 If x = [5 -3 18 4] and y = [-9 13 7 4], what will be the result of the following
operations?
z = ~y > x
z=x&y
z=x|y
z = xor (x, y)
7|Page Digital Signal Processing
Observation:
Problem # 3 Type this matrix in MATLAB and answer the following questions:
A=
Create a 3x3 array B consisting of all elements in the second and last column of A.
Create a 2x4 array C consisting of all elements in the second and last row of A.
Create a 2x4 array D consisting of all elements in the first and last row of A.
Compute the length, size, and sum of array A.
Observation:
Problem # 4 Enter the matrices A = and B = and use MATLAB to compute -1.4A+3.1B and
2.67A-11.B.
Observation:
Problem # 5 Take two inputs from the user. If both inputs named as ‘x’ and ‘y’ are non-negative
then compute z and w, and display its result.
z = √x + √y
w = log x – 3 log y
Observation:
13 Bonus Question: 2
Generate sine wave of varying frequencies and play them
Total Marks 15
14 Web Resources
http://web2.clarkson.edu/class/ma571/Xeno-MATLAB_guide.pdf
https://web.njit.edu/~yyoung/M222Spring2016/matlab_tutorial.pdf
http://people.duke.edu/~hpgavin/matlab.html
13 Video Resource
http://www.learningmatlab.com/videos/
https://www.youtube.com/watch?v=QOJCvHUkJSs
https://www.youtube.com/watch?v=tpN12ruYATc
https://www.youtube.com/watch?v=y5PTcskuldE&list=PLX8cYDJmWL1mfq6BI-
klKB7YzRdJZ2_WW
https://www.youtube.com/watch?
v=r3xup1GamDs&list=PLAqLzsHAOZ_Ym56Q9Qo5KeRoTglXed4qM
8|Page Digital Signal Processing
Summary:This lab gives a short introduction about MATLAB. It includes basic operations,
constructs, logic, plotting and edit files.
Instructions:
1. Do it yourself.
2. Submit it before leaving until been specified by teacher
3. After marking, these will be deposited with teacher for record.