lab1_dsp
TRANSCRIPT
-
8/3/2019 Lab1_DSP
1/8
School of Electrical Engineering and Computer Science
Digital Signal Processing: Lab Experiments
LAB 01: Getting Started with MATLAB
The Command window
You can start MATLAB by double clicking on the MATLAB icon that should be on the desktop
of your computer. This brings up the window called the Command Window. This window
allows a user to enter simple commands. To clear the Command Window type clc and next press
the Enter or Return key. To perform a simple computations type a command and next press the
Enter or Return key. For instance,
s = 1 + 2 (1)
The output is shown in blue below,
s =
3
fun = sin(pi/4) (2)
fun =
0.7071
In the second example the trigonometric function sine and the constant are used. In MATLAB
they are named sin and pi, respectively.
Note that the results of these computations are saved in variables whose names are chosen by the
user. If they will be needed during your current MATLAB session, then you can obtain their
values typing their names and pressing the Enter or Return key. For instance,
s (3)
s =
3
Variable name begins with a letter, followed by letters, numbers or underscores. MATLAB Release 2008
recognizes only the first 63 characters of a variable name.
To close MATLAB type exit in the Command Window and next press Enter or Return key. A
second way to close your current MATLAB session is to select File in the MATLAB's toolbar
and next click on Exit MATLAB option. All unsaved information residing in the MATLAB
Workspace will be lost.
-
8/3/2019 Lab1_DSP
2/8
School of Electrical Engineering and Computer Science
Digital Signal Processing: Lab Experiments
Numbers in MATLAB
There are three kinds of numbers used in MATLAB: integers real numbers complex numbersIntegers are entered without the decimal point
xi = 10 (4)
xi =
10
However, the following number
xr = 10.01 (5)
xr =
10.0100
is saved as the real number. Variables realmin and realmax denote the smallest and the largest positive
real numbers inMATLAB. For instance,
realmin (6)
ans =
2.2251e-308
Complex numbers in MATLAB are represented in rectangular form. The imaginary unit -1 is
denoted either by i orj
i (7)
ans =
0 + 1.0000i
In addition to classes of numbers mentioned above, MATLAB has three variables representing
the nonnumbers:
-Inf Inf NaNTheInfand Infare the IEEE representations for the negative and positive infinity, respectively.
Infinity is generated by overflow or by the operation of dividing by zero. The NaN stands for the
not-a-numberand is obtained as a result of the mathematically undefined operations such as
0.0/0.0 or -
-
8/3/2019 Lab1_DSP
3/8
School of Electrical Engineering and Computer Science
Digital Signal Processing: Lab Experiments
Workspace in MATLAB
All variables used in the current MATLAB session are saved in the Workspace. You can viewthe content of the Workspace by typing whos in the Command Window. For instance,
whos (8)
Name Size Bytes Class Attributes
ans 1x1 16 double complex
fun 1x1 8 double
s 1x1 8 double
xi 1x1 8 double
xr 1x1 8 double
shows all variables used in current session.
MATLAB HELP
One of the nice features of MATLAB is its help system. To learn more about a function you are
to use, say abs, type in the Command Window
help abs (9)
ABS Absolute value.ABS(X) is the absolute value of the elements of X. When
X is complex, ABS(X) is the complex modulus (magnitude) of
the elements of X.
See also sign, angle, unwrap, hypot.
Overloaded methods:
frd/abs
distributed/abs
iddata/abs
sym/abs
Reference page in Help browser
doc abs
If you do not remember the exact name of a function you want to learn more about use command
lookfor followed by the incomplete name of a function in the Command Window.
Now in the command window type
Help colon (9a)
Make sure you understand how the colon operator works
-
8/3/2019 Lab1_DSP
4/8
School of Electrical Engineering and Computer Science
Digital Signal Processing: Lab Experiments
To enter a statement that is too long to be typed in one line, use three periods, , followed by
Enter or Return. For instance,
x = sin(1) - sin(2) + sin(3) - sin(4) + sin(5) -...
sin(6) + sin(7) - sin(8) + sin(9) - sin(10) (9b)x =
0.7744
You can suppress output to the screen by adding a semicolon after the statement
u = 2 + 3; (9c)
MATLAB as a calculatorList of basic arithmetic operations in MATLAB include
Operation Symbol
addition +
subtraction -
multiplication *
division /
exponentiation ^
Observe the output of the following expressions
pi*pi 10 (8)
sin(pi/4) (9)ans 2 %
-
8/3/2019 Lab1_DSP
5/8
School of Electrical Engineering and Computer Science
Digital Signal Processing: Lab Experiments
(a) Make sure that you understand the colon notation. In particular, explain in words what thefollowing MATLAB code will produce
jkl = 0 : 6 (22) jkl = 2 : 4 : 17 (23)
jkl = 99 : -1 : 88 (24)
ttt = 2 : (1/9) : 4 (25)
tpi = pi * [ 0:0.1:2 ]; (26)
(b) Extracting and/or inserting numbers into a vector is very easy to do. Consider the followingdefinitionof xx:
xx = [ zeros(1,3), linspace(0,1,5), ones(1,4) ] (27)
xx(4:6) (28)
size(xx) (29)length(xx) (30)
xx(2:2:length(xx)) (31)
Explain the results echoed from the last four lines of the above code.
(c) Observe the result of the following assignments:yy = xx; yy(4:6) = pi*(1:3) (32)
EXERCISE 1:
Now write a statement that will take the vector xx defined in part (b) and replace the even indexed
elements (i.e., xx(2), xx(4), etc) with the constant (pi raised to the power pi). Use a vector
replacement, not a loop.
End of Exercise 1
Experiment with vectors in MATLAB. Think of the vector as a set of numbers. Try the following:
xk = cos( pi*(0:11)/4 ) %
-
8/3/2019 Lab1_DSP
6/8
School of Electrical Engineering and Computer Science
Digital Signal Processing: Lab Experiments
yy = [ ]; %
-
8/3/2019 Lab1_DSP
7/8
School of Electrical Engineering and Computer Science
Digital Signal Processing: Lab Experiments
Exercise 4
Generate a time vector (tt) to cover a range of t that will exhibit approximately two cycles of the
4000 Hz sinusoid. Use a definition for tt similar to that in Exercise 3. If we use T to denote the period of
the sinusoids, define the starting time of the vector tt to be equal to T, and the ending time as +T. Thenthe two cycles will include t = 0. Finally, make sure that you have at least 25 samples per period of the
sinusoidal wave. In other words, when you use the colon operator to define the time vector, make the
increment small enough to generate 25 samples per period.
Make a plot of both signals over the range of T t T. You may use arbitrary amplitude and phase for
generating the 4000 Hz sinusoid.
End of Exercise 4
IMPORTANT1. You must show completed exercises to the instructor during lab time and get the
attached sheet signed by the instructor. Submit the sheet to the instructor before
leaving.
2. A report of this lab will be due at the start of next lab. Include a discussion of theexercises in the report.
-
8/3/2019 Lab1_DSP
8/8
School of Electrical Engineering and Computer Science
Digital Signal Processing: Lab Experiments
INSTRUCTOR VERIFICATION SHEET
LAB number:___________________ Date of LAB:______________
Class: __________ Section:________
Name:________________________________ Reg #:____________
Name:________________________________ Reg #:____________
Name:________________________________ Reg #:____________
Exercise 1:
Verified:_______________________________ Date/Time:_______________
Exercise 2:
Verified:_______________________________ Date/Time:_______________
Exercise 3:
Verified:_______________________________ Date/Time:_______________
Exercise 4:
Verified:_______________________________ Date/Time:_______________