Classic Waveshapes and Spectra
Reginald Bain
rbain@mozart.sc.edu
Printer friendly
waveshapes.orc
waveshapes.sco
Introduction
From their use in panning routines to their use in creating grain envelopes,
basic waveshapes such as the sine, sawtooth, square and triangle still
play an important role in today's digital signal processing applications. These
waveshapes are referred to as "classic" because of the significant role they
played in analog synthesis techniques. This magazine's Spring 1999 Beginners
column featured an article by Hans Mikelson entitled Oscillators.
Mikelson's article demonstrates how to use Csound's oscil opcode in
combination with an f-table and GEN10 to produce complex tones from
harmonically-related sinusoidal components. Conceptually, this article picks up
where Mikelson's article left off. It will demonstrate how to
generate and analyze complex tones with both harmonic and inharmonic partial
relationships, and will describe a number of terms associated with the analysis
of complex tones including: waveform, Fourier analysis and
spectrum. To show a creative application of these principles, some
interesting waveshapes will be generated using relationships derived from the
Fibonacci and Lucas sequences.
Waveform described
A waveform graph describes the variation of an audio signal's amplitude over time. It
is most often encountered in the form of a two-dimensional graph of amplitude
a versus time t. For example, Fig. 1 shows one complete cycle or period of
a sinusoidal waveform.
Fig. 1. One cycle of a sine wave.
| a |  |
| | t |
The horizontal axis indicates the passage of time. The vertical axis indicates
the amplitude a at any time t. For the sake of mathematical
simplicity, let's say that the sine wave in Fig. 1 has a frequency of 1 Hz. In
that case, each hash mark on the horizontal axis would represent 1/10 of a
second. Now let us turn our attention to the vertical axis. In a 16 bit audio
system, 216 different amplitude values may be represented,
resulting in a range of amplitude values that varies from, say, 0 to 65,535.
Alternatively, some digital audio systems represent amplitude values as varying
from 0 to 1. Both representations are valid and it is easy for a computer to
convert between the two formats. What is more, as Fig. 1 demonstrates, it is
often necessary to be able to represent amplitude variation both above and below
the vertical axis. Negative values can be employed for this purpose. An audio
signal whose amplitude varies from 1 to -1 is said to be bipolar; an
audio signal whose amplitude varies from 0 to 1 is said to be unipolar.
(An ADSR amplitude envelope is an example of a unipolar signal.) Consequently,
each hash mark on the vertical axis in Fig. 1 can be interpreted as representing
1/10 of a maximum amplitude value of 1. All of the waveform graphs in this article
were created with PacificTech's GraphingCalculator (www.PacificT.com). In order to produce a
smoother looking curve, the amplitude of the signal in Fig. 1 was scaled
by 80%. That is, every amplitude value was multiplied by .2 and the
amplitude of the signal shown in Fig. 1 varies from .2 to -.2 rather than from 1
to -1. In Csound, amplitude values must be strategically scaled because
whenever two or more audio signals are added together the potential
exists for the sum of their amplitudes to exceed 1 at some point. If the
amplitude ever exceeds 1, clipping and thus distortion will occur.
Generating complex tones from harmonically related sinusoidal components
If one uses Csound to render the waveform shown in Fig. 1 a simple tone is produced.
Because a sine wave is derived from simple harmonic motion, a type of oscillatory
motion that produces energy at a single frequency, it is a tone devoid of
overtones. In additive synthesis, a complex tone is produced by adding
simple tones together. If one uses a large number of sinusoidal components and
varies the amplitude of each individual component independently over time, a
variety of interesting timbres may be created. However, in order to keep our
Csound instruments simple, we will employ only sinusoidal components with static
amplitudes.
Figures 2a through 2e display a set of five sine waves whose frequencies are
harmonically related and whose amplitudes are equivalent. A compact way of
expressing such a sequence of related frequencies is to use the following sequence
notation borrowed from John Pierce's book The Science of Musical Sound:
Waveforms combine according to the principle of superposition.
Essentially, the peaks and troughs of each waveform add and subtract respectively
at each corresponding moment in time. The resultant waveform shown in Fig. 2f has
some interesting properties. First, note that its waveform has a significantly
different shape than the component waveforms shown in Figures 2a-2e. Nonetheless,
Fig. 2f's waveform appears to repeat itself every 1 Hz. Second, notice that the
rate at which it repeats is equal to the fundamental frequency f0. Each repetition within the waveform is called a
cycle. The period ( T ) is the amount of time it takes to
complete each cycle. The frequency of the waveform is defined as the
reciprocal of T:
f = 1/T
Because it repeats at regular intervals, the complex tone displayed in Fig. 2f
is said to be periodic. Further, because its waveform is periodic we
perceive it to have a definite pitch. That pitch is associated with the number of
cycles per second, which is commonly designated as hertz (Hz.). As you
will soon hear for yourself in the demo files, a waveform's shape contributes
significantly to our perception of the timbre of a complex tone. Other
factors such as loudness and amplitude envelope also play a crucial role in
timbral perception, however let's keep the focus here on the study of the
relationship between waveform and timbre. Finally, another interesting point
worth noting is that if we omit the fundamental from the complex tone sequence
above, for example,
we still perceive the pitch of the resulting complex tone to be at the missing
fundamental f0.
Analyzing complex tones
In 1822, the French mathematician Jean Baptist Fourier
(1768-1830) came forth with a mathematical proof demonstrating that any waveform,
no matter how complex, could be reduced to an infinite set of sine (or cosine)
wave components. Fourier's equation is shown in Fig. 3. It shows that each sine wave
component of a complex waveform can be expressed in terms of amplitude ( A ), frequency ( f ) and phase
(
):
Fig. 3. Fourier's equation
 |
Fourier further demonstrated that if the waveform is periodic, the
sine wave components are all members of an harmonic series. As most musical
sounds with a definite pitch have periodic or very nearly
periodic waveforms, Fourier's proof turned out to be both a profound scientific and
musical discovery. Though a complete discussion of the Fourier series is obviously beyond the
scope of our current discussion, we can still use the basic form of this equation to
gain some insight into the analysis of complex tones. Further, Csound's
dispfft opcode can be used to perform a Fast Fourier Transform (FFT)
on any audio signal. The FFT algorithm that is available in Csound is
based on Fourier's theory and provides a convenient means of revealing the spectrum of any complex tone.
Fig. 4. The spectrum of a sine wave revealed
using Csound's dispfft opcode
| a |  |
| | f |
Spectrum described
A spectrum displays the set of frequency components that make up a complex tone. It
is most often encountered in the form of a two-dimensional graph of amplitude
a versus frequency f. As we will see below in Fig. 5, for periodic
waveforms it makes sense to label the horizontal axis with harmonic partial
numbers. Fig. 4 shows the spectrum of a 440 Hz. sine wave. This graph was
generated using Csound's dispfft opcode. The graph clearly shows that
there is a single energy spike at the fundamental frequency. As can be seen in the
following code example, the dispfft
opcode is quite easy to add to any Csound instrument:
asig oscili iamp, ifqc, itable
dispfft asig, idur, 512
It should be noted that the FFT algorithm is computationally
intensive, so be sure to comment it out of your orchestra files when you don't
require it for analysis.
Classic Harmonic Waveshapes and their Spectra
The chart below compares the spectral content of four "classic" waveforms: the
sine, sawtooth, square and triangle wave. The square wave belongs to a larger
class of waveshapes called pulse waves. A pulse wave is a waveshape with
essentially two states: on or off. The ratio of the on state to the period of the
waveform is called the duty cycle. For example, a square wave has a duty
cycle of 1:2. Consequently, every second harmonic of a square wave is missing.
Other pulse waves may be created by varying the duty cycle. For example, a duty
cycle of 1:3 omits every third harmonic. In Csound, there are two ways to
approach the generation of these waveshapes: 1. GEN10 can be used to create
band-limited wavesforms from a finite number of
partials, or 2. GEN07 can be used to specify the ideal waveshape.
(Please see the Mikelson Oscillators article for more information about
the difference between these two approaches.)
Fig. 5. Choose a spectrum from the menu.
N.B.: Spectrum diagrams are not drawn to scale and only the first
9 partials are shown.
Classic Waveshapes and their Spectra:
|
|
| Waveshape | Partial No. Ratios indicate relative amplitudes of partials
|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | etc. |
Sine wave
w, s, f |
1 | | | | | | | | | | | | |
Sawtooth wave
Band-limited: w, s, f
Ideal: w, s |
1 | 1/2 | 1/3 | 1/4 | 1/5 | 1/6 | 1/7 | 1/8 | 1/9 | 1/10 | 1/11 | 1/12 | ... |
Square wave
Band-limited: w, s, f
Ideal: w, s |
1 | | 1/3 | | 1/5 | | 1/7 | | 1/9 | | 1/11 | | ... |
Triangle wave
Band-limited: w, s, f
Ideal: w, s |
1 | | 1/9 | | 1/25 | | 1/49 | | 1/81 | | 1/121 | | ... |
| Link Legend: |
w - waveshape, Csound f-table display
s - spectrum, Csound dispfft display
f - function/graph produced with GraphingCalculator
|
|
Hyperlinks are provided on the left hand side of the chart that allow one to view the waveshape (w), spectrum (s), and function/graph (f) for the
band-limited and ideal versions of each waveshape. Careful study of these diagrams
will reveal some interesting facts. For example, a comparison of the spectra of
the band-limited and ideal versions of each waveshape will reveal that their
spectral content is not the same.
Generating Complex Tones with Nearly Harmonic and Inharmonic Spectra
Csound can also be used to generate complex tones consisting of sinusoidal
components that not harmonically related. This can be done using GEN09. Here is
the syntax from the The Csound Manual for creating a f-table using
GEN09:
f # time size 9 pna stra phsa pnb strb phsb . . .
where size is the number of points in the f-table; pna, pnb, etc.
is the partial no. (non-harmonic partials are permitted); stra, strb, etc.
indicates the strength of partials; and pna, pnb, etc. indicates the phase
of each partial.
Like Fig. 2 above, Fig. 6a and Fig. 6b contain a waveform graph of a complex tone
built from five sinusoidal components. However, where Fig. 2 had a
harmonic spectrum, Fig. 6a has a nearly harmonic
spectrum and Fig. 6b has an inharmonic spectrum. The exact partial
relationships are indicated below each graph.
Fig. 6. Waveform graphs obtained from nearly harmonic (Fig. 6a)
and inharmonic (Fig. 6b) partial relationships.
 Fig. 6a. f0, 2.01 f0, 2.95 f0, 4.02 f0, 4.95 f0 |  Fig. 6b. f0, 2.3 f0, 2.6 f0, 4.4 f0, 4.7 f0 |
Periodic Signals
"...periodic is a term frequently used by scientists and engineers to
describe a large class of signals whose component parts fall in the harmonic
series, but in fact signals in nature are not periodic and the human auditory
system knows this to be so."
|
John Chowning "Perceptual Fusion and Auditory Perspective"
in Music, Cognition, and Computerized Sound
|
Carefully compare the two waveform graphs in Fig. 6. Notice how each "cycle" of
the waveform in Fig. 6a is nearly the same shape, yet there is a noticeable variation in
both waveshape and amplitude due to the fact that the components are slightly out of sync with
an harmonic series. As you can hear in the demo files, the complex tone shown in
Fig. 6a produces a sensation of pitch. However, the tone in Fig. 6b does
not appear to contain any repetitions and thus its components do not fuse together
in our auditory system to produce a single pitch sensation.
Getting Creative
Even when one limits oneself to the use of sinusoidal components with static
amplitudes, a variety of interesting timbres may still be created. For example, let's design a few
complex tones based on the Fibonacci and Lucas sequences. Fig. 7 shows the Fibonacci sequence.
Fig. 7. The Fibonacci sequence.
| 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, etc. |
First, let's create two complex tones with harmonic partial relationships and
relative amplitudes derived from the Fibonacci sequence and store them in f-tables f50 and f51.
;- Partial No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 6 17 18 19 20 21
f50 0 16384 10 0 .8 .5 0 .3 0 0 .2 0 0 0 0 .1
f51 0 16384 10 1.3 .8 .5 0 .3 0 0 .2 0 0 0 0 .1 0 0 0 0 0 0 0 .1
Next, let's create two complex tones with inharmonic partial and amplitude
relationships and store them in f-tables f60 and f61. The first
tone will be derived from the Fibonacci sequence, the second tone will be
derived from the related Lucas sequence. The Lucas sequence is shown in Fig. 8.
Fig. 8. The Lucas sequence.
| 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, etc. |
; Fibonacci Partials 1.3 2.1 3.4 5.5 8.9 14.4 23.3
f60 0 16384 9 130 .89 0 210 .55 0 340 .34 0 550 .21 0 890 .13 0 1440 .05 0 233 .05 0
; Lucas Partials 1.1 1.8 2.9 4.7 7.6 12.3 19.9
f61 0 16384 9 110 1 0 180 .89 0 290 .55 0 470 .34 0 760 .21 0 1230 .13 0 1990 .08 0
Listen to the demo files to compare the beautiful simplicity of the complex tone with harmonic partial
relationships that is stored in f50 with the beautiful complexity of the complex tone with
inharmonic partial relationships that is stored in f60.
Fig. 9. Waveform graphs resulting from harmonic (Fig. 9a) and inharmonic (Fig. 9b) partial
and amplitude relationships derived from the Fibonacci sequence.
 Fig. 9a. f50 |  Fig. 9b. f60 |
Try it yourself
The generation and analysis of complex tones from classic waveshapes is a great
place to begin your exploration of Csound's capabilities. The study of basic
principles associated with complex tone generation will help you gain a better
understanding of the harmonic series, additive synthesis and fundamental
principles of musical acoustics. Once you have had a chance to render the demo
files, try creating some complex tones of your own. And be sure to analyze your
results using Csound's dispfft opcode.
References
Adams, Robert Train. Electronic Music Composition for Beginners. Dubuque, IA: Brown & Benchmark, 1986.
Anderson, Craig. "Spot Problem Areas with Spectral Analysis." Keyboard (November 2001).
Chowning, John. "Perceptual Fusion and Auditory Perspective." Perry Cook, ed., Music, Cognition, and Computerized Sound:
    An Introduction to Psychoacoustics. Cambridge, MA: MIT Press, 1999.
Fischman, Rajmil. "A Survey of Classic Synthesis Techniques." Richard Boulanger, ed., The Csound Book Cambridge, MA: MIT Press, 2000.
Halliday, David and Robert Resnick. Fundamentals of Physics, New York: Wiley, 1988.
Knott, Ron. Fibonacci Numbers and the Golden Section. Available online: www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fib.html.
Pierce, John R. The Science of Musical Sound, New York: W.H. Freeman, 1992.
Mikelson, Hans. "Oscillators," The Csound Magazine. Available online:
http://www.csounds.com/ezine/spring1999/beginner/index.html.
Schindler, Allan. Eastman Csound Tutorial. Available online: www.esm.rochester.edu/onlinedocs/allan.cs/.