Signal LAb1
Signal LAb1
Pre-Lab and Warm-Up: You should read at least the Pre-Lab and Warm-up sections of this lab assignment
and go over all exercises in the Pre-Lab section before attending your lab session.
Verification: The Warm-up section of each lab must be completed during your assigned Lab time and
the steps marked Instructor Verification must also be signed off during the lab time. One of the laboratory
instructors must verify the appropriate steps by signing on the Instructor Verification line. When you have
completed a step that requires verification, simply demonstrate the step to the instructor. Turn in the com-
pleted verification sheet before you leave the lab.
Lab Report: It is only necessary to turn in a report on Section 3 with graphs and explanations. You are
asked to label the axes of your plots and include a title for every plot. In order to keep track of plots, include
plots inlined within your report.
1 Pre-Lab
In this first week, the Pre-Lab will be extremely short and very easy. Make sure that you read through the
information below prior to coming to lab.
1.1 Overview
M ATLAB will be used extensively in all the labs. The primary goal of this lab is to familiarize yourself
with using M ATLAB. Please read Appendix B: Programming in M ATLAB for an overview. Here are three
specific goals for this lab:
1. Learn basic M ATLAB commands and syntax, including the help system.
2. Learn to write and edit your own script files in M ATLAB, and run them as commands.
3. Learn a little about advanced programming techniques for M ATLAB, i.e., vectorization.
(b) Run the M ATLAB help desk by typing helpdesk. The help desk provides a hypertext interface to the
M ATLAB documentation. The M ATLAB preferences can be set to use Netscape or Internet Explorer as
the browser for help. Two links of interest are Getting Help (at the bottom of the right-hand frame),
and Getting Started which is under MATLAB in the left-hand frame.
McClellan, Schafer, and Yoder, DSP First, 2e, ISBN 0-13-065562-7. 1 McClellan, Schafer and Yoder, Signal Processing First.
Prentice Hall, Upper Saddle River, NJ 07458. c 2015 Pearson Education, Inc. Prentice Hall, Upper Saddle River, New Jersey, 2003. c 2003 Prentice Hall.
(c) Explore the M ATLAB help capability available at the command line. Try the following:
help
help plot
help colon %<--- a VERY IMPORTANT notation
help ops
help zeros
help ones
lookfor filter %<--- keyword search
NOTE: it is possible to force M ATLAB to display only one screen-full of information at once by
issuing the command more on).
(d) Run the M ATLAB demos: type demo and explore a variety of basic M ATLAB commands and plots.
(e) Use M ATLAB as a calculator. Try the following:
pi*pi - 10
sin(pi/4)
ans ˆ 2 %<--- "ans" holds the last result
(f) Do variable name assignment in M ATLAB. Try the following:
x = sin( pi/5 );
cos( pi/5 ) %<--- assigned to what?
y = sqrt( 1 - x*x )
ans
(g) Complex numbers are natural in M ATLAB. 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 ])
2 Warm-Up
2.1 M ATLAB Array Indexing
(a) Make sure that you understand the colon notation. In particular, explain in words what the following
M ATLAB 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 ];
(b) 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))
McClellan, Schafer, and Yoder, DSP First, 2e, ISBN 0-13-065562-7. 2 McClellan, Schafer and Yoder, Signal Processing First.
Prentice Hall, Upper Saddle River, NJ 07458. c 2015 Pearson Education, Inc. Prentice Hall, Upper Saddle River, New Jersey, 2003. c 2003 Prentice Hall.
Explain the results echoed from the last four lines of the above code.
(b) (A taste of vectorization) Loops can be written in M ATLAB, but they are NOT the most efficient way
to get things done. It’s better to always avoid loops and use the colon notation instead. The following
code has a loop that computes values of the cosine function. (The index of yy() must start at 1.)
Rewrite this computation without using the loop (follow the style in the previous part).
yy = [ ]; %<--- initialize the yy vector to be empty
for k=-5:5
yy(k+6) = cos( k*pi/3 )
end
yy
Explain why it is necessary to write yy(k+6). What happens if you use yy(k) instead?
Instructor Verification (separate page)
(c) Plotting is easy in M ATLAB for both real and complex numbers. The basic plot command will plot a
vector y versus a vector x connecting successive points by straight lines. Try the following:
x = [-3 -1 0 1 3 ];
y = x.*x - 3*x;
plot( x, y )
z = x + y*sqrt(-1)
plot( z ) %<---- complex values: plot imag vs. real
Use help arith to learn how the operation xx.*xx works when xx is a vector; compare to matrix
multiply.
When unsure about a command, use help.
(d) Use the built-in M ATLAB editor (on Windows-95/98/NT), or an external one such as EMACS on
UNIX/LINUX, to create a script file called mylab1.m containing the following lines:
McClellan, Schafer, and Yoder, DSP First, 2e, ISBN 0-13-065562-7. 3 McClellan, Schafer and Yoder, Signal Processing First.
Prentice Hall, Upper Saddle River, NJ 07458. c 2015 Pearson Education, Inc. Prentice Hall, Upper Saddle River, New Jersey, 2003. c 2003 Prentice Hall.
tt = -1 : 0.01 : 1;
xx = cos( 5*pi*tt );
zz = 1.4*exp(j*pi/2)*exp(j*5*pi*tt);
plot( tt, xx, ’b-’, tt, real(zz), ’r--’ ), grid on %<--- plot
a sinusoid
title(’TEST PLOT of a SINUSOID’)
xlabel(’TIME (sec)’)
Explain why the plot of real(zz) is a sinusoid. What is its phase and amplitude ? Make a calculation
of the phase from a time-shift measured on the plot.
Instructor Verification (separate page)
(e) Run your script from M ATLAB. To run the file mylab1 that you created previously, try
mylab1 %<---will run the commands in the file
type mylab1 %<---will type out the contents of
% mylab1.m to the screen
(a) Run the M ATLAB sound demo by typing xpsound at the M ATLAB prompt. If you are unable to hear
the sounds in the M ATLAB demo then ask an instructor for help.
(b) Now generate a tone (i.e., a sinusoid) in M ATLAB and listen to it with the soundsc() command.1 The
first two lines of code in part 2.2(d) create a vector xx of values of a 2.5 Hz sinusoid. The frequency
of your sinusoidal tone should be 2000 Hz and its duration should be 0.9 sec. Use a sampling rate
(fs) equal to 11025 samples/sec. The sampling rate dictates the time interval between time points,
so the time-vector should be defined as follows:
tt = 0:(1/fs):dur;
where fs is the desired sampling rate and dur is the desired duration (in seconds). Read the online
help for both sound() and soundsc() to get more information on using this command. What is the
length (number of samples) of your tt vector?
Instructor Verification (separate page)
argument (fs) is the sampling rate for playing the samples. In addition, soundsc(xx,fs) does automatic scaling and then calls
sound(xx,fs) to actually play the signal.
McClellan, Schafer, and Yoder, DSP First, 2e, ISBN 0-13-065562-7. 4 McClellan, Schafer and Yoder, Signal Processing First.
Prentice Hall, Upper Saddle River, NJ 07458. c 2015 Pearson Education, Inc. Prentice Hall, Upper Saddle River, New Jersey, 2003. c 2003 Prentice Hall.
(a) Generate a time vector (tt) to cover a range of t that will exhibit approximately two cycles of the
4000 Hz sinusoids defined in the next part, part (b). Use a definition for tt similar to part 2.2(d). 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 CT . Then the two cycles will include t D 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.
(b) Generate two 4000 Hz sinusoids with arbitrary amplitude and time-shift.
x1 .t / D A1 cos.2.4000/.t tm1 // x2 .t / D A2 cos.2.4000/.t tm2 //
Select the value of the amplitudes and time-shifts as follows: Let A1 be equal to your age and set
A2 D 1:2A1 . For the time-shifts, set tm1 D .37:2=M /T and tm2 D .41:3=D/T where D and M are
the day and month of your birthday, and T is the period.
Make a plot of both signals over the range of T t T . For your final printed output in part (d)
below, use subplot(3,1,1) and subplot(3,1,2) to make a three-panel figure that puts both of
these plots in the same figure window. See help subplot.
(c) Create a third sinusoid as the sum: x3 .t / D x1 .t / C x2 .t /. In M ATLAB this amounts to summing the
vectors that hold the values of each sinusoid. Make a plot of x3 .t / over the same range of time as
used in the plots of part (b). Include this as the third panel in the plot by using subplot(3,1,3).
(d) Before printing the three plots, put a title on each subplot, and include your name in one of the titles.
See help title, help print and help orient, especially orient tall.
McClellan, Schafer, and Yoder, DSP First, 2e, ISBN 0-13-065562-7. 5 McClellan, Schafer and Yoder, Signal Processing First.
Prentice Hall, Upper Saddle River, NJ 07458. c 2015 Pearson Education, Inc. Prentice Hall, Upper Saddle River, New Jersey, 2003. c 2003 Prentice Hall.
Lab: Introduction to MATLAB
INSTRUCTOR VERIFICATION SHEET
Turn this page in to your lab grading TA before the end of your scheduled Lab time.
Verified: Date/Time:
Part 2.2(b) Explain why it is necessary to write yy(k+6). What happens if you use yy(k) instead?
Verified: Date/Time:
Part 2.2(d) Explain why the plot of real(zz) is a sinusoid. What is its amplitude and phase? In the space
below, make a calculation of the phase from time-shift.
Verified: Date/Time:
Verified: Date/Time:
McClellan, Schafer, and Yoder, DSP First, 2e, ISBN 0-13-065562-7. 6 McClellan, Schafer and Yoder, Signal Processing First.
Prentice Hall, Upper Saddle River, NJ 07458. c 2015 Pearson Education, Inc. Prentice Hall, Upper Saddle River, New Jersey, 2003. c 2003 Prentice Hall.