November 26, 2026

Visual Math: Build a Square Wave From Sine Waves

Red square wave with small ripples on each flat top, built from 12 sine waves, under the title Fourier Series

A square wave has sharp corners and flat tops. A sine wave has neither. Yet a stack of sine waves, each with its own height and speed, adds up to a square wave, and the more of them join the stack, the squarer the result gets. That recipe is a Fourier series, and the free Square Wave From Sine Waves app on x9m8 lets anyone build it one sine wave at a time, watch the rotating circles that draw it, and listen to the sum.

The square wave build as a short video, from the X9M8 channel on YouTube

Sine waves in, square wave out

The idea comes from Joseph Fourier. According to Britannica, his Théorie analytique de la chaleur of 1822 showed how heat conduction in solid bodies can be analyzed with infinite series of sines and cosines, which now carry his name. Britannica adds that the work reached far beyond heat conduction. In practice, a repeating signal is described as a sum of sine waves whose frequencies are whole-number multiples of one base frequency, which is the form used throughout this page.

For a square wave that rises to 1 and drops to −1, the sum has a short form. Only the odd multiples appear (1, 3, 5, 7 and so on), and each sine wave is scaled by 4/π divided by its multiple. The first four heights work out to 1.2732, 0.4244, 0.2546 and 0.1819. The base sine wave carries most of the shape, and each later one contributes less. The MIT course notes on Gibbs’ phenomenon use the same odd-multiples form for the standard square wave.

A single red sine wave drawn over a grey square wave, the first step of a Fourier series
One sine wave. Its peak reaches 1.27, already above the height of the square wave it is trying to imitate.

Three sine waves are enough to see the pattern

Adding the second sine wave, three times as fast and a third as tall, pulls the shoulders of the curve outward. The third, five times as fast, flattens the top a little more. At three sine waves the highest point of the sum is 1.1884; at two it is 1.2004; at one it is 1.2732. The tops are still wavy, but the shape is recognizably square.

Red curve built from three sine waves starting to flatten into a square wave
Three sine waves added, reaching up to the fifth multiple of the base frequency.

How the rotating circles draw the wave

Every sine wave in the sum can be pictured as the height of a point moving around a circle. The radius of the circle is the wave’s height, and the speed of the point is its frequency. Chain the circles together, each one centered on the tip of the previous arrow, and the height of the final tip over time is the whole sum. The left half of the app draws exactly this chain; the right half plots the tip’s height as it scrolls past, with the ideal square wave drawn faintly behind it for comparison.

Large circles spin slowly and set the overall outline. Tiny, fast circles near the end of the chain add the fine detail at the corners. Dragging the slider upward adds circles to the end of the chain, and the red curve visibly tightens.

Run it yourself

Open the app in its own tab, or use its Full screen button.

The Auto-add button repeats the build from one sine wave up to forty, adding a new one roughly every second. The Sound button plays the current sum as a tone, with a pitch slider from 80 to 440 Hz, so each added sine wave can be heard as well as seen. Square, sawtooth and triangle waves are all built into the same panel, and the page address accepts ?wave=tri&n=4 style settings for linking to a particular state.

The overshoot that never goes away

Watching the sum sharpen, one detail refuses to fade. Right beside each jump, the curve shoots past the flat top and rings back. Adding more sine waves moves that spike closer to the edge, but its height stays put, as MIT’s 2024 notes on the same topic explain. The 2011 notes state it plainly: the overshoot is always about 9% of the size of the jump, and for the square wave that means roughly 0.18 above a flat top of 1, a peak near 1.18.

The app’s readout shows the same limit emerging as sine waves are added. The highest point of the square-wave sum measures 1.1823 at five sine waves, 1.1796 at twelve and 1.1790 at forty. A Brown University page on the Gibbs phenomenon gives the limiting peak as 1.1789797 for a jump of size 2. The numbers in the readout are computed live from the series itself, and by forty sine waves they sit within about 0.0001 of that limit.

Red square wave built from 40 sine waves with sharp edges and a narrow spike of overshoot at each jump
Forty sine waves. The edges are steep and the tops nearly flat, yet the spike at each jump still peaks near 1.179.

Square, sawtooth and triangle recipes

The three waves in the app differ in which sine waves they use and how fast the heights shrink.

  • Square wave: odd multiples only, heights falling in proportion to 1/k. The slow shrinkage is why many sine waves are needed for sharp corners.
  • Sawtooth wave: every multiple, 1, 2, 3 and on, with alternating signs and heights again falling as 1/k. At twelve sine waves its highest point is 1.1011, so it shows the same kind of overshoot at its jump.
  • Triangle wave: odd multiples only, but heights falling as 1/k², with a leading factor of 8/π² (0.8106). With only one sine wave the peak is 0.8106; with four it is 0.9496. In the cases computed here (one, two and four sine waves) the peak stays below 1, which fits a triangle having no jump to overshoot.

The contrast shows what controls sharpness. Heights that fade slowly (1/k) mean high-frequency sine waves still matter, so corners are expensive to build. Heights that fade quickly (1/k²) mean a handful of sine waves is already close, which is why the triangle settles into place so much faster than the square.

How the animation was made

The still images on this page were drawn with Manim, a free, open-source Python package. An arXiv paper on Manim in STEM education credits its development to Grant Sanderson, creator of the 3Blue1Brown channel, and notes that it includes built-in functions for animating objects and supports LaTeX for equations. Animations are written as code: each scene lists the objects to draw and the transformations between them, and the renderer produces the video frames.

The interactive app above is a separate build in plain JavaScript, so the sliders respond instantly in the browser without any rendering step.

Sources

491 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

One Response to “Visual Math: Build a Square Wave From Sine Waves”

  1. When Clicks Turn Into a Note: Where Rhythm Becomes Pitch

    […] multiples of the repetition rate. That is the idea behind the first post in this series, Build a Square Wave From Sine Waves. A click train is the most extreme repeating pattern there is, and its recipe is correspondingly […]

Leave a Reply

Your email address will not be published. Required fields are marked *