TExcelTAD
Home › Fourier Analysis & Signals › Square, Sawtooth and Triangle Waves

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:

square:4π∑k oddsin⁡(2πkt)ksawtooth:2π∑k≥1(−1)k+1sin⁡(2πkt)ktriangle:8π2∑k odd(−1)(k−1)/2sin⁡(2πkt)k2\begin{aligned} \text{square:}\quad & \frac{4}{\pi} \sum_{k \text{ odd}} \frac{\sin(2\pi k t)}{k} \\ \text{sawtooth:}\quad & \frac{2}{\pi} \sum_{k \ge 1} (-1)^{k+1} \frac{\sin(2\pi k t)}{k} \\ \text{triangle:}\quad & \frac{8}{\pi^2} \sum_{k \text{ odd}} (-1)^{(k-1)/2} \frac{\sin(2\pi k t)}{k^2} \end{aligned}

(The sawtooth here rises from −1-1 to 11 over (−1/2,1/2)(-1/2, 1/2); the triangle peaks at t=1/4t = 1/4.) Square and triangle have only odd harmonics; the sawtooth has all of them. The triangle's 1/k21/k^2 makes it sound much "softer".

python · try it yourself
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 Ak2/2A_k^2/2, so:

python · try it yourself
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.square and scipy.signal.sawtooth generate 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

8/(5π)≈0.5098/(5\pi) \approx 0.509.

Solution

Scaling the wave by 2 scales every coefficient by 2: 2⋅45π=85π2 \cdot \frac{4}{5\pi} = \frac{8}{5\pi}.

Exercise 2: Triangle from square

The triangle wave is (a multiple of) the integral of the square wave. Explain the 1/k21/k^2 from this.

Answer

Integrating sin⁡(2πkt)\sin(2\pi k t) divides it by 2πk2\pi k, so 1/k1/k becomes 1/k21/k^2.

Solution

∫sin⁡(2πkt) dt=−cos⁡(2πkt)/(2πk)\int \sin(2\pi k t)\,dt = -\cos(2\pi k t)/(2\pi k). Integration divides each harmonic by its frequency, turning the square's 1/k1/k into 1/k21/k^2 (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 ∑k odd1/k4=π4/96\sum_{k \text{ odd}} 1/k^4 = \pi^4/96, and check it numerically.

Answer

Both sides are about 1.014678.

Solution

The triangle's mean square is 1/31/3, and the sum of Ak2/2A_k^2/2 is 32π4∑k odd1/k4\frac{32}{\pi^4}\sum_{k \text{ odd}} 1/k^4. Setting them equal gives π4/96\pi^4/96.

python · try it yourself
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?

  1. square
  2. triangle
  3. sawtooth
  4. none of them

The triangle wave's harmonics fall off like…

  1. 1/k1/k
  2. 1/k21/k^2
  3. 1/k31/k^3
  4. e−ke^{-k}

Why does a naive digital sawtooth alias?

  1. it has a DC offset
  2. its harmonics extend to infinitely high frequencies
  3. it is not periodic
  4. it is odd