Standard Wave Forms

The first section presents  computationally efficient ways to generate standard waveforms with Mathematica. The second section presents tradtional Fourier-synthesis means of generating waveforms.

Efficient Generation of Standard Waveforms

For each of the waveforms below, first a graphic formula and example are given, and then a sound formula and example.

The Fract function is used by all the following functions:

Fract[x_ ] := x - Floor[x]

Sawtooth Waves

Graphic Formula:

SawPlot[Freq_] := Plot[Fract[-t * Freq],{t,0,3/Freq}]

SawPlot[440]

Sound Formula:

SawPlay[Freq_,Dur_] := Play[Fract[-t * Freq],{t,0,Dur},
	SampleRate->44100]

SawPlay[440,0.5]

Square Waves

Graphic Formula:

SquarePlot[Frequency_] := 
		Plot[ 1/2(Sign[Fract[t * Frequency]-1/2]+1),
			{t,0,3/Frequency}]

SquarePlot[440]

Sound Formula:

SquarePlay[Freq_,Dur_] := 
	Play[1/2(Sign[Fract[t * Freq]-1/2]+1),{t,0,Dur},
		SampleRate->44100]

SquarePlay[1,440,0.5]

Triangle Waves

Graphic Formula:

Triangle[Freq_] := Plot[2 * Abs[Fract[t * Freq] - 1/2],
	{t,0,3/Freq}]

Triangle[440]

Sound Formula:

TrianglePlay[Freq_, Dur_] := 
	Play[2 * Abs[Fract[t * Freq] - 1/2],{t,0,Dur},
		SampleRate->44100]

TrianglePlay[440,0.5]

Fourier Synthesis of Sawtooth Waves

A sawtooth wave is a wave in which there is a fundamental and a theoretically infinite number of overtones.  The amplitude of each overtones is the inverse of its number: the amplitude of the second overtone is one-half that of the fundamental, the amplitude of the third overtone is one-third that of the fundamental,   and so on.

The approximate equation for a sawtooth wave is:

	e = A * sin(omega * t) + A/2 * sin(2 * omega * t) + A/3 * sin(3 * omega * t) +...+
		A/n * sin(n* omega * t)
	
or in Mathematica:

	e := Sum[A/i * Sin[ i * omega  * t ], {i,1,n}]

where 
	e = amplitude at time t
	omega = frequency in radians per second ( 2 * Pi * frequency in Hz)
	t = time in seconds
	A = maximum amplitude
	i = 1, 2, 3,..., n
	n = highest harmonic present
	
Practically, n * f should be less than 20,000 Hz, the upper limit of human hearing.

The sawtooth is a basic waveform used for sound synthesis because it contains all the harmonics of a given fundamental, and many musical sounds derive from sawtooth sound, such as string tone.

Plotting Sawtooth Waves

Below is a Mathematica function to plot sawtooth waves.  It takes three arguments:
 
	Amplitude:  	maximum amplitude of the wave
	Frequency:	frequency of the fundamental in hertz
	Harmonics:	number of the highest overtone

SawtoothWavePlot[Amplitude_, Frequency_, Harmonics_] := 
	Plot[Evaluate[
		Sum[Amplitude/i * Sin[i * 2 Pi Frequency t],
		{i,1,Harmonics}]],
		{t,0,3/Frequency}]

Here is a plot of a sawtooth wave at 440 Hz with five harmonics:

SawtoothWavePlot[1, 440, 5]

Here is the same wave plotted again, this time with 45 harmonics.

(Forty-five harmonics is the maximum number humans can hear when the fundamental is 440 Hz, since the frequency of the forty-fifth harmonic is 45 * 440 = 19800 Hz.)

SawtoothWavePlot[1, 440, 45]

Playing a Sawtooth Wave

Here is a Mathematica function to play a sawtooth wave, using the above formula.  The variables are the same with the addition of Duration.
	
		Duration:  duration of sound

The SampleRate is set to 44100, so that the highest harmonics can be heard.

SawtoothWavePlay[Amplitude_,Frequency_,Harmonics_,Duration_] := 
	Play[Evaluate[
		Sum[Amplitude/i * Sin[i * 2 Pi Frequency t],
		{i,1,Harmonics}]],
		{t,0,Duration},
		SampleRate->44100]

SawtoothWavePlay[1, 440, 45, 0.5]

Fourier Synthesis of Square Waves

A square wave is a wave whose component parts are a fundamental, and the odd members of its harmonic series. The amplitude of each overtone is proportional to the inverse of its number.  Thus, a square wave has half as many overtones present as a sawtooth wave.

The equation for a square wave is:

	e = A * sin(omega * t) + A/3 sin(3 *omega * t) + A/5 sin(5 * omega * t) + ...  	

An approximation with Mathematica:

	Sum[A/(2 i + 1) * Sin[ (2 i + 1) * omega * t],{i,0,n}]

where 	
	e = amplitude at time t
	A = maximum amplitude of the fundamental
	omega = frequency of the fundamental in radians
	t = time
	n = number of harmonics present
	
Many instruments (for example., the clarinet) have only odd harmonics present.

Plotting Square Waves

Below is a Mathematica function to plot square waves.  It takes three arguments:

	Amplitude: 	the amplitude of the fundamental
	Frequency: 	the frequency of the fundamental
	Harmonics: 	the number of harmonics present

SquareWavePlot[Amplitude_, Frequency_, Harmonics_] := 
	Plot[Evaluate[Sum[ 
		(Amplitude/(2 i + 1)) * 
		Sin[(2 * i + 1) * 2 Pi Frequency t],
			{i,0,Harmonics}]],
			{t,0,3/Frequency}]

Here is a plot of a square wave with ten harmonics, and a fundamental of 440 Hz

SquareWavePlot[1, 440, 10]

Playing Square Waves

Here is a Mathematica function to play a square wave.  It adds Duration to the three arguments above:

		Duration:  duration of the total sound
	
The SampleRate is set to 44100, so that the highest harmonics can be heard.

SquareWavePlay[Amplitude_, Frequency_, Harmonics_,Duration_] := 
	Play[Evaluate[Sum[ 
		(Amplitude/(2 i + 1)) * 
		Sin[(2 * i + 1) * 2 Pi Frequency t],
			{i,0,Harmonics}]],
			{t,0,Duration},
			SampleRate->44100]

Here is a square wave, with a fundemental frequency of 440 Hz and 22 harmonics present, the maximum number one could hear.

SquareWavePlay[1, 440, 22, 0.5]

Fourier Synthesis of Triangle Waves

A triangle wave is a wave whose component parts are a cosine fundamental, and the odd members of its harmonic series.  The amplitude of each overtone is proportional to the inverse of its number squared.  Notice that this results in the upper harmonics being much weaker than in a square wave, and, correspondingly, makes the sound of a triangle wave close to that of a sine wave.

The equation for a square wave is:

	e = A * cos(omega * t) + A/9 *cos(3 * omega * t) + A/25 *cos(5 * omega * t) + ...
	
or an approximation with Mathematica:

	e = Sum[ A/(2 i + 1)^2 * Cos[ (2 * i + 1) * omega * t],{i,0,n}]

where 
	e = amplitude at time t
	A = amplitude of fundemental
	omega = freqeuncy of fundamental in radians
	n = number of harmonics
	t = time

Plotting Triangle Waves

Below is a Mathematica function to plot triangle waves.  It take three arguments:

	Amplitude: 	amplitude of fundamental
	Frequency: 	frequency of fundamental
	Harmonics: 	number of harmonics

TriangleWavePlot[Amplitude_, Frequency_, Harmonics_] := 
	Plot[Evaluate[Sum[ 
		Amplitude/(2 i + 1)^2 * 
		Cos[(2 i + 1) * 2 Pi Frequency t ],
			{i, 0, Harmonics}]],
			{t,0,3/Frequency}]

Here is a plot of a triangle wave with a frequency of 440 Hz and 22 harmonics.

TriangleWavePlot[1, 440, 22]

Playing Triangle Waves

Here is a Mathematica function to play a triange wave.  It adds Duration to the three arguments above:

		Duration:  duration of the total sound
	
The SampleRate is set to 44100, so that the highest harmonics can be heard.

TriangleWavePlay[Amplitude_,Frequency_,Harmonics_,Duration_] := 
	Play[Evaluate[Sum[ 
		Amplitude / (2 i + 1)^2 * 
		Cos[(2 i + 1) * 2 Pi Frequency t],
			{i, 0, Harmonics}]],
			{t,0,Duration},
			SampleRate->44100]

Here is the sound of a triangle wave with a fundamental of 440 Hz and 22 harmonics:

TriangleWavePlay[1, 440, 22, 0.5]