Square, Sawtooth and Triangle Waves
Three waveforms appear in every synthesizer, function generator and textbook: the square, the sawtooth and the triangle. Their Fourier series are short formulas that are worth knowing by heart, and the differences between them (which harmonics, how fast they fall) explain why they sound so different.
The three series
With period 1, amplitude 1 and zero mean:
(The sawtooth here rises from to over ; the triangle peaks at .) Square and triangle have only odd harmonics; the sawtooth has all of them. The triangle's makes it sound much "softer".
import numpy as np
N = 8192
t = np.arange(N) / N
square = np.where(t < 0.5, 1.0, -1.0)
saw = 2 * ((t + 0.5) % 1) - 1 # rises from -1 to 1, jump at t = 1/2
tri = 2 / np.pi * np.arcsin(np.sin(2 * np.pi * t))
for name, f in (("square", square), ("sawtooth", saw), ("triangle", tri)):
A = 2 * np.abs(np.fft.fft(f)[1:8]) / N # amplitudes of harmonics 1..7
print(f"{name:9s}", np.round(A, 4).tolist())
print("predicted ", "square 4/(pi k):", np.round(4 / (np.pi * np.array([1, 3, 5, 7])), 4).tolist())
print("predicted ", "triangle 8/(pi k)^2:", np.round(8 / (np.pi * np.array([1, 3, 5, 7])) ** 2, 4).tolist())square [1.2732, 0.0, 0.4244, 0.0, 0.2546, 0.0, 0.1819]
sawtooth [0.6366, 0.3183, 0.2122, 0.1592, 0.1273, 0.1061, 0.0909]
triangle [0.8106, 0.0, 0.0901, 0.0, 0.0324, 0.0, 0.0165]
predicted square 4/(pi k): [1.2732, 0.4244, 0.2546, 0.1819]
predicted triangle 8/(pi k)^2: [0.8106, 0.0901, 0.0324, 0.0165]
Hearing the difference: harmonic energy
What fraction of the power lies in the fundamental? By Parseval the power is the sum of , so:
import numpy as np
K = np.arange(1, 200001)
odd = K[K % 2 == 1]
total = {"square": 1.0, "sawtooth": 1 / 3, "triangle": 1 / 3} # mean of f^2 over a period
fund = {"square": (4 / np.pi) ** 2 / 2, "sawtooth": (2 / np.pi) ** 2 / 2, "triangle": (8 / np.pi ** 2) ** 2 / 2}
for name in total:
print(f"{name:9s} fundamental carries {100 * fund[name] / total[name]:.2f} % of the power")
print("check, square: sum of (4/(pi k))^2 / 2 over odd k =", round(float(np.sum((4 / (np.pi * odd)) ** 2) / 2), 6))square fundamental carries 81.06 % of the power
sawtooth fundamental carries 60.79 % of the power
triangle fundamental carries 98.55 % of the power
check, square: sum of (4/(pi k))^2 / 2 over odd k = 0.999998
The triangle is almost a pure sine (98.6 % in the fundamental); the sawtooth has the richest overtones, which is why it is the starting point for brass and string sounds in subtractive synthesis.
In software
- Naively sampled square and sawtooth waves alias badly because their harmonics never stop; synthesizers use band-limited versions (summing only harmonics below half the sampling rate, or BLEP corrections).
scipy.signal.squareandscipy.signal.sawtoothgenerate the naive waveforms.
Exercises
Exercise 1: Amplitude of a harmonic
What is the amplitude of the 5th harmonic of a square wave with amplitude 2?
Answer
.
Solution
Scaling the wave by 2 scales every coefficient by 2: .
Exercise 2: Triangle from square
The triangle wave is (a multiple of) the integral of the square wave. Explain the from this.
Answer
Integrating divides it by , so becomes .
Solution
. Integration divides each harmonic by its frequency, turning the square's into (and shifting phases by a quarter period). Differentiation does the opposite and makes high harmonics stronger.
Exercise 3: Basel from the triangle (code)
Use Parseval for the triangle wave to show , and check it numerically.
Answer
Both sides are about 1.014678.
Solution
The triangle's mean square is , and the sum of is . Setting them equal gives .
import numpy as np
k = np.arange(1, 200001, 2, dtype=float)
print(round(float(np.sum(1 / k ** 4)), 6), round(np.pi ** 4 / 96, 6))1.014678 1.014678
Test yourself
Which waveform contains even harmonics?
- square
- triangle
- sawtooth
- none of them
The triangle wave's harmonics fall off like…
Why does a naive digital sawtooth alias?
- it has a DC offset
- its harmonics extend to infinitely high frequencies
- it is not periodic
- it is odd