Fourier Series

Overview


The Fourier series
{% f(x) \approx \frac{a_0}{2} + \sum_{i=1} ^n (a_n cos \frac{n \pi x}{c} + b_n sin \frac{n \pi x}{c}) %}
converges under a fairly broad set of conditions. Therefore, taking the first {% n %} terms is often used to approximate the target function.

The following demonstrates approximating the function {% f(x) = x^2 %} using the first {% n %} Fourier terms. Moving the slider changes the number of terms taken. (from 0 to 10)

copy

Implementation


As an example, once one has computed the first 3 cosine coefficients, once could write a function that approximates the target function as follows.


let approx = x => {
  return 0.5 * a0 + a1 * Math.cos(x) + a2 * Math.cos(2*x);
}
					


Then, we can chart the results by creating a set of samples as in the following.


let samples = $from(-1 * Math.PI, Math.PI, 1000).map(p=>{
  return {
    x:p,
    y:approx(p)
  };											
})
					
Try it!