Classroom · Sample lesson · Fourier and frequency

Building a square wave from sines

Add sine waves at 1, 3, 5, 7… times the basic frequency with amplitudes 1, 1/3, 1/5, 1/7… and a square wave appears, along with a small overshoot at every jump that never goes away.

7 steps · about 12 minutes · needs: sine waves

The animation, step by step

  1. A square wave of period 2π, jumping between +1 and −1.
  2. The first term, (4/π) sin x, already follows the wave's rise and fall; its peak is 4/π ≈ 1.27.
  3. The third harmonic (in terracotta) flattens the top and steepens the sides.
  4. Three terms: the shoulders square off; small ripples remain.
  5. Four terms, n = 1, 3, 5, 7. The earlier sums stay as faint lines.
  6. With harmonics up to n = 39 the tops are nearly flat and the edges steep.
  7. Next to each jump the sum still peaks near 1.18: about 9% of the jump, however many terms are added.

In one paragraph

A square wave, flipping between +1 and −1, looks nothing like a smooth sine. Yet it can be built from sines alone: start with one sine of the same period, then add sines at 3, 5, 7… times that frequency with amplitudes 1/3, 1/5, 1/7… The sum gets squarer with every term. This is a Fourier series, and the same idea, splitting a signal into frequencies, underlies spectra, filters and the way MRI forms images. One surprise remains: near each jump the sum overshoots by about 9% of the jump, however many terms are added. That is the Gibbs phenomenon.

Key ideas

  • A periodic signal can be written as a sum of sines and cosines at whole multiples of its basic frequency (its harmonics).
  • The square wave is odd and has half-wave symmetry, so only odd sine harmonics appear: n = 1, 3, 5, …
  • The amplitude of harmonic n is 4/(nπ): it falls off as 1/n, slowly, because the wave has jumps.
  • Each added term sharpens the edges and flattens the tops; the error shrinks everywhere except right at the jumps.
  • Near a jump the partial sums overshoot by about 9% of the jump size (peak ≈ 1.179 for a jump from −1 to +1). More terms move the overshoot closer to the jump but do not shrink it.
  • At the jump itself the series gives the midpoint, 0.

Step by step

  1. The target. A square wave of period 2π: +1 for 0 < x < π, −1 for π < x < 2π, repeating.
  2. One term. The best single sine is (4/π) sin x. Its peak, 4/π ≈ 1.27, is higher than 1: it has to make up for the area it misses near the edges.
  3. Add the third harmonic. (4/π)·sin 3x / 3 pulls the top down in the middle and pushes the sides up, so the shoulders square off.
  4. Add the fifth and seventh. Each smaller, faster sine corrects the ripples left by the ones before it.
  5. Twenty terms. With harmonics up to n = 39 the sum is nearly flat on the tops and steep at the edges, with fast small ripples.
  6. The overshoot that stays. Next to every jump the sum still overshoots, to about 1.18. With more terms the ripple gets narrower and moves closer to the jump, but its height stays near 9% of the jump.

The formulas

f(x)= 4π ∑k=1∞ sin⁡((2k−1)x)2k−1 = 4π (sin⁡x+sin⁡3x3+sin⁡5x5+…) (1)
bn= 1π ∫−ππ f(x)sin⁡(nx)dx = { 4nπn odd 0n even (2)
limK→∞ maxx SK(x) =2πSi(π) ≈1.1790 (3)

Equation 1 is the square wave with period 2π and values ±1; SK is the sum of its first K terms. Equation 2 gives each harmonic's weight. In equation 3, Si is the sine integral; the overshoot above 1 is 0.179, which is 8.95% of the jump of 2 (from −1 to +1): the "about 9%" of the Gibbs phenomenon.

Try it in Python

Sum the first K terms and measure the peak next to the jump: it settles near 1.179 instead of falling to 1.

square_wave.pyPython · numpy, matplotlib
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-np.pi, np.pi, 20001)
square = np.sign(np.sin(x))

def partial_sum(x, K):
    """The first K terms: (4/π) Σ sin((2k−1)x) / (2k−1), k = 1..K."""
    n = 2 * np.arange(1, K + 1)[:, None] - 1
    return 4 / np.pi * np.sum(np.sin(n * x) / n, axis=0)

for K in (3, 10, 100):
    y = partial_sum(x, K)
    overshoot = (y.max() - 1) / 2          # as a fraction of the jump from −1 to +1
    print(f"K = {K:3d}: peak {y.max():.4f}, overshoot {overshoot:.1%} of the jump")
    plt.plot(x, y, lw=1, label=f"{K} terms")

plt.plot(x, square, "k--", lw=1, label="square wave")
plt.legend(); plt.xlabel("x")
plt.show()
Output
K =   3: peak 1.1884, overshoot 9.4% of the jump
K =  10: peak 1.1798, overshoot 9.0% of the jump
K = 100: peak 1.1790, overshoot 8.9% of the jump

Check yourself

Why are there no even harmonics (sin 2x, sin 4x, …) in the square wave?

The second half of each period is the first half turned upside down (half-wave symmetry). Even harmonics repeat identically in both halves, so their contributions cancel and every bn with even n is 0.

With 1000 terms, how high does the sum go next to a jump?

Still about 1.179: the ripple is squeezed much closer to the jump, but its height does not shrink.

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.