Aqua Phoenix
     >>  Lectures >>  Matlab 2  


2.3 Assignment - Lecture 2

For this assignment you will experiment with vectors and matrices in the form of sound files and images, while getting a sense for Matlab. You will solve several small problems that amount up to 2 exercises. You should use the exercise from class as a guide to solving this assignment.

One of the main foci of this assignment is for you to get a really good grip on knowing the difference between rows and columns. Make sure that you understand the difference and how to handle them well!

Use vector and matrix operations where necessary. This includes arithmetic and indexing operations. For example, when asked to divide one vector by another, do it is not permitted to perform this operration explicitely on each pair of numbers - you should use vector operations instead.

Suppress the Command Window output of all steps, unless otherwise noted.

2.3.1 Part A

  blackrectangle.bmp   Black Rectangle Bitmap
  1. Download the image file above.
  2. Load it in Matlab and assign it to a variable.
  3. Display the image.
  4. For each color ( Red, Green, and Blue), extract the sub-matrix between rows 558:643 and columns 622:703 and assign each of the 3 sub-matrices to a variable (for example, subimageR, subimageG, subimageB). Display each of the three images.
  5. Pick the image that tells you the most - one of the images contains more information that you expect. If all of the images are blank, you should revisit the previous step (Hint: Did you perhaps confuse rows and columns?).
  6. Convert the chosen image to data type double and assign it to a new variable. Use this new variable from here on.
  7. Extract the vector between rows 13:80 and column 24 and assign it to the variable vectorA
  8. Extract the vector between rows 13:80 and column 45 and assign it to the variable vectorB
  9. Extract the vector between rows 13:80 and column 46 and assign it to the variable vectorCNow, perform the following operations:

  10. Exponentiate vectorA to vectorB (element-by-element) and assign the result to a new variable.
  11. Multiply the resulting variable by vectorB (element-by-element) and assign the result to a new variable.
  12. Add the previous result to vectorC and assign this result to a new variable.
  13. Now add the scalar 32 to the previous result and assign this result to a new variable.
  14. Now use function char() on the result, e.g. if the previous result is stored in variable A, use char(A). Make sure not to use a semicolon for this last step. Read the result in plain English. If the result does not make sense, you should go over this assignment again.
  15. Finally, transpose the result from char() from the previous step, in order to put the phrase in one row as opposed to one column. Do not supress the output from this step.

2.3.2 Part B

  authoritah.wav   Sound file from Southpark
  1. Download the sound file above.
  2. Load the sound file and assign it to a variable, e.g. soundfile
  3. Play the sound file to make sure that it has been loaded correctly.
  4. Assign the waveform sin([0:0.4:pi*150]) to a variable.
  5. Play the waveform. You will use this waveform to replace a part of authoritah.wav
  6. Extract the word "authoritah" from the sound file, just like it has been done in the in-class exercise, and assign it to a new variable. The word is between indices 9800:length( soundfile)
  7. Find out the length of the extracted word's sound file using either function length() or size(). The waveform resembling a beep will need to be of this size in order to replace the word "authoritah". Do not suppress the output from this step.
  8. To find out how many times the sine wave needs to be repeated, compute the following: x = length * 0.4/pi where length is the previously computed length. Note that the variable pi is already defined in Matlab.
  9. Generate the beep waveform of the correct length as follows: sin([0.4:0.4:pi*x]) and assign the result to a new variable.
  10. Verify that the length of the word " authoritah" is the same as that of the new waveform. Do not suppress the output from this step.
  11. Now, replace the sound " authoritah" between indices 9800:length(soundfile) of the original sound file with the new waveform. You must use matrix-subscripting (indexing) for this step. It is not acceptable to concatenate different matrices in order to produce the result of this step.
  12. Play back the changed wave form. The sound " authoritah" should now be replaced by a beep.

2.3.3 Part C

Build a small image with color lettering " HELLO". Generate 3 matrices, one for each of the color intensity layers: red, green, and blue. The matrices should be sized to 7 rows and 21 columns. Populate each matrix with color intensity values to form the word "HELLO", with H: red, E: green, L: blue, L: yellow, O: cyan, and all remaining pixels (background): black. The bitmap image is as follows:

2 H H EEE L   L    O  
3 H H E   L   L   O O 
4 HHH EEE L   L   O O 
5 H H E   L   L   O O 


  1. Red Layer: Generate a 2D matrix in variable r sized to 7 rows and 21 columns with all zeros. Have a look at function zeros!
  2. Green Layer: Generate the same matrix in variable g.
  3. Blue Layer: Generate the same matrix in variable b.
  4. Populate each of the matrices (in any order) with pixel intensity values of 255. You'll have to decide what matrices to place these values in to generate the correct color, but you must follow the instructions below on whether to place individual pixels, row vectors, or columns vectors. In other words, if the assignment asks you to place a column vector, you should do so with one command, and not several sub-steps.

    Hint: Recall from lecture how to replace individual elements in matrices, and how to replace rows, columns, and sub-matrices: In an assignment where the left-hand side denotes a destination and the right-hand side denotes a value, the dimensionality of the value must match the dimensionality of the destination (see lecture 1). For example, to place a column vector C in matrix M, the left-hand side should address the correct location of C, and the right-hand side should resemble a column vector of equal size with a set of new values.

    • H:
      • Column vector in column 2 and row 2-6,
      • Column vector in column 4 and row 2-6,
      • Pixel at row 4 and column 3.
    • E:
      • Row vector in row 2 and column 6-8,
      • Row vector in row 4 and column 6-8,
      • Row vector in row 6 and column 6-8,
      • Pixel value at row 3 and column 6,
      • Pixel value at row 5 and column 6.
    • L:
      • Column vector in column 10 and row 2-6,
      • Row vector in row 6 and column 11-12.
    • L:
      • Column vector in column 14 and row 2-6,
      • Row vector in row 6 and column 15-16.
    • O:
      • Column vector in column 18 and row 3-5,
      • Column vector in column 20 and row 3-5,
      • Pixel value at row 2 and column 19,
      • Pixel value at row 6 and column 19.

  5. Generate a 3D matrix in variable hello by merging the 3 color intensity matrices. The individual color intensity matrices are 2-D with columns and rows. In order to create a 3-D matrix with columns, rows, and layers (depth), you must address variable hello at the appropriate depth values, and place the 2-D matrices at those locations. Have a look at lecture 2 for examples on 3-D indexing.
  6. Display the matrix in the Command Window. Do not supress the output for this step.
  7. Convert the type of 3-D matrix hello to data type uint8.
  8. Display the image represented by matrix hello.