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.
Command Window output of all steps, unless otherwise noted.
2.3.1 Part A
- Download the image file above.
- Load it in Matlab and assign it to a variable.
- Display the image.
- 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.
- 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?).
- Convert the chosen image to data type double and assign it to a new variable. Use this new variable from here on.
- Extract the vector between rows 13:80 and column 24 and assign it to the variable
- Extract the vector between rows 13:80 and column 45 and assign it to the variable
- Extract the vector between rows 13:80 and column 46 and assign it to the variable
vectorCNow, perform the following operations:
vectorA to vectorB (element-by-element) and assign the result to a new variable.
- Multiply the resulting variable by
vectorB (element-by-element) and assign the result to a new variable.
- Add the previous result to
vectorC and assign this result to a new variable.
- Now add the scalar
32 to the previous result and assign this result to a new variable.
- 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.
- 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
- Download the sound file above.
- Load the sound file and assign it to a variable, e.g.
- Play the sound file to make sure that it has been loaded correctly.
- Assign the waveform
to a variable.
- Play the waveform. You will use this waveform to replace a part of
- 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(
- 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.
- 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.
- Generate the beep waveform of the correct length as follows:
and assign the result to a new variable.
- 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.
- 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.
- 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:
|1|| || || || || || || || || || || || || || || || || || || || ||
|2|| ||H|| ||H|| ||E||E||E|| ||L|| || || ||L|| || || || ||O|| ||
|3|| ||H|| ||H|| ||E|| || || ||L|| || || ||L|| || || ||O|| ||O||
|4|| ||H||H||H|| ||E||E||E|| ||L|| || || ||L|| || || ||O|| ||O||
|5|| ||H|| ||H|| ||E|| || || ||L|| || || ||L|| || || ||O|| ||O||
|6|| ||H|| ||H|| ||E||E||E|| ||L||L||L|| ||L||L||L|| || ||O|| ||
|7|| || || || || || || || || || || || || || || || || || || || ||
- 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!
- Green Layer: Generate the same matrix in variable
- Blue Layer: Generate the same matrix in variable
- 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.
- 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.
- 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.
- Column vector in column 10 and row 2-6,
- Row vector in row 6 and column 11-12.
- Column vector in column 14 and row 2-6,
- Row vector in row 6 and column 15-16.
- 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.
- 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.
- Display the matrix in the
Command Window. Do not supress the output for this step.
- Convert the type of 3-D matrix
hello to data type uint8.
- Display the image represented by matrix