Arrays and Sound: In-Lab Section
Arrays and Sound: In-Lab Section
There also is a help desk (formatted in HTML for viewing from a Web
browser) with useful introductory material. It is accessed from the Help menu.
If you have no prior experience with MATLAB, see the topic “Getting Started” in
the help desk. Spend some time with this. You can find in the help desk all the
information you need to carry out the following exercises.
1. A variable in MATLAB is an array. An array has dimensions N × M , where
N and M are in Naturals. N is the number of rows and M is the number of
columns. If N = M = 1, the variable is a scalar. If N = 1 and M > 1, then the
variable is a row vector. If N > 1 and M = 1, then the variable is a column
vector. If both N and M are greater than one, then the variable is a matrix,
and if N = M , then the variable is a square matrix. The coefficients of an
array are real or complex numbers.
(a) Each of the following is an assignment of a value to a variable called
array. For each, identify the dimensions of the array (M and N ), and
6 Laboratory Manual
25
!
k.
k=0
Use a for loop (try help for) to specify each individual addition in the
summation.
L.1 Arrays and sound 7
(b) Use the sum function to give a more compact, one-line specification
of the sum in part (a). The difference between these two approaches
illustrates the difference between using MATLAB and using a more tra-
ditional programming language. The for loop is closer to the style one
would use with C++ or Java. The sum function illustrates what MATLAB
does best: compact operations on entire arrays.
(c) In MATLAB, any built-in function that operates on a scalar can also
operate on an array. For example,
>> sin(pi/4)
ans =
0.7071
ans =
ans =
1 4 9 16
Use this pointwise multiply to tabulate the values of sin2 for the set
(b) Sketch by hand the sine wave x : [−1, 1]→ Reals, given by
In your sketch carefully plot the value at time 0. Assume the domain
represents time in seconds. What is the frequency of this sine wave in
Hertz and in radians per second, what is its period in seconds, and how
many complete cycles are there in the interval [−1, 1]?
(c) Sample the function x from the previous part at 8 kHz, and using
MATLAB, plot the samples for the entire interval [−1, 1]. How many
samples are there?
(d) Change the frequency of the sine wave from the previous section to 440
Hz and plot the signal for the interval [−1, 1]. Why is the plot hard to
read? Plot the samples that lie in the interval [0, 0.01] instead (this is a
10-msec interval).
(e) The MATLAB function sound (see help sound) with syntax
>> sound(sampledSignal, frequency)
and
>> sound(2*sampledSignal,frequency)
where sampledSignal is the signal you created in part (d) above. Ex-
plain in what way are these different from what you heard in the previous
part. Listen to
>> sound(sampledSignal,frequency/2)
and
>> sound(sampledSignal,frequency*2)