MathematicsLesson 1 of 1About 20 minutes

Building a square wave from sine waves

A square wave jumps suddenly between two values. A sine wave changes smoothly. It is surprising, then, that smooth sine waves can be added together to build a square wave. This lesson shows how it works.

By the end of this lesson you can

  • write the first few terms of the Fourier series of a square wave;
  • explain why only odd frequencies appear;
  • describe the overshoot near each jump (the Gibbs phenomenon);
  • connect the idea to sound and electronics.

The idea

Think first

Every sine wave is smooth and rounded. Can adding smooth curves ever produce a sharp corner? Make a guess before you use the graph.

Compare your answer

Any finite number of sine waves gives a smooth curve, so the corner is never perfect. But as you add more terms, the sum gets as close as you like to the square wave everywhere except right at the jumps. The graph below lets you watch this happen.

The square wave in this lesson equals \(1\) for \(0 < x < \pi\) and \(-1\) for \(\pi < x < 2\pi\), and then repeats. Its Fourier series uses only sine waves with odd frequencies:

$$f(x) = \frac{4}{\pi}\left( \sin x + \frac{\sin 3x}{3} + \frac{\sin 5x}{5} + \frac{\sin 7x}{7} + \cdots \right)$$

Keeping only the first \(n\) terms gives the approximation \(f_n(x)\). Move the slider to add terms one at a time.

Partial sums of the series

\(f_n(x) = \dfrac{4}{\pi} \displaystyle\sum_{k=1}^{n} \dfrac{\sin\big((2k-1)x\big)}{2k-1}\)
Sum of \(n\) terms Square wave Individual terms (first six)

Try this

  1. Start at \(n = 1\). How high does the single sine wave reach compared with the square wave?
  2. Add terms one at a time up to 6, watching the thin blue curves. Where does each new term push the sum up, and where down?
  3. Jump to 25 terms and look closely at the jumps at \(x = \pi\) and \(x = 2\pi\).

What to notice

  1. More terms, closer fit. Each new term corrects part of the error left by the previous ones, so the curve gets flatter on the top and bottom and steeper at the jumps.
  2. Only odd frequencies. The square wave has the same shape on both halves of its period, only flipped. Sine waves with even frequencies do not share that property, so their coefficients are zero.
  3. The overshoot does not go away. Near each jump the sum overshoots to about \(1.18\), roughly 9% of the jump height. Adding terms makes the overshoot narrower but not lower. This is called the Gibbs phenomenon.

Where it appears in physics

The same idea explains why a violin and a flute sound different when playing the same note: each instrument adds a different mix of higher frequencies to the base frequency. In electronics, a square-wave signal from a digital circuit contains many odd harmonics, which is why fast switching can cause interference at higher frequencies.

Check yourself

Question 1

Write the first three nonzero terms of the series.

Show answer

\(\dfrac{4}{\pi}\sin x,\ \dfrac{4}{3\pi}\sin 3x,\ \dfrac{4}{5\pi}\sin 5x\)

Question 2

With only one term, the peak of the curve is higher than 1. What is its height, to three decimal places?

Show answer

\(\dfrac{4}{\pi} \approx 1.273\), reached at \(x = \dfrac{\pi}{2}\).

Question 3

You use 100 terms instead of 25. What happens to the overshoot next to each jump?

  1. It disappears.
  2. It stays about the same height but becomes narrower.
  3. It becomes taller.
  4. It moves to the middle of each flat section.
Show answer

The Gibbs overshoot stays near 9% of the jump however many terms you add; it just squeezes closer to the jump.

Question 4

Set \(x = \dfrac{\pi}{2}\) in the series. Since \(f\left(\dfrac{\pi}{2}\right) = 1\), what famous result for \(\pi\) follows?

Show answer

\(\sin\left(\dfrac{(2k-1)\pi}{2}\right)\) alternates between \(1\) and \(-1\), so \(1 = \dfrac{4}{\pi}\left(1 - \dfrac{1}{3} + \dfrac{1}{5} - \dfrac{1}{7} + \cdots\right)\), which gives \(\dfrac{\pi}{4} = 1 - \dfrac{1}{3} + \dfrac{1}{5} - \cdots\) (the Leibniz series).

Summary

  • A square wave is the sum of sine waves with odd frequencies, with amplitudes \(\dfrac{4}{\pi k}\) for \(k = 1, 3, 5, \dots\)
  • More terms give a closer fit, but the overshoot near each jump (about 9%) never disappears.
  • Adding harmonics in different amounts is what gives musical instruments their distinct sounds.

Finished?

Mark the lesson as complete to keep track of your progress. Your progress is saved in this browser only.