Digital Sound & Music: Concepts, Applications, & Science, Chapter 2, last updated 6/25/2013

32

Figure 2.34 The sum of two sine waves

We've used the fplot function in these examples. This function makes it appear as if the

graph of the sine function is continuous. Of course, MATLAB can't really graph a continuous list

of numbers, which would be infinite in length. The name MATLAB, in fact, is an abbreviation

of "matrix laboratory." MATLAB works with arrays and matrices. In Chapter 5, we'll explain

how sound is digitized such that a sound file is just a array of numbers. The plot function is the

best one to use in MATLAB to graph these values. Here's how this works.

First, you have to declare get an array of values to use as input to a sine function. Let's

say that you want one second of digital audio at a sampling rate of 44,100 Hz (i.e., samples/s) (a

standard sampling rate). Let's set the values of variables for sampling rate sr and number of

seconds s, just to remind you for future reference of the relationship between the two.

sr = 44100;

s = 1;

Now, to give yourself an array of time values across which you can evaluate the sine

function, you do the following:

t = linspace(0,s, sr*s);

This creates an array of sr values, . Note that when you don't put a semi-

colon after a command, the result of the command is displayed on the screen. Thus, without a

semi-colon above, you'd see the 44,100 values scroll in front of you.

To evaluate the sine function across these values, you type

y = sin(2*pi*262*t);

One statement in MATLAB can cause an operation to be done on every element of an array. In

this example, y = sin(2*pi*262*t) takes the sine on each element of array t and stores the result

in array y. To plot the sine wave, you type

plot(t,y);

Time is on the x-axis, between 0 and 1 second. Amplitude of the sound wave is on the vertical

axis, scaled to values between 1 and 1. The graph is too dense for you to see the wave

properly. There are three ways you can zoom in. One is by choosing Axes Properties from the

0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05

-2

-1

0

1

2