Lesson 1 of 15

Fourier Series

Fourier Series

A Fourier series decomposes a periodic function into a sum of sines and cosines. For a function f(x) with period 2L, the series is:

fN(x)=a02+∑n=1N[ancos⁡(nπxL)+bnsin⁡(nπxL)]f_N(x) = \frac{a_0}{2} + \sum_{n=1}^{N} \left[ a_n \cos\left(\frac{n\pi x}{L}\right) + b_n \sin\left(\frac{n\pi x}{L}\right) \right]

The Fourier coefficients are computed via integration:

  • DC component: a0=1L∫−LLf(x) dxa_0 = \frac{1}{L} \int_{-L}^{L} f(x)\, dx
  • Cosine coefficients: an=1L∫−LLf(x)cos⁡(nπxL)dxa_n = \frac{1}{L} \int_{-L}^{L} f(x) \cos\left(\frac{n\pi x}{L}\right) dx
  • Sine coefficients: bn=1L∫−LLf(x)sin⁡(nπxL)dxb_n = \frac{1}{L} \int_{-L}^{L} f(x) \sin\left(\frac{n\pi x}{L}\right) dx

Numerical Integration

We represent f(x) by N evenly spaced sample points on [-L, L) using a midpoint grid:

xi=−L+2L(i+0.5)N,i=0,1,…,N−1x_i = -L + \frac{2L(i + 0.5)}{N}, \quad i = 0, 1, \ldots, N-1

The step size is Δx=2L/N\Delta x = 2L/N. The integrals become sums:

a0≈1L∑i=0N−1f(xi) Δxa_0 \approx \frac{1}{L} \sum_{i=0}^{N-1} f(x_i)\, \Delta x

an≈1L∑i=0N−1f(xi)cos⁡(nπxiL)Δxa_n \approx \frac{1}{L} \sum_{i=0}^{N-1} f(x_i) \cos\left(\frac{n\pi x_i}{L}\right) \Delta x

bn≈1L∑i=0N−1f(xi)sin⁡(nπxiL)Δxb_n \approx \frac{1}{L} \sum_{i=0}^{N-1} f(x_i) \sin\left(\frac{n\pi x_i}{L}\right) \Delta x

Square Wave Example

A square wave on [−π,π][-\pi, \pi] is f(x)=1f(x) = 1 if x>0x > 0, else −1-1. Its Fourier series contains only odd sine harmonics:

f(x)=4π[sin⁡(x)+sin⁡(3x)3+sin⁡(5x)5+⋯ ]f(x) = \frac{4}{\pi} \left[ \sin(x) + \frac{\sin(3x)}{3} + \frac{\sin(5x)}{5} + \cdots \right]

So b1=4/π≈1.2732b_1 = 4/\pi \approx 1.2732, b3=4/(3π)≈0.4244b_3 = 4/(3\pi) \approx 0.4244, and all an=0a_n = 0.

Your Task

Implement the four Fourier series functions. Use N=1000 sample points on the midpoint grid. The f_values input is a list of N function values at the grid points.

Python runtime loading...
Loading...
Click "Run" to execute your code.