Skip to content

Oscillations and Waves

Sound, Fourier, and DSP

Any sound is a stack of pure tones

Play the exact same note - say the A above middle C - on a violin, a flute, and a piano, and you can tell them apart instantly with your eyes closed. The pitch is identical. Something else is different. That something is the reason audio turns out to be, underneath, pure math, and why an equalizer, an MP3 file, and a spectrogram app all work.

A pure, featureless tone - the beep of a hearing test - is a single sine wave. But almost no real sound is a bare sine. A voice, a violin, a struck drum: their waveforms are jagged, complicated, nothing like that smooth curve. So how can a jagged sound and a smooth beep be the same kind of thing?

First, a guess

A violin playing one steady note makes a complicated, jagged waveform. How is it related to a simple sine wave?

Build a sound out of sines

Each slider below is the loudness of one pure sine tone: the lowest is the , and the rest are its at two times, three times, and so on. The top panel adds them all up into one waveform. Try the presets, then drag the sliders and watch a smooth wave grow teeth.

Slide the harmonics up and down and watch the waveform on top rebuild itself. It is always just a stack of pure sine tones. The bottom panel is the same sound as a recipe: how much of each harmonic. That recipe is what makes a violin and a flute on the same note sound different.

You just did additive synthesis by hand. The jagged shapes were never anything but a handful of sines stacked up. Now run the idea backward: given a recorded sound, figure out how much of each sine is in it. That backward step is the whole game.

Fourier's claim, and two ways to look at a sound

The mathematician Joseph Fourier proved something that sounds too strong to be true: any repeating signal, however complicated, can be written as a sum of sine waves at whole-number multiples of one base frequency, each with its own amplitude.

Any periodic signal is a sum of harmonics: the fundamental frequency f and its integer multiples.

That sum is exactly the summation you met in math, one term per harmonic. It gives you two equally true ways to hold the same sound in your head. The is the wiggly waveform on top of the widget. The is the bar chart underneath: the recipe of how much of each harmonic. The bar chart is the sound's , and moving between these two pictures is what nearly all audio software does all day.

Pitch versus timbre, finally separated

The fundamental's frequency sets the pitch - how high or low the note sounds. The mix of harmonics on top sets the , the character that tells a violin from a flute. Same fundamental, different harmonic recipe: same note, different instrument. That is why the presets in the widget all share a pitch but sound like different things.

Why audio is a branch of computer science

A microphone turns sound into a smooth voltage, but a computer cannot store a smooth anything. So it measures the voltage tens of thousands of times a second and keeps just those numbers. That is . CD audio samples times per second, which is why an audio file is just a long array of numbers.

To find the spectrum of that array - to pull out the harmonic recipe - a computer runs the , or FFT: the algorithm that does the backward step, waveform to spectrum, and does it fast. Its speed is the kind of thing the Big-O lens measures - about work instead of , which is the difference between real-time audio and a stutter.

And once a sound is in the frequency domain, you can throw parts of it away on purpose. Your ears cannot hear very high frequencies, and a loud tone hides a soft one nearby. An computes the spectrum, deletes the components you would never notice, and stores only what is left. That is how a song shrinks to a tenth of its size with no obvious loss - it is the spectrum, minus the parts you cannot hear.

The same idea, in code

Building a sound from harmonics is one summation loop, exactly what the widget does each frame:

import numpy as np

sample_rate = 44100
t = np.linspace(0, 1, sample_rate, endpoint=False)   # one second of samples
f0 = 220.0                                            # fundamental: the pitch

amps = [1.0, 0.5, 0.33, 0.25, 0.2, 0.17]             # a sawtooth-ish recipe
wave = sum(a * np.sin(2 * np.pi * (n + 1) * f0 * t)  # one term per harmonic
           for n, a in enumerate(amps))

spectrum = np.abs(np.fft.rfft(wave))                 # backward step: waveform -> harmonics
# spectrum spikes at 220, 440, 660, ... Hz: the recipe reappears.
Additive synthesis: sum a few harmonics to build a waveform, then read its spectrum back with an FFT.

Lock it in

  • Any repeating sound is a sum of pure sine tones: the fundamental plus harmonics at whole-number multiples of it.
  • Pitch is set by the fundamental frequency; timbre - what tells instruments apart - is set by the mix of harmonics.
  • A sound has two equivalent descriptions: the time-domain waveform and the frequency-domain spectrum. The FFT converts between them fast.
  • Computers store sound by sampling it into an array of numbers, tens of thousands per second.
  • MP3 compression works in the frequency domain, discarding components the ear cannot hear.

Check yourself

A violin and a flute play the exact same note. Why do they still sound different?

This is the pitch-versus-timbre distinction the widget makes physical. Try to state it, then check.

What sets the pitch you hear - how high or low a note sounds?

An MP3 file is much smaller than the raw recording mainly because it:

Match each term to what it means.

drop here

The lowest tone; its frequency is the pitch

drop here

Tones at integer multiples of the fundamental

drop here

The mix of harmonics - what tells instruments apart

drop here

A plot of amplitude versus frequency

Primary source

Steven W. Smith, The Scientist and Engineer's Guide to Digital Signal Processing (free online)

Chapters 8 and 9 walk through the Fourier transform and the frequency domain with almost no prerequisites and lots of pictures - the friendliest bridge from this lesson into real DSP. For the physics of sound and harmonics themselves, Feynman Lectures Vol I, Chapter 50 (Harmonics) is the classic treatment.[1][2]

Sources

  1. 1.Steven W. Smith - The Scientist and Engineer's Guide to DSP, Ch 8-9 (The Fourier Transform)
  2. 2.Feynman, Leighton, Sands - The Feynman Lectures on Physics, Vol I, Ch 50 (Harmonics)