Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 68 ยท Series, Transforms and PDEs

Fourier Series

Build periodic signals from sines and cosines: Fourier coefficients, the square wave, convergence, the Gibbs phenomenon and partial-sum plots in Python.

A guitar string, an AC supply, a vibrating bridge and a digital audio file all produce signals that repeat. Joseph Fourier's remarkable claim was that almost any repeating signal, however jagged, can be written as a sum of plain sines and cosines whose frequencies are whole multiples of a base frequency. That sum is the Fourier series.

Why is this useful? Sines and cosines are the natural "pure tones" of linear systems: feed one into a spring, a circuit or a heat-conducting rod and you get the same frequency back, only scaled and shifted. Break a complicated input into pure tones, handle each one easily, and add the answers. This idea powers the Fourier transform, filter design and the PDE solutions later in this chapter.

The series

Let f(x) have period 2L. To keep formulas short, we take L = pi, so f(x + 2*pi) = f(x). Then

f(x) = a0/2 + sum over n >= 1 of ( a_n cos(n x) + b_n sin(n x) )

and the coefficients come from integrals over one period:

  • a_n = (1/pi) * integral of f(x) cos(n x) dx from -pi to pi
  • b_n = (1/pi) * integral of f(x) sin(n x) dx from -pi to pi

Here a0/2 is simply the average value of f. The term with n = 1 is the fundamental; n = 2, 3, ... are the harmonics.

Why do these integrals pick out the coefficients? Because the functions cos(n x) and sin(m x) are orthogonal over a period. For example, the integral of sin(n x) * sin(m x) from -pi to pi is 0 when n differs from m and equals pi when they match. Multiply the whole series by sin(m x) and integrate, and every term vanishes except the one containing b_m. The same trick is how projecting a vector onto a perpendicular axis isolates one component, an idea you will see again in Eigenvalues and Eigenvectors in Engineering.

flowchart LR
    A["periodic f(x)"] --> B["integrate against<br/>cos(nx), sin(nx)"]
    B --> C["coefficients a_n, b_n"]
    C --> D["add the harmonics"]
    D --> E["approximation of f(x)"]

Analysis (finding coefficients) and synthesis (adding harmonics) are the two directions of a Fourier series.

Shortcuts from symmetry. If f is even (f(-x) = f(x)), every b_n is 0 and only cosines appear. If f is odd (f(-x) = -f(x)), every a_n is 0 (including a0) and only sines appear. Check symmetry first; it halves the work.

Example: the square wave

Define f(x) = -1 for -pi < x < 0 and f(x) = +1 for 0 < x < pi, repeated forever. It is odd, so only sines appear, and

b_n = (2/pi) * integral from 0 to pi of sin(n x) dx = 2/(n*pi) * (1 - cos(n*pi))

Since cos(n*pi) is -1 for odd n and +1 for even n:

  • even n: b_n = 0
  • odd n: b_n = 4/(n*pi)

So

f(x) = (4/pi) * ( sin(x) + sin(3x)/3 + sin(5x)/5 + ... )

A flat-topped wave built from smooth sines, with only odd harmonics whose strengths fall like 1/n.

Python Coefficients with sympy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Odd n give 4/(k*pi), even n give 0, and for the sawtooth x the coefficients alternate in sign and fall like 2/n.

Partial sums in action

Truncating at N harmonics gives the partial sum S_N(x). Adding terms makes the picture sharper at the flat parts and steeper at the jump.

Python Square wave partial sums

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice how the sum with one harmonic is a single sine bump, three harmonics already look squarish, and forty are almost perfect away from the jumps.

Convergence and the Gibbs phenomenon

Where does the series equal the function? Dirichlet's theorem says: if f is piecewise smooth, the series converges to f(x) at every point where f is continuous, and to the midpoint of the jump at a discontinuity. For our square wave at x = 0, every term is sin(0) = 0, which is exactly the midpoint of -1 and +1.

Near a jump, the partial sums behave oddly. They overshoot the true value, and adding more terms does not make the overshoot go away; it only squeezes it closer to the jump. The peak stays about 9% of the jump height above the plateau. This is the Gibbs phenomenon. For a jump of size 2 the partial sums peak near 1.179 instead of 1.

Python Measuring the Gibbs overshoot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The peak moves toward x = 0 (roughly at pi/(2N)) but its height stays near 1.18. Gibbs ringing is real physics for engineers: it appears in sharp low-pass filters, in image compression artifacts and in truncated numerical solutions. Smoother signals converge better: the faster a function's smoothness, the faster its b_n fall (1/n for jumps, 1/n**2 for a triangle wave, faster still for smooth curves).

Other periods, energy and the complex form

For a period 2L, replace n x by n*pi*x/L and the integrals run from -L to L. Nothing else changes.

Parseval's identity says the average power of the signal equals the sum of the powers of its harmonics:

(1/pi) * integral of f**2 = a0**2/2 + sum of (a_n**2 + b_n**2)

For the square wave f**2 = 1, so the left side is 2, and you can check that sum of 16/(pi**2 n**2) over odd n gives 2. This identity is how engineers say "how much of the signal's energy sits in the first few harmonics".

Using Euler's formula (see Complex Numbers and Phasors), the series can be written compactly as f(x) = sum of c_n e**(i n x) over all integers n. The complex form is what the FFT computes.

Python Numeric coefficients and Parseval

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The triangle wave |t| is continuous, so its coefficients fall like 1/n**2, and seven harmonics already capture nearly all of its power. Compare with the square wave's slow 1/n decay.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. The function f(x) is odd and 2*pi-periodic. Which coefficients are zero?

    An odd function times cos(nx) is odd, so its integral over a symmetric interval is 0. Only the sine terms survive.
  2. 2. At a jump discontinuity, a Fourier series converges to

    By Dirichlet's theorem the series converges to the average of the two one-sided limits at a jump.
  3. 3. What happens to the Gibbs overshoot as you add more harmonics?

    The ringing gets narrower and sits closer to the discontinuity, but its height does not shrink.
  4. 4. The square wave has b_n = 4/(n*pi) for odd n. Which harmonics are missing?

    For even n, 1 - cos(n*pi) = 0, so b_n = 0. The square wave contains only odd harmonics.
  5. 5. Why can we extract one coefficient by integrating f against cos(mx) or sin(mx)?

    Orthogonality makes every cross term integrate to zero, leaving only the coefficient you are after.

Summary

  • A periodic function can be written as a0/2 + sum of (a_n cos(nx) + b_n sin(nx)), with coefficients from integrals over one period.
  • Even functions use only cosines; odd functions use only sines.
  • The square wave has only odd harmonics with strengths 4/(n*pi).
  • At a jump the series converges to the midpoint, and the Gibbs overshoot (about 9% of the jump) never disappears.
  • Smoother signals have faster-decaying coefficients; Parseval's identity relates signal power to harmonic power.

Next: The Fourier Transform and the FFT.