Sub topic 13
Harmonics and Building Complex Waveforms
This chapter assumes you know the superposition principle, the standing-wave modes of a string (f_n = n·v/(2L)), and basic sine and cosine. It also assumes the idea of a wave's spectrum as a list of frequencies with amplitudes. By the end you should be able to say which harmonics a waveform contains. You should also be able to build a waveform by adding sine waves and say where that method stops working.
From modes to harmonics
A string fixed at both ends can vibrate in modes with frequencies fn = n·f1, where f1 is the fundamental. Each frequency in this list is a harmonic. The fundamental is harmonic 1, the mode at 2·f1 is harmonic 2, and so on.
Some musicians count overtones, which are the frequencies above the fundamental. The first overtone is harmonic 2, the second overtone is harmonic 3, and so on. This chapter uses harmonic numbers, because they match the n in the formula.
For the string in the earlier code example (T = 100 N, μ = 0.01 kg/m, L = 0.65 m), the wave speed is 100 m/s and f_1 = 100 / (2 × 0.65) ≈ 76.9 Hz. The third harmonic is therefore about 230.8 Hz.
A real plucked string does not vibrate in just one mode. The pluck excites many modes at once, and superposition says the motion is their sum:
y(x, t) = sum over n of b_n * sin(n*pi*x/L) * cos(2*pi*n*f_1*t + phase_n)The numbers b_n set how strongly each harmonic contributes. Together they are the string's spectrum.
The Fourier idea
Fourier's theorem says that any periodic waveform with period T can be written as a sum of sine waves. Their frequencies are the fundamental f = 1/T and its integer multiples. The amplitude of each term can be computed:
y(t) = sum over n of [ a_n cos(2*pi*n*f*t) + b_n sin(2*pi*n*f*t) ]
b_n = (2/T) * integral over one period of y(t) sin(2*pi*n*f*t) dtThe formula for a_n is the same with cosine in place of sine. This is Fourier synthesis when you add the terms to build a waveform. It is Fourier analysis when you work out the coefficients from a given waveform.
Two points matter for what follows:
- The sum has only harmonics of the fundamental. A waveform that repeats every T seconds cannot contain other frequencies.
- Fourier analysis works on any shape, but physical superposition requires a linear medium. For a string, that is the small-amplitude regime where the restoring force is proportional to displacement.
Common waveforms and their recipes
Each standard waveform has a fixed recipe of harmonic amplitudes, as the table shows.
| Waveform | Harmonics present | Relative amplitude of harmonic n |
|---|---|---|
| Sine | Only n = 1 | 1 for n = 1, otherwise 0 |
| Square | Odd n only | 1/n |
| Sawtooth | All n | 1/n |
| Triangle | Odd n only | 1/n², with alternating signs |
The square-wave recipe is, with unit amplitude:
y(t) = (4/pi) * [ sin(wt) + sin(3wt)/3 + sin(5wt)/5 + ... ]The coefficients fall off slowly (1/n) for the square and sawtooth waves, and quickly (1/n²) for the triangle. A sharp edge in the waveform needs many high harmonics, and a smooth waveform needs few. The triangle wave has corners but no jumps, so its spectrum decays faster.
The next program builds a square wave from its first few odd harmonics:
import numpy as np
t = np.linspace(0, 1, 2000, endpoint=False) # one period, time in periods
for n_terms in (1, 3, 10, 50):
y = np.zeros_like(t)
for k in range(n_terms):
n = 2 * k + 1 # odd harmonics only
y += (4 / np.pi) * np.sin(2 * np.pi * n * t) / n
print(f"{n_terms:3d} odd harmonics: peak = {y.max():.3f}, "
f"middle of plateau = {y[500]:.3f}")The plateau value approaches 1 as terms are added. The peak, however, does not settle at 1. It stays near 1.18 however many terms you use.
Gibbs overshoot
The leftover error near the jump is the Gibbs phenomenon. The partial sum overshoots a jump by about 9% of the jump's size, which is about 0.18 for a jump from -1 to +1. Adding more harmonics squeezes the overshoot into a narrower region next to the jump but does not shrink it. A discontinuity is the one place where a finite sum of smooth sine waves cannot match the target.
Why the pluck position matters
For a string plucked at a fraction p of its length from one end, the harmonic amplitudes are proportional to:
b_n ~ sin(n * pi * p) / n^2Plucking exactly at the middle (p = 1/2) makes sin(nπ/2) equal zero for every even n. The even harmonics vanish, because the pluck point is a node of those modes and you cannot excite a mode at its node. Plucking near the end (small p) keeps many harmonics strong, which gives a bright, thin sound. Plucking near the middle gives a rounder, mellower sound.
You can check this with an FFT (fast Fourier transform), which is the numerical tool for Fourier analysis. With fs = 8000 samples and a duration of 1 s, each FFT bin is 1 Hz wide, so a 100 Hz fundamental falls exactly on a bin:
import numpy as np
fs = 8000 # samples per second
f0 = 100 # Hz, fundamental
N = fs # 1 second of samples, so bins are 1 Hz apart
t = np.arange(N) / fs
for p in (0.5, 0.2):
y = np.zeros(N)
for n in range(1, 31): # harmonics up to 3000 Hz
y += np.sin(n * np.pi * p) / n**2 * np.sin(2 * np.pi * n * f0 * t)
amp = 2 * np.abs(np.fft.rfft(y)) / N # amplitude of each bin
print(f"pluck at p = {p}")
for n in range(1, 7):
print(f" harmonic {n}: {amp[n * f0]:.4f}")At p = 0.5 the printout shows harmonics 2, 4 and 6 at 0.0000, and harmonics 1, 3 and 5 at 1.0000, 0.1111 and 0.0400. At p = 0.2 the even harmonics are present, except that harmonic 5 vanishes, because sin(5π × 0.2) = sin(π) = 0.
Timbre
Two instruments playing the same note have the same fundamental but different harmonic amplitudes. We hear the shared f_1 as the pitch and the different amplitude pattern as timbre, the quality that separates a flute from a violin. The pattern comes from how each instrument is excited and from how its body and air column favor certain frequencies (resonance, from the previous chapter).
Pipes show the same logic. A pipe closed at one end supports only odd harmonics, so its sound has a hollow quality compared with an open pipe, which supports all harmonics.
Limits and failure modes
- Nonlinearity. If a string is plucked very hard, its tension changes during the motion and pitch drifts. The modes are no longer independent, and the Fourier picture of separate modes breaks down. Superposition holds only while the medium is linear.
- Stiffness. Real strings resist bending. The overtones then sit slightly above n·f_1, a property called inharmonicity, most noticeable in piano bass strings. The frequencies are no longer exact integer multiples, so the sound is no longer strictly periodic.
- Non-periodic signals. A short pulse or a decaying note has no single period. Its spectrum is continuous or spread out instead of consisting of sharp lines, and you analyze it with a Fourier transform over a time window.
- FFT mismatches. If the analysis window does not contain a whole number of periods, energy leaks into neighboring bins and the amplitudes read slightly low. The example above avoids this by choosing 1 s and 100 Hz.
- Aliasing. Harmonics above half the sampling rate fold back to lower frequencies. In the example, harmonic 30 is 3000 Hz, safely below 4000 Hz.
Exercise
A string is plucked exactly at its midpoint. Its fundamental is 76.9 Hz. Which harmonics are missing from the sound, and what is the frequency of the lowest harmonic that is present besides the fundamental?
Show answer
The midpoint is a node of every even-numbered mode, so harmonics 2, 4, 6, and so on are missing. The lowest other harmonic present is the third, at 3 × 76.9 Hz ≈ 230.7 Hz (230.8 Hz if you use the unrounded f_1 = 76.92 Hz).
Exercise
You add the first 5 odd harmonics to build a square wave. Using the 1/n recipe, what is the amplitude of the fifth term (n = 9) relative to the first term, and does adding more terms remove the overshoot at the jumps?
Show answer
The relative amplitude is 1/9 ≈ 0.111 of the fundamental's amplitude, since the fifth odd harmonic is n = 9. More terms do not remove the overshoot. It stays at about 9% of the jump height (the Gibbs phenomenon), but it is confined to a narrower region next to each jump.