George Mason University
ECE 201: Introduction to Signal Analysis
Spring 2017
Laboratory Project #2
Assigned: February 02, 2017
Due Date: Week of February 13, 2017
Due Date
Your lab report must be submitted on blackboard by 5:00 PM on the day of your lab on the week
that it is due. Late lab reports will be penalized as noted in the Lab syllabus.
Lab Report
Your lab report for this lab will consist of answers and complete documentation of the questions
and exercises in Section 3. The pre-lab, Section 2, is designed to get you ready for the problems
in Section 3. Guidelines for preparing the lab report have been discussed during the first week of
lab, and the format for the report has been posted on Blackboard. If you have any questions, talk
to your lab TA. For any plots that you make, make sure to label the axes and include a title for
every plot. In order to keep track of plots, include your plot inline within your report.
Honor Code
Forgeries and plagiarism are a violation of the honor code and any reasonable suspicion of an honor
code violation will be reported. You are allowed to discuss lab exercises with other students, but
the submitted work should be original and it should be your own work.
1
Introduction
Before going to lab you should, at a minimum, read the pre-lab to get familiar with plotting, writing
script files, and listening to sound files. As in the previous lab, you may find it helpful to read the
help that is available for the MATLAB commands used in this lab. If you do not have access to
MATLAB at home, or are unable to access the GMU Virtual Commuting Lab (VCL), you may read
about these commands on the MATLAB web page:
https://www.mathworks.com/help/matlab/
It may be difficult to complete all assignments for your lab report during the lab period if you do
not come prepared.
Since you will be listening to some sound files in this lab, it is recommended that you bring
headphones to the lab for listening.
2
Pre-Lab
The pre-lab is designed to get you ready for the exercises that are to be completed in your lab
report. In this pre-lab, you will be learning how to make plots, write MATLAB scripts, and listen
to sound files. The primary goals of this lab are:
1. To learn how to write and edit your own script files in MATLAB, and run them as commands.
2. To explore more plotting capabilities, learn how to make labeled plots, and print them to a
file.
3. Learn how to use the soundsc.m to listen to audio files.
4. To plot sinusoids and sums of sinusoids, and to understand how to find the frequency, phase,
and amplitude of a sum of sinusoids.
2.1
The Colon Operator and Vectorization
In the first lab you were introduced to the colon operator, which is one of the most useful operators
in MATLAB. One way to use this operator is to create a vector of equally spaced numeric values.
For example,
>> n=0:10
n =
0
1
2
3
4
5
6
7
8
9
10
creates a vector of integers from zero to ten that are stored in the variable n. To create a vector of
equally spaced numbers between zero and one, in increments of 0.1, one would type
>> n=0:0.1:1
n =
Columns 1 through 8
0
0.1000
Columns 9 through 11
0.8000
0.9000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
1.0000
There are other uses for the colon operator that we will learn about in later labs.
(a) Experiment with vectors in MATLAB. Think of the vector as a set of numbers. Try the
following:
>> xk = cos( pi*(0:11)/4 );
%<---
compute cosines
Explain how the different values of the cosine are stored in the vector xk. What is xk(1)? Is
xk(0) defined? Recall that the semicolon at the end of a statement will suppress the echo to
the screen, and that the text following the % is a comment and may be omitted.
(b) (A taste of vectorization) Loops can be written in MATLAB, but they are NOT the most
efficient way to get things done. It is always better to avoid loops and use the colon notation
instead. The following code has a loop that computes values of the cosine function.1 Note
that the index of yy() must start at 1.
yy = [ ];
for k=-5:5
yy(k+6) = cos(k*pi/3)
end
%<--- initialize the yy vector to be empty
The first command initializes the vector yy to be empty. Rewrite this computation without
using a loop (follow the style in the previous part). Explain why it is necessary to write
yy(k+6). What happens if you were to replace yy(k+6) with with yy(k) in the third line of
the code?
1
We will see in the next section how to write and execute a sequence of MATLAB commands such as these.
2.2
Plotting
Plotting is easy in MATLAB, and you began to get some experience with how to plot in the first lab.
Again, you are encouraged to explore some of the capabilities of the MATLAB plotting function by
typing
>> help plot
Here we will continue exploring the plot command and see some of the other things that it can
do. First, make a plot of a sinusoidal signal that is similar to the one that you created in part (b)
of Section 2.1 as follows,
>>
>>
>>
>>
tdur=0.1;
t=0:tdur/100:tdur;
x=cos(2*pi*10*t);
plot(x)
These commands will plot one period of a 10 Hz sinusoid using 101 samples of the sinusoid over
the interval t = 0 to t = 0.1.
This plot does not have the labels that we would like it to have. Note, for example, that the
horizontal axis is indexed by n, which represents the indices of the values stored in the vector x.
What we would like is to label the axes with values of time that go from t = 0 to t = 0.1. A simple
way to do this is to replace plot(x) with plot(t,x) as follows,
>> plot(t,x)
With this syntax, values that are stored in the vector x (instead of the index) are plotted versus
the values that are stored in the vector y.
Some other commands that are useful in labeling plots and making them more descriptive are
the following:
>>
>>
>>
>>
>>
title(’Example Plot’)
grid on
xlabel(’t (sec)’)
ylabel(’Magnitude’)
legend(’10 Hz Sinusoid’)
%<-%<-%<-%<-%<--
Makes a Title for your graph
Creates a grid on your plot
Label for x-axis
Label for y-axis
Add a legend
Note that once the legend is created, you may click and drag it anywhere on the plot.
2.3
Printing to a File
Once you have created a plot, it is useful to be able to print your plot to a file so that you may
then use it in a lab report. If you would like to save a plot as a pdf file, type:
>> print -dpdf myplot
and your plot will be saved with the filename myplot.pdf. Before including this pdf file in a report,
you may want to crop it so that it does not fill up a whole page. There are many other drivers that
allow you to save your plot in different formats, such as tiff, jpeg, and png. Again, see the help
file on plot for more information.
Finally, another useful MATLAB function is subplot, a command that creates multiple plots in
tiled positions. More specifically,
>> subplot(mnp)
%<-- or you may use subplot(m,n,p)
breaks the figure window into an m-by-n matrix of axes (m rows and n columns), and selects the
p-th axes for the current plot. The axes are counted along the top row of the Figure window, then
the second row, etc. For example,
>> subplot(211), plot(current)
>> subplot(212), plot(voltage)
creates a plot of current in the top half of the window and voltage in the bottom half. If, instead,
two side-by-side plots were desired, one would type
>> subplot(121), plot(current)
>> subplot(122), plot(voltage)
2.4
MATLAB Script Files
It is very useful to be able to type a sequence of MATLAB commands to execute and save them
in a file so that they may be run multiple times, or so that you may edit them to change their
function. These MATLAB programs, or script files, may be created by invoking the MATLAB
editor, entering the commands, and then either running the script file from the editor, or saving
them to a file so that they may be run from the MATLAB command line.
(a) Start the built-in MATLAB editor by typing:
>> edit
and use the editor to create a script file containing the following lines:
>>
>>
>>
>>
>>
>>
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, title(’Test plot of Sinusoid’)
xlabel(’Time (sec)’)
%<--- plot a sinusoid
Examine each line of code to make sure that you understand the syntax and what it does.
(b) To run this script you may click on the run button at the top of the editor window, but you
will first be asked to save the file. You may also type a filename after the edit command
and then simply run it when you are done editing, either using the run button at the top
or by returning to the MATLAB command line and typing the name of the script you have
created. Later we will learn how to create functions that allow you to pass parameter values
to a script from the command line, such as
>> A=1, f0=440, phi=0;
>> mysine(A,f0,phi);
where the mysine script is expecting to be passed values for the amplitude, frequency and
phase of the sine function that it is to create.
(c) Explain why the plot of real(zz) that is created in the script file that you created is a
sinusoid. What is its phase and amplitude? Calculate (estimate) the phase shift of the
sinusoid from the plot.
(d) Run your script that you created.
2.5
MATLAB Sound
The exercises in this section involve sound signals, so you should bring headphones to the lab for
listening.
(a) Run the MATLAB sound demo by typing xpsound at the MATLAB prompt. If you are unable
to hear the sounds in the MATLAB demo then ask an instructor for help.
Remember: When unsure about a command, use help.
(b) Generate a tone (i.e., a sinusoid) in MATLAB and listen to it with the soundsc() command.2
Refer back to part (a) of Section 2.4 for some code that creates values of a sinusoid. The
frequency of your sinusoidal tone should be 1300 Hz and its duration should be 0.9 sec. Use a
sampling rate fs equal to 11025 samples/sec. Remember that the sampling rate defines the
length of the interval between samples of the sinusoid, 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 of your tt vector?
3
Laboratory: Manipulating Sinusoids with MATLAB
You are now done with the pre-lab and prepared to work the following exercises that are to be
written up in your lab report.
3.1
Sums of Sinusoids
Write a MATLAB script file to do steps (a) through (d) below. Include a listing of the script file in
your report.
(a) Generate a time vector tt to cover a range of values of time t that will span approximately
two cycles of a 4000 Hz sinusoid. If we use T to denote the period of the sinusoid, define
the starting time of the vector tt to be equal to −T , and the ending time as +T so that the
two cycles will include t = 0. 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.
2
The soundsc(xx,fs) function requires two arguments: the first one (xx) contains the vector of data to be played,
the second argument (fs) is the rate for playing the samples. In addition, soundsc(xx,fs) does automatic scaling
and then calls sound(xx,fs) to actually play the signal.
(b) Using the time vector that you created in part (a), generate two 4000 Hz sinusoids with
amplitudes A1 and A2 and time delays t1 and t2 ,
x1 (t) = A1 cos(2π(4000)(t − t1 ))
x2 (t) = A2 cos(2π(4000)(t − t2 ))
where these amplitudes and time shifts are defined as follows. Let A1 be equal to your age
and set A2 = 1.2A1 . For the time-shifts, set t1 = (37.2/M )T and t2 = −(41.3/D)T where D
and M are the day and month of your birthday, and T is the period of the sinusoid. In your
lab report, make sure to document the values that you used for M and D.
Make a plot of both signals over the range of −T ≤ t ≤ T . Use subplot(311) and
subplot(312) to make a three-panel subplot that puts these two plots in the top two panels
in the figure window.
(c) Create a third sinusoid that is the sum of x1 (t) and x2 (t),
x3 (t) = x1 (t) + x2 (t)
In MATLAB 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(313).
(d) Before saving these three plots to a file, put a title on each subplot, and include your name
in one of the titles. You may want to see help orient, especially orient tall.
3.2
Theoretical Calculations
Remember that the phase of a sinusoid can be calculated after measuring the time location of a
positive peak,3 if we know the frequency.
(a) Make measurements of the “time-location of a positive peak” and the amplitude from the plots
of x1 (t) and x2 (t), and write those values for Ai and ti directly on the plots. Then calculate
(by hand) the phases of the two signals, x1 (t) and x2 (t), by converting each time-shift ti to
phase. Note: when doing computations, express phase angles in radians, not degrees!
(b) Measure the amplitude and time-shift of x3 (t) directly from the plot and then calculate the
phase φ3 by hand. In your report, show how the amplitude and time-shift were measured,
and how the phase was calculated.
(c) Now use the phasor addition theorem. Carry out a phasor addition of complex amplitudes
for x1 (t) and x2 (t) to determine the complex amplitude for x3 (t). Use the complex amplitude
for x3 (t) to verify that your previous calculations of A3 and φ3 were correct.
3
Usually we say time-delay or time-shift instead of the “time location of a positive peak.”
3.3
Complex Amplitude
(a) Write one line of MATLAB code that will generate values of the sinusoid x1 (t) above by using
the complex-amplitude representation:
x1 (t) = <e{Xejωt }
(b) Write one line of MATLAB code that will calculate the magnitude of the sinusoid that is the
given by
x4 (t) = x1 (t) − 2x2 (t)
and write one line of MATLAB code that will calculate the phase of x4 (t).
© Copyright 2026 Paperzz