Digital Sound & Music: Concepts, Applications, & Science, Chapter 3, last updated 6/25/2013
The variables majors and minors hold arrays of integers, which can be created in
MATLAB by placing the integers between square brackets, with no commas separating them.
This is useful for defining, for each note in a diatonic scale, the number of semitones that the
note is away from the key note.
The variable scale is also an array, the same length as majors and minors (eight elements,
because eight notes are played for the diatonic scale). Say that a major scale is to be created.
Each element in scale is set to , where majors[i] is the original value of
element i in the array majors. (Note that arrays are numbered beginning at 1 in
MATLAB.) This sets scale equal to [1, , , , , , , ]. When
the start note is multiplied by each of these numbers, one at a time, the
frequencies of the notes in a scale are produced.
The line
x = [1:sr]/sr;
creates an array of 44,100 points between 0 and 1 at which the sine function is
evaluated. (This is essentially equivalent to x = linspace(0,1, sr), which we used in previous
A3 with a frequency of 220 Hz is used as a reference point from which all other
frequencies are built. Thus
startnote = 220*(2^((startnoteoffset+3)/12));
sets the start note to be middle C plus the user-defined offset.
In the for loop that repeats for eight seconds, the statement
outarray(1+(i-1)*sr:sr*i) = sin((2*pi*scale(i))*t);
writes the sound data into the appropriate section of outarray. It generates these samples by
evaluating a sine function of the appropriate frequency across the 44,100-element array x. This
statement is an example of how conveniently MATLAB handles array operations. A single call
to a sine function can be used to evaluate the function over an entire array of values. The
scale = scale/12;
works similarly, dividing each element in the array scale by 12. The statement
scale = 2.^scale;
is also an element-by-element array operation, but in this case a dot has to be added to the
exponentiation operator since ^ alone is matrix exponentiation, which can have only an integer
Equal vs Just
Previous Page Next Page