Description
EE 235 Lab 1 Part One: Introduction to Matlab
In this lab, you will work through a series of exercises to get you started in Matlab (“Matrix Laboratory”). You will learn how to use Matlab variables, perform basic operations, and write your own script files. Note: • Pre-labs should be done individually, and turned in at the beginning of the lab. Each student will be checked off by the TA during the lab section. • All in-lab exercises and the lab report should be completed in groups of 2-3. • No lab report this week – lab report will be with part 2 next week What Is Expected From You In Lab 1 Part One Completion of the Prelab exercise: show to the TA at beginning of lab (2 points) Completion of 2 in-lab check offs with TA (2 points) PRE-LAB Read the GettingStarted tutorial and familiarize yourself with the Matlab user interface. This material will be needed to answer the prelab questions and in the lab. 1. Set up the Remote Desktop Connection. Create a new folder by right-clicking in CURRENT FOLDER window and selecting New Folder. Call this folder Lab 1. Doubleclick the folder Lab 1 and then press “Select Folder”. You will see that your “Current Folder” now shows you are in the Lab 1 directory that you specified. Create a script template for future use: Write your name, lab section, and the date in your EDITOR window, save the file as template.m. Make a screenshot showing the Matlab interface with the specified folder and script template. Exit Matlab and then restart it. Make sure you can find this file again, both through Matlab and using a folder browser outside of Matlab. 2. While we will be working on paper with continuous-time signals, we need to use discrete-time representations of those signals in lab. a. Give an equation defining sampling time TS in terms of sampling frequency FS b. How many time samples are in a vector created by the Matlab command t=0:0.5:2 ? IN-LAB For the following exercises, refer to the GettingStarted tutorial, specifically Sections Matlab Variables and Basic Operations for Exercise 1 and Section Creating a Signal that is a Function of Time for Exercise 2. Ask your TA if something isn’t clear. 1. Lab Exercise #1: M-files We will write code using M-files which have the extension *.m. M-files can be used as scripts that contain a sequence of commands that are executed exactly as if they were manually typed into the Matlab COMMAND window. a) Open up a new script by doing the following: Go to Home tab -> New Script Developed by Eldridge Alcantara (Spr 2015); modified by Trang Tran & Mari Ostendorf (Aut 2016, 2017) Page 2 of 3 b) Save your script: Go to Editor tab -> Save. Then, save file as Ex1.m c) On the Matlab script window on the upper center window, first copy or type in the following lines of code and comments (which are done in Matlab using the % symbol): d) Under PART A: i) Write the line of code to create a 3 x 1 column vector called y1 with values 4, 6, and 2. Suppress the output using a semicolon at the end of the line of code. ii) Write the line of code to a 3 x 3 matrix called z, where the first row has all ones, the second row has values 3, 6, and 9, and the third row has all zeros. Again, suppress the output by using a semicolon at the end of the line of code. iii) Test your current script by pressing or by going to Editor -> Run. You should see your two variables y1 and z on your WORKSPACE window. e) Under PART B: i) Write the line of code to extract the value 6 in vector y1, and store that value in variable c. Display the output so we can see the value of c on the COMMAND window. Therefore, omit the semicolon at the end of the line of code. ii) Using the colon operator, write the line of code to extract the last two elements in vector y1 and store them in variable c1. Display the output by omitting the semicolon. iii) Test your script so far by pressing or by going to Editor -> Run. You should see the variables y1, z, c, and c1 on your WORKPSACE window. You should also see the values of c and c1 displayed on the COMMAND window. f) Under PART C: i) Write the line of code to access the value 9 in matrix z, and store that value in variable d. Display the output on the COMMAND window. ii) Using the colon operator, change the values of matrix z so that the last two columns in the 3rd row are -1. Display the output on the COMMAND window. iii) Test your script so far by pressing or by going to Editor -> Run. You should see the variables y1, z, c, c1, and d on your workspace window. You should also see the values of c, c1, d, and z displayed on the COMMAND window. g) Under PART D: % FILE: Ex1.m % NAME: [FILL IN NAME HERE] % DESCRIPTION: Matlab Variables and Basic Operations % Clear all variables and close all windows clear all; close all; % PART A % PART B % PART C % PART D Developed by Eldridge Alcantara (Spr 2015); modified by Trang Tran & Mari Ostendorf (Aut 2016, 2017) Page 3 of 3 i) Write the line of code to get the number of columns in vector y1, and store that value in variable e. For this part, display the output on the COMMAND window. ii) Test your script so far by pressing or by going to Editor -> Run. CHECKOFF #1: Demonstrate your script for Ex1.m with your Lab TA. 2. Lab Exercise #2: Defining signals a) Open up a new script and save it as Ex2.m b) Copy or type in the following for Ex2.m: c) For PART A: Write the lines of code to define the following two signals x(t) = 0.5t and y(t) = t2. Both signals will be defined over the range 0 ≤ t ≤ 3 with a sampling frequency of 2. d) For PART B: Write the line of code to define a new signal z(t), where z(t) = x(t) – 2y(t). e) For PART C: Write the line of code to access z(t) at t = 2, and store the result in variable w1. Display the output on the COMMAND window. f) For PART D: Write the lines of code to extract values of z(t) from 0 ≤ t ≤ 1.5, and store the values in variable w2. Display the output on the COMMAND window. g) Run and test your script. CHECKOFF #2: Demonstrate your script for Ex2.m with your Lab TA. % FILE: Ex2.m % NAME: [FILL IN NAME HERE] % DESCRIPTION: More Matlab Variables and Basic Operations % Clear all variables and close all windows clear all; close all; % PART A % PART B % PART C % PART D
EE 235 Lab 1 Part Two:
Introduction to Matlab
In this lab, you will work through another series of exercises to finish off your introduction to
Matlab (“Matrix Laboratory”). Note: All lab exercises and the lab report should be completed
in groups of 2-3, with the same people as for Lab 1 Part One.
What Is Expected From You In Lab 1 Part Two
Completion of the Prelab Exercise (1 point)
Completion of 2 more in-lab check offs with TA (2 points)
Completion of a lab report summarizing all 4 exercises (3 points)
PRE-LAB:
Read the Matlab tutorials on plotting and sound file basics (Sections Matlab Plotting Basics and
Working with Sound Files in the Getting Started document) and type “help sound” in Matlab to
see how the command works.
1. Specify the command that you would use to plot the first of two signals that are
displayed with one over the other.
2. Explain the difference between sound(y) and sound(y,Fs)
IN-LAB:
Complete Exercises 3 and 4, referring to the tutorials on Matlab Plotting Basics and Working
with Sound Files if you are stuck.
1. Lab Exercise #3: Plotting functions
In this exercise, we will learn the basics of plotting functions in Matlab. Open a new script
and call it Ex3.m with the outline below:
a) For PART A: Define a signal x(t) = 2 – t over the range -2 ≤ t ≤ 4 with a sampling
frequency of Fs = 5. Plot x(t) vs. t on a 1 x 2 figure window as the 1st subplot. Adjust
range of x-axis to be between -5 and 5, as well as range of y-axis to be between -5 and 5.
Turn on the grid and be sure to label plot and axes appropriately.
% FILE: Ex3.m
% NAME: [FILL IN NAME HERE]
% DESCRIPTION: Matlab Plotting Basics
% Clear all variables and close all windows
clear all;
close all;
% PART A
% PART B
% PART C
b) For PART B: Using the same time samples from (a), define and plot y(t) = -0.5t2 on the
2nd subplot. Adjust range of x-axis to be between -5 and 5, as well as range of y-axis to
be between -5 and 5. Turn on the grid and be sure to label plot and axes appropriately.
c) For PART C: Define a signal x(t) = cos(2πt) over the range 0 ≤ t ≤ 5. Create a 3×1 subplot
window and plot three versions of this signal with three different sampling frequencies
of Fs=10, Fs=1, and Fs=0.5. Turn on the grid and be sure to label plot and axes
appropriately. You should see that we do not get an accurate plot of x(t) for one or more
cases. This shows that the selection of the sampling frequency is very important, which
will be discussed in a future lab. For now, it is useful to know about this in debugging
Matlab problems.
CHECKOFF #3: Demonstrate your script for Ex3.m with your Lab TA.
Lab Report Question #1 of 2: Comment on why the cosine frequency would influence your
choice of Fs. Include the 3×1 plot in your lab report.
2. Lab Exercise #4: Working with sound files
In this last exercise, we will learn how to play and plot sound files. We will use sound files
that are already built into Matlab. Open a new script and call it Ex4.m with the outline
below:
a) For PART A:
i) Load sound file chirp.mat. Store y in variable chirpSound and Fs in chirpFs
ii) Load sound file gong.mat. Store y in variable gongSound and Fs in gongFs
b) In PART B:
i) Using chirpFs, compute the time samples for chirp and call that vector t_chirp
ii) Using gongFs, compute the time samples for gong and call that vector t_gong
% FILE: Ex4.m
% NAME: [FILL IN NAME HERE]
% DESCRIPTION: Sound Files and Wrap-Up
% Clear all variables and close all windows
clear all;
close all;
% PART A
% PART B
% PART C
% PART D
c) In PART C: Using the sound and time sample vectors from (a) and (b), plot the two
sound signals on one figure window using a 2 x 1 subplot window. No need to adjust
the axes or turn on the grid, but do label plot and axes appropriately.
d) In PART D: Play the two sound files in succession with a 4-second pause in between.
You can use the function pause to help you do this. Type help pause on the
COMMAND window for assistance on the appropriate function syntax.
CHECKOFF #4: Demonstrate your script for Ex4.m with your Lab TA.
Lab Report Question 2 of 2: Try playing the sound files with a different Fs and without
specifying Fs. Comment on the effect of changing Fs in your report.
LAB REPORT:
Turn in a PDF of your lab report (one per team) online via the link on the class website. A
sample format for the lab report is on the class website. You may vary from this format but
please include the basic sections. The report is due prior to the start of your next lab section. Lab
turn-in times will be checked, so no late lab reports will be accepted unless arranged in advance
with the instructor.

