Fourier Series Demo: Harmonics and Waveforms

10 min read
MathematicsAudioCanvas APIFourier

Open the Fourier series demo to compare a chain of rotating circles with the waveform traced by its tip. Choose Square, Sawtooth, or Triangle, then change Terms to see a different finite sum. The demo does not play synthesized audio. Button sounds may play depending on the site's sound setting.

Start with the waveform and term count

The initial settings are Square, five terms, Speed 1, and visible circles. Set Terms to 1 to start with a single rotation, then add terms to inspect their combined motion. Switch to Triangle or Sawtooth and compare the circle chain with the trace on the right.

Control What changes
Wave Select Square, Sawtooth, or Triangle
Terms Set the number of terms from 1 to 30
Speed Change phase speed from 0.1 to 3; this is not an audio frequency or a value in Hz
Circles Make the circles and radius lines Visible or Hidden; waveform calculation continues

Changing Wave or Terms resets the drawing history and phase. Speed and Circles do not perform that reset. After selecting a new waveform or term count, you can watch its new trace extend across the display.

How the circles produce a waveform

Each term has a multiplier n relative to the fundamental rotation and a coefficient. A larger n rotates faster. Attach each circle to the preceding tip, then record the height of the final tip on the right: this displays a sum of sine components.

Some coefficients are negative. The circle radius uses the coefficient's absolute value, while the vector toward its tip keeps the sign. Replacing a negative coefficient with a positive one changes the sum and produces a different waveform.

Screen y coordinates increase downward, so the sine component is subtracted from y. With a positive display scale, each term updates the tip in this form:

const signedAmplitude = amplitude * scale
const angle = n * t + phase
x += Math.cos(angle) * signedAmplitude
y -= Math.sin(angle) * signedAmplitude

This excerpt shows the drawing relationship. The circle radius uses an absolute value, but the radius-line endpoint, next circle center, and final tip must follow the same signed sum.

Coefficients for the three waveforms

The demo constructs the following coefficients. Here k runs from 0 through Terms minus 1, and every term has phase 0. “Harmonics” refers to the n-fold rotations and sine components, not components of audio playing on this page.

Waveform n Coefficient amplitude
Square 2k + 1 4 / (πn)
Sawtooth k + 1 (2 / π) × (−1)^(n + 1) / n
Triangle 2k + 1 (8 / π²) × (−1)^k / n²

Square and Triangle use only odd components. Square coefficients decrease as 1/n; Triangle coefficients alternate in sign, with magnitudes that decrease as 1/n². Sawtooth uses successive integer components with alternating signs.

For example, Triangle's first two terms have a positive coefficient at n = 1 and a negative one at n = 3. Circle size alone does not reveal the sign. Compare the direction from each center toward the next point with the final tip.

Wolfram MathWorld's triangle-wave series also has alternating odd components and a 1/n² factor. Period, origin, and amplitude conventions can change the appearance of a formula, so align those conditions before comparing coefficients from different references.

Limits when changing the term count

This demo adds a finite number of terms, so it cannot reproduce an ideal square wave's jumps exactly. It is useful for inspecting the shape at a chosen term count; there is no fixed count that makes the result complete or perfect.

The display also rescales the coefficients by the sum of their absolute values to fit the circles inside the canvas. Changing Terms changes that scale. Comparing the vertical extent of two settings directly does not measure absolute amplitude or approximation error.

Speed controls phase progression over elapsed time. The implementation advances phase by elapsed seconds times Speed times 2; it does not set an audio sample rate or playback frequency.

The waveform on the right stores at most 400 heights, appending one per draw call. It is not a recording and does not guarantee evenly spaced audio samples.

Choosing another waveform or audio-input demo

The related demos have different inputs and purposes. Choose between inspecting a Fourier sum and observing microphone input.

Demo Input and observations
Series: Fourier Series Rotating circles and a waveform whose coefficients are determined automatically by Wave and Terms; no synthesized-audio playback
Microphone: Sound Waves Waveform, frequency distribution, and estimated pitch from permitted microphone input; the input is not played through the speakers
Simulation: Oscilloscope Simulated Y-T and X-Y waveforms generated by formulas, not microphone measurements

In Sound Waves, use Allow Microphone to start, Freeze and Unfreeze to pause or resume the display, and Stop to end input. Microphone permission is required.

The Oscilloscope's Trigger control draws a reference line. It does not synchronize the trace as a measurement instrument's trigger would.

Start with a small number of Terms in the Fourier series demo, then inspect what changes as you add components.

Go deeper on this topic

Creative Coding

Mathematical simulations, nature visualization, and generative art in the browser

Article 10 of 13 in this series.

View topic page

Use the topic hub as a map, then continue to the next article in the series. New here? Start at the home hub →