Unformatted text preview:

Lab 2: More MATLAB and Working with SoundsEE299 Winter 2008Due: In lab, on or b e fore 25 January 2008.You will need: an EE computer account for accessing the web server, and a pair of headphones forlistening to sounds.ObjectiveIn this lab, you will learn a few more skills in MATLAB. In the following sections, we cover:• Use of M-files and for loops in MATLAB• Building sounds from sinusoids• Reading, writing and recording sounds• Use MATLAB commands to display the frequency content of signals.The TA will pick different people to run the M-files that you create in this lab. You can work together toimplement them, and different people can work on different aspects of the lab, but you will each need to beable to demonstrate any of the parts and answer questions about them.PrelabIf you do not already have an EE computer account, you will nee d to get one. Seehttps://www.ee.washington.edu/computing/faq/new account.htmlfor more information. It is a good idea to do this at least a day before your lab, so that there is time toresolve any problems should they arise.One of the exercises that you will do is to synthesize a short sequence of notes from some song. Forthis part of the lab, you should figure out your song ahead of time, and get a musical score with the basicnotes and their lengths (whole, quarter, etc.). While you can do chords if you want to be more ambitious,but you are not required to, and you should plan on a simple single-note-at-a-time version for your firstimplementation to simplify script debugging.In addition, you will be recording some sound in the lab. If if you do not want to record your own voice,you might want to bring s ome sort of musical instrument or noisemaker to record. Pick something that willhave frequency content that changes as a function of time.1 Introduction to MATLAB (Part II)In this part we cover a few more features in MATLAB. You will learn how to write scripts in MATLAB useM-files, and the basic programming concept of for lo ops. You will use these together to create a simple songwith sinusoids.11.1 For LoopsIn this section we will cover a basic programming tool: for is used to tell MAT LAB to do the same setof statements multiple times. If you are familiar with this concept from other programming languages thissection should be easy, because the concepts are the same in MATLAB—just the syntax may be different.The general form for for loops is:for variable = starting value : stepsize : endingvalue. . . some M AT LAB statements . . .endAt the beginning of the first loop, variable is set to startingvalue, and at the beginning of every subsequentloop, variable has stepsize added to it. Each time through the loop, the statements in the middle areexecuted. The loop stops when the new value of variable would be greater than endingvalue. If stepsize isleft out, the default value of 1 is used. (See the MATLAB help function for more information.)Consider this simple MATLAB code:z=0;for n=1:10z=z+n;endThe first line assigns the value 0 to the variable z. Then the for loop begins: the variable n is set to thefirst value 1. The single line within the for loop sets z to z+n, so on the first time through the loop z willbe set to 0 + 1 = 1. At the beginning of the next loop, the value of n is increased by 1 and the line withinthe loop is executed again with the new values of z and n,i.e. 1+2=3. The final loop occurs when n=10;then it stops. The MATLAB function sum(x), if x is a vector, adds all the elements in the vector x. Checkthe definition of the sum function in MATLAB help, and verify for yourself that the MATLAB code aboveis equivalent to z=sum(1:10).Another example of a for loop is:A=-5:5;L=length(A);for i=1:LB(i)=A(L+1-i);endWhat is the relationship between the resulting vector B and A? Verify for yourself that the same effect couldbe accomplished using the B=fliplr(A) operation in MATLAB.1.2 M-filesAs you learned in Lab 1, you can use MATLAB interactively by typing commands in the Command Window,and having MATLAB execute them one at a time. Another way to use MATLAB is to create a file containinga sequence of commands, called an M-file (its filename will end in “.m”), and run all the commands in theM-file at the end. M-files are very useful when the task you want to do requires several commands, sinceit saves work typing things in when you make a mistake. M-files can be saved, so they can be run multipletimes and easily changed if you want to do some step differently. Another reason is that for larger projects,it’s good to divide up your task into smaller pieces and M-files make this easier.There are actually two types of M-files: scripts and functions. We will learn about scripts in this lab,and functions in the next lab.A script is just a sequence of MATLAB commands, like those that you would type into the CommandWindow. MATLAB runs the commands in the M-file in order, as if they were typed into the CommandWindow. You can run an existing script if it is in your working directory. Try downloading the “b e ethoven.m”script1from the class web page, editing, and running that. We’ll step you through it here, but you maywant to see the document “Using MATLAB for EE students” (linked on the class webpage) for m ore details1The idea of synthesizing Beethoven’s Fifth is due to Virginia Stonick.2about creating folders, saving and uploading M-files, as well as several other details about using MATLABin the EE computer labs.Create a new folder named “Lab 2.” This folder can be anywhere on your computer. The M-files thatyou make in this lab will be kept temporarily in this folder, but you will need to save them somewherepermanent, like your home directory (EE account) when you are done. If there is already a “Lab 2” folderon the desktop, it c ould be from the other section or another course, so better to just use a different directoryor new name to make sure you don’t accidentally use someone else’s M-files.Move the “beethoven.m” file to this folder. Change the MATLAB “Current Directory” to the Lab 2folder (or whatever you called it): at the top of the MATLAB screen, click on the ‘...’ button (“browse forfolder”) next to the Current Directory label. See Figure 1. Then select the desired folder and click ok. Oneway to run the script is to simply type the name of the file (e.g. “beethoven” not “beethoven.m”) in theCommand window. Try it. If you are in the folder with the correct M-file, you should


View Full Document

UW EE 299 - Study Notes

Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?