Lesson 18 of 18

Fourier Sine Coefficients

Fourier Series

Any periodic function can be expressed as a sum of sines and cosines — the Fourier series:

f(x)=a02+∑n=1∞[ancos⁡(nx)+bnsin⁡(nx)]f(x) = \frac{a_0}{2} + \sum_{n=1}^{\infty} \left[a_n \cos(nx) + b_n \sin(nx)\right]

for functions on [−π,π][-\pi, \pi]. The coefficients are:

an=1π∫−ππf(x)cos⁡(nx) dxbn=1π∫−ππf(x)sin⁡(nx) dxa_n = \frac{1}{\pi}\int_{-\pi}^{\pi} f(x)\cos(nx)\,dx \qquad b_n = \frac{1}{\pi}\int_{-\pi}^{\pi} f(x)\sin(nx)\,dx

Sine Coefficients of f(x)=xf(x) = x

Since f(x)=xf(x) = x is an odd function, all cosine coefficients an=0a_n = 0. The sine coefficients are:

bn=2(−1)n+1nb_n = \frac{2(-1)^{n+1}}{n}

So: x=2sin⁡(x)−sin⁡(2x)+23sin⁡(3x)−⋯on (−π,π)x = 2\sin(x) - \sin(2x) + \frac{2}{3}\sin(3x) - \cdots \quad \text{on } (-\pi, \pi)

nnbnb_n
12.0000
2−1.0000
30.6667

Numerical Computation

Approximate the integral using the left Riemann sum with NN steps on [−π,π][-\pi, \pi]:

bn≈1π∑i=0N−1f(xi)sin⁡(nxi)⋅hwhere h=2πNb_n \approx \frac{1}{\pi} \sum_{i=0}^{N-1} f(x_i) \sin(n x_i) \cdot h \quad \text{where } h = \frac{2\pi}{N}

Your Task

Implement double fourier_bn(f, n, steps) that numerically computes the nn-th Fourier sine coefficient of ff on [−π,π][-\pi, \pi].

TCC compiler loading...
Loading...
Click "Run" to execute your code.