MathsCalculus

Fourier Series
calculator.

Rebuild a repeating signal from sines and cosines, and see how many terms it takes.

How it's worked out

A Fourier series/09:_Partial_Differential_Equations/9.03:_Fourier_Series) writes a function that repeats every 2L as a constant plus waves. Each wave fits a whole number of times into one period. Here L is the half-period, so a period of 2π means L=πL = \pi. The wave with number n is called harmonic n.

f(x)\displaystyle f(x)=a02\displaystyle {}= \frac{a_0}{2}+∑n=1∞ancos⁡nπxL\displaystyle {}+ \sum_{n=1}^{\infty} a_n \cos\frac{n\pi x}{L}+bnsin⁡nπxL\displaystyle {}+ b_n \sin\frac{n\pi x}{L}

To find how much of a wave is in f, multiply f by that wave and integrate over one period. Every other wave averages out to 0, so only the one you want is left.

an\displaystyle a_n=1L∫−LLf(x)cos⁡nπxLdx\displaystyle {}= \frac{1}{L}\int_{-L}^{L} f(x)\cos\frac{n\pi x}{L}\,dx
bn\displaystyle b_n=1L∫−LLf(x)sin⁡nπxLdx\displaystyle {}= \frac{1}{L}\int_{-L}^{L} f(x)\sin\frac{n\pi x}{L}\,dx

The cosine formula with n=0n = 0 gives a₀, and that is why the series starts with half of it: a02\frac{a_0}{2} is the average of f over a period. The tool works each integral out with Simpson's rule on 4,096 panels. Each harmonic can also be written as one shifted wave, with amplitude an2+bn2\sqrt{a_n^2 + b_n^2} and phase given by the angle of the point (an,bn)(a_n, b_n).

The check. Parseval's theorem says the squares of all the coefficients add up to the energy of f, which is 1L\frac{1}{L} times the integral of f squared. So the share that N harmonics hold tells you how much of f the sum has captured.

1L∫−LLf2dx\displaystyle \frac{1}{L}\int_{-L}^{L} f^2\,dx=a022+∑n=1∞an2+bn2\displaystyle {}= \frac{a_0^2}{2} + \sum_{n=1}^{\infty} a_n^2 + b_n^2

Worked examples

Elena expands a square wave

Elena's signal is −1 for negative x and 1 for positive x, repeating every 2π. It is an odd function, so every aₙ is 0. Only odd harmonics of sine appear, with bn=4nπb_n = \frac{4}{n\pi}: that gives 1.2732 for n=1n = 1, 0.4244 for n=3n = 3 and 0.2546 for n=5n = 5.

With 9 harmonics, the sum holds 96% of the energy, yet it peaks at 1.182, above the wave's height of 1. That is the Gibbs overshoot. Adding more terms makes the bump narrower but no shorter, and with 99 harmonics it still peaks at 1.179.

Open this example in the tool

Kai looks at a rectifier's output

A single diode passes the top half of a sine wave and blocks the rest. Kai types that as max(0, sin x). The average is 0.3183 of the peak, which is the direct voltage a filter would pull out.

The first harmonic is a sine of size 0.5, half the input. The next one is a cosine at twice the frequency, with size 0.2122. That second term is the ripple the filter has to remove. The function is neither odd nor even, so both kinds of wave appear.

Open this example in the tool

x² gives a famous sum

On [−π, π], x² is even, so only cosines appear, with an=4(−1)nn2a_n = \frac{4(-1)^n}{n^2}. The first is −4, and the average is 3.2899.

Now set x=πx = \pi. The signs cancel, so every term turns positive, and the series rearranges into a result you may have seen before:

∑n=1∞1n2\displaystyle \sum_{n=1}^{\infty} \frac{1}{n^2}=π26\displaystyle {}= \frac{\pi^2}{6}

With 5 harmonics, the sum at π is 9.1443, against the true 9.8696. The gap closes slowly at this corner, because the repeated curve has a sharp point there.

Open this example in the tool

Reading your result

Zeros in the table are real. For an even function/09:_Partial_Differential_Equations/9.04:_Fourier_Sine_and_Cosine_Series) every bₙ is 0, and for an odd one every aₙ is 0. The tool also rounds anything under about a billionth of the function's size to 0, so rounding noise does not look like a harmonic.

Smooth functions need fewer terms. A jump makes coefficients shrink like 1n\frac{1}{n}, and a corner like 1n2\frac{1}{n^2}. So a triangle wave passes far more of its energy in the first few harmonics than a square wave does.

Share of the energy held by the first N harmonics
NSquare waveTriangle wave
181.07%99.638%
390.08%99.943%
1196.65%99.998%
2198.18%100%
9999.61%100%

The peak near a jump settles near 1.179 for a wave that jumps from −1 to 1, per the sine integral. That is about 9% of the jump of 2. At the jump itself, the series settles on the midpoint of the two sides, which is 0 here.

Other periods. Set L to half of your period. For a sawtooth with period 4, use L=2L = 2, and the formula can use L by name, as in xL\frac{x}{L}. Its first sine has size 0.6366, and a triangle wave's first harmonic with L=πL = \pi has size 0.4053.

Common mistakes

The tool goes up to 100 harmonics, which still leaves about 40 samples per cycle at the top of the range. For a list of measured samples and not a formula, use the discrete Fourier transform calculator.

  • Using the whole period for L. L is half the period. A signal that repeats every 4 needs L=2L = 2, or every coefficient comes out wrong.
  • Forgetting the half on a₀. The constant term is a02\frac{a_0}{2}, not a₀. Some books define a₀ as the average itself, so check which one yours uses before you compare.
  • Typing a function that is not on −L to L. The tool reads f on that interval. If your function is given on 0 to 2L, write the same period in terms of the shifted x, or use a form that repeats correctly.
  • Expecting more terms to cure the overshoot. Near a jump the peak height does not fall. It only gets narrower.

Questions people ask

An odd function times a cosine is odd, and an odd function integrates to 0 over a range centered on 0. So every aₙ vanishes. The same reasoning kills every sine term when the function is even.

Sources