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

35

Figure 2.38 Creating a square wave by adding four sine functions

You can see that it is beginning to get square but has many ripples on the top. Adding four more

terms gives further refinement to the square wave, as illustrated in Figure 2.39:

Figure 2.39 Creating a square wave by adding eight sine functions

Creating the wave in this brute force manner is tedious. We can make it easier by using

MATLAB's sum function and its ability to do operations on entire arrays. For example, you can

plot a 262 Hz square wave using 51 terms with the following MATLAB command:

fplot('sum(sin(2*pi*262*([1:2:101])*t)./([1:2:101]))',[0 0.005 -1 1])

The array notation [1:2:101] creates an array of 51 points spaced two units apart – in effect,

including the odd harmonic frequencies in the summation and dividing by the odd number. The

sum function adds up these frequency components. The function is graphed over the points 0 to

0.005 on the horizontal axis and –1 to 1 on the vertical axis. The operation causes the division

to be executed element by element across the two arrays.

The sawtooth wave is an infinite sum of all harmonic frequencies with diminishing

amplitudes, as in the following equation:

Let f be a fundamental frequency. Then a sawtooth wave created from this

fundamental frequency is defined by the infinite summation

∑

Equation 2.10

is a scaling factor to ensure that the result of the summation is in the range of 1 to 1.

The sawtooth wave can be plotted by the following MATLAB command:

0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01

-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01

-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1