Lab-1: Matlab/Octave and sound processing


The goal of this first lab is to gain familiarity with Matlab/Octave in the context of sound processing. Octave is available as Free Software and can be downloaded from http://www.octave.org/download.html and its manual is available at http://www.gnu.org/software/octave/docs.html. For information on Matlab go to The MathWorks site.

[From now on the labs are presented as if you would use Octave, but there is basically no difference with Matlab]

1.1 Basic commands

The following exercises will begin your orientation in Octave.

(a) Open an "xterm" and type "octave". To be able to read and write sound you need to download a sound from Freesound.org (ex: corto.wav). Put it in the directory where you started octave.

(b) Explore the Octave help capability. Type each of the following lines to read about these commands:

(c) Use Octave as a calculator. Try the following:

(d) Variable names can store scalars and matrices in Octave. Try the following:

(e) Complex numbers are available in Octave. Notice that the names of some basic operations are unexpected, e.g., abs for magnitude. Try the following:

(f) Plotting is easy in Octave, for both real and complex numbers. The basic plot command will plot a vector yy versus a vector xx. Try the following:

Drop the semicolons, if you want to display the values in the x, y, and z vectors. xx.*xx denotes an element wise calculation in contrast to a matrix multiplication xx*xx . When unsure about a command, use help.

1.2 Array indexing

(a) Make sure that you understand the colon notation. In particular, explain what the following Octave code will produce

(b) Extracting and/or inserting numbers in a vector is very easy to do. Consider the following definition:

Explain the result echoed from the last three lines of the above code.

(c) In the previous part, the vector xx contains 12 elements. Observe the result of the following assignment:

Now write a statement that will replace the odd-indexed elements of xx with the constant -77 (i.e., xx(1), xx(3), etc). Use a vector indexing and vector replacement.

1.3 Use vectors instead of loops

(a) Experiment with vectors in Octave. Think of a vector as a list of numbers. Try the following:

Explain how the last example computes the different values of cosine without a loop. The text following the % is a comment; it may be omitted. If you remove the semicolon at the end of the first statement, all the elements of kset will be echoed to the screen.

(b) Vectorization is an essential programming skill in Octave. Loops can be done in Octave, but they are not the most effcient way to get things done. It's better to avoid loops and use the vector notation instead. For example, the code below uses a loop to compute values of the sine function. Rewrite this computation without using the loop (as in the previous part).

1.4 Script files

(a) Use an editor such as emacs or notepad (on UNIX or DOS), to create a script file called funky.m containing the following lines:

Save the script e.g. in the folder octave_files.

(b) Run your script from Octave. To run the skript funky that you created in part (c), try

(c) Add three lines of code to your script, so that it will plot a cosine xx2=0.5*cos( 2*pi*0.789*tt ) on top of the sine. Two graphs can be plotted on top of each other by "b" denotes blue color, "r" denotes red color

1.4 Waves

(a) Now generate a tone (i.e., a sinusoid) and listen to it with the sound command. The frequency of your tone should be 2 KHz and the duration should be 1 sec. The following lines of code should be saved in a file called mysound.m and run from the command line.

The format of the wavwrite command might be different for your version of octave. if you get an error from the above code, try to figure out the right syntax using "help wavwrite". Play the sound file "sound.wav" outside Octave with any sound player, for example "play sound.wav" from the commandline. You can use wavread to read in a sound from a file.

1.5 Functions

The following warm-up exercises are to help you in writing functions in Octave. Although these examples contain minor errors, they do exemplify the correct structure and syntax for writing functions.

(a) Find the mistake in the following function:

(b) Find the mistake in the following function:

(c) Explain the following lines of Octave code:

(d) Write a function that performs the same task as the following without using a for loop.

1.6 Vectorization

(a) Explain the following lines of Octave code:

(b) Write a new function that performs the same task as the following function without using a for loop. Use the idea in part (a). In addition, the Octave logical operators are summarized via help relop.

1.7 Manipulating sinusoids

Generate two 3000 hertz sinusoids with different amplitudes and phases.

(a) Select the value of the amplitudes as follows: let A1 = 13 and use your age for A2. For the phases, use the last two digits of your telephone number for phi1 (in degrees), and take phi2 = -30degrees.

(b) Make a plot of both signals over a range of t that will exhibit approximately 3 cycles. Make sure the plot starts at a negative time so that it will include t = 0, and make sure that your have at least 20 samples per period of the wave.

(c) Verify that the phase of the two signals x1(t) and x2(t) is correct at t = 0, and also verify that each one has the correct maximum amplitude.

(d) Use subplot(3,1,1) and subplot(3,1,2) to make a three-panel subplot that puts both of these plots on the same window. See help subplot.

(e) Create a third sinusoid as the sum: x3(t) = x1(t) + x2(t). In Octave this amounts to summing the vectors that hold the samples of each sinusoid. Make a plot of x3(t) over the same range of time as used in the previous two plots. Include this as the third panel in the window by using subplot(3,1,3).

(f) Measure the magnitude and phase of x3(t) directly from the plot. Explain how the magnitude and phase were measured.

1.8 Reading and understanding sounds

A digital sound is characterized by its sampling rate (FS) in Hertzs and the number of bits per sample (BITS).

(a) Read the sound file you downloaded in section 1.1 using the function WAVREAD.