Numerical Methods Week 10

Fourier Series and Transforms

Learning outcomes:


  • Numerically calculate Fourier series of functions.
  • Perform numerical Fourier transform of functions.

  • Matt Watkins mwatkins@lincoln.ac.uk

    Fourier Series

    Expanding a function in a series of basis functions, particularly periodic sin and cosines can be very useful.

  • May help analysing a function.
  • Can be used make a filter - removing high or low frequency noise.
  • Useful solving some differential equations.
  • ...
  • Suppose we have a periodic function - this means that $$ f(x+T) = f(x) $$ where $T = 2l$ is the period.

    Assume we can expand the function as a sum of (known) functions with the same periodicity $$ \phi_n(x) = \cos \left( \frac{n \pi x}{l} \right) \text{ and } \psi_n(x) = \sin \left( \frac{n \pi x}{l} \right) $$ where $n \in \mathbb{Z}$.

    Check that all the $\phi_n(x)$ and $\psi_n(x)$ have periodicity $T$ (by considering the functions at $x$ and $x+T$).

    Note that this notation for $ \phi_n(x) = \cos \left( \frac{n \pi x}{l} \right)$ and $\psi_n(x) = \sin \left( \frac{n \pi x}{l} \right) $ will be used throughout this lecture.


    Orthogonal Functions

    $\phi_n(x)$ and $\psi_n(x)$ have the special properties that $$ \int_{-l}^{l} \phi_n(x) \phi_m(x) \text{d}x = l \delta_{nm} \text{and} \int_{-l}^{l} \psi_n(x) \psi_m(x) \text{d}x = l \delta_{nm} $$ where $\delta_{nm}$ is the Kronecker delta. $$ \delta_{nm} = \begin{cases} 1 \text{ for } n = m \\ 0 \text{ for } n \neq m \end{cases} $$

    Also $$ \int_{-l}^{l} \phi_n(x) \psi_m(x) \text{d}x = 0 $$ for all $n,m$

    Fourier Series

    Assume that we can expand our function on the interval $[-l,l]$ as $$ \begin{align*} f(x) & = \frac{a_0}{2} + \sum_{n=1}^{\infty} [a_n \phi_n(x) + b_n \psi_n(x)]\\ & = \frac{a_0}{2} + \sum_{n=1}^{\infty} [a_n \cos \left( \frac{n \pi x}{l} \right) + b_n \sin \left( \frac{n \pi x}{l} \right)] \end{align*} $$ This isn't actually true for all $x$, but it is good enough for now.

    Note this will repeat over $[l,3l]$ and other $[il,(i+2)l]$ where $i \in \mathbb{Z}$

    We can use the orthogonality of the functions to calculate the coefficients, $a_m$ and $b_m$. To do this we multiply by the function whose coefficient we wish to find, and integrate over $T$.

    For instance, to find $a_m$ the coefficient of $\cos \left( \frac{m \pi x}{l} \right)$ we multiply by $\phi_m = \cos \left( \frac{m \pi x}{l} \right)$ and integrate over the period: $$ \begin{align*} \int_{-l}^{l}\phi_m(x) f(x) \text{d}x & = \int_{-l}^{l}\phi_m(x) \frac{a_0}{2} \text{d}x + \int_{-l}^{l}\phi_m(x) \sum_{n=1}^{\infty} [a_n \phi_n(x) + b_n \psi_n(x)] \text{d}x \\ & = \frac{a_0}{2} \int_{-l}^{l}\phi_m(x)\text{d}x + \sum_{n=1}^{\infty} [ a_n \int_{-l}^{l}\phi_m(x) \phi_n(x)\text{d}x + b_n \int_{-l}^{l}\phi_m(x) \psi_n(x)\text{d}x] \end{align*} $$ Using the orthogonality relations only the term containing $a_m$ survives, giving $$ a_m = \frac{1}{l}\int_{-l}^{l}\phi_m(x) f(x) \text{d}x $$

    To find $b_m$ we multiply by $\psi_m$ and integrate over the period: $$ \begin{align*} \int_{-l}^{l}\psi_m(x) f(x) \text{d}x & = \int_{-l}^{l}\psi_m(x) \frac{a_0}{2} \text{d}x + \int_{-l}^{l}\psi_m(x) \sum_{n=1}^{\infty} [a_n \phi_n(x) + b_n \psi_n(x)] \text{d}x \\ & = \frac{a_0}{2} \int_{-l}^{l}\psi_m(x)\text{d}x + \sum_{n=1}^{\infty} [ a_n \int_{-l}^{l}\psi_m(x) \phi_n(x)\text{d}x + b_n \int_{-l}^{l}\psi_m(x) \psi_n(x)\text{d}x] \end{align*} $$ Using the orthogonality relations only the term containing $b_m$ survives, giving $$ b_m = \frac{1}{l}\int_{-l}^{l}\psi_m(x) f(x) \text{d}x $$

  • Check that $a_m = \frac{1}{l}\int_{-l}^{l}\phi_m(x) f(x) \text{d}x$ and $b_m = \frac{1}{l}\int_{-l}^{l}\psi_m(x) f(x) \text{d}x$
  • Numerical Fourier Series

    To calculate the terms in the fourier series we need to numerically integrate our function.

    Assuming that we have a series of $n$ evenly spaced $\{x,f(x)\}$ values we can simply calculate the integrals using the trapezium rule as $$ a_m = \frac{1}{l} \sum_{i=0}^{n-1} \phi_m(x_i) f(x_i) \Delta x $$ This discretization is very similar to what we did in previous weeks - we discretize the period $2l$ along the $x$ axis with $n$ points in each period. The $n+1$th point is a replica of $0$th. We don't want to include the $n+1$th point at $2l$ because it is the same as the first one. $\Delta x$ will be the period of the function divided by the number of points, $\Delta x = \frac{2l}{n}$.

    Suppose we want to sample a function with period $2 \pi$ with n = 4 points in each period. This means the 5th point will be at $2 \pi$ the beginning of the next period. The spacing between the points will be the period divided by the 4 spaces between the 5 points. So $\Delta x = 2 \pi / 4 = \pi / 2$.

    Note that there is a maximum frequency of wave that can be represented. Don't go above $n/2$ terms in the Fourier series otherwise you will get 'aliasing'.

    =

  • Write code to perform these integrations. Check the orthogonality conditions are satisfied by your code.
  • Calculate the Fourier series of the periodic function $f(x) = \cos(x)$ within $[-\pi ,\pi]$, which then repeats itself every $2 \pi$
  • Calculate the Fourier series of the periodic function $f(x) = \cos(x) + \cos(2x)$ within $[-\pi ,\pi]$.


  • Extra things to try.

  • Calculate the Fourier series of the periodic function $f(x) = x^2$ within $[-\pi ,\pi)$, which then repeats itself every $2 \pi$
  • Calculate the Fourier series of the periodic function $f(x) = x$ within $[-\pi ,\pi)$, which then repeats itself every $2 \pi$
  • What does the previous series converge to at $x = -\pi$
  • Parseval's Theorem

    I the 'power' of the function is conversed, or multiplying on both sides by $f(x)$ and integrating over the period $$ \frac{1}{l} \int_{-l}^{l} f(x)^2 \text{d}x = \frac{a_0^2}{2} + \sum_{n=1}^{\infty} [a_n ^2 + b_n ^2] $$

  • Check that Parseval's Theorem is satisfied for $f(x) = x^2$.
  • Check that Parseval's Theorem is satisfied for $f(x) = x$ taking the periodic interval to be $[-2 \pi, 0)$.
  • Other forms of Fourier Series

    Odd or even functions can be expanded as just $\sin$ or $\cos$ series.

    The expansion can be written in terms of complex exponentials instead of $\sin$ and $\cos$ terms - instead of our functions $\phi$ and $\psi$ we use $$ \chi_n(x) = \exp \left( \frac{inx\pi}{l} \right) $$ and expand $f(x)$ as $$ f(x) = \sum_{n=-\infty}^{\infty}c_n \chi_n(x) $$

    This is just rewriting the series, because $\chi_n$ are just linear combinations of $\phi_n$ and $\psi_n$. The $\chi_n$ also satisfy orthogonality relations, and the coefficients can be found in the same way - multiply by the complex conjugate of the function whose coefficient you want to find and then integrate over the period. $$ c_n = \int_{-l}^{l} \chi^*_n(x) f(x) \text{d}x $$

    Solving Differential Equations

    Differential equations can often be transformed into algebraic form using Fourier series. Often the exponential form is most easy to use.

    Take an example of the from $$ \ddot{y} + \omega_0^2 y = f(t) $$ where the function $f(t)$ is a periodic function with the period $T = 2 \pi / \omega$. We can expand $f(t)$ as a Fourier series $$ f(t)= \sum_{n = -\infty}^{\infty}f_n e^{in\pi t \omega / \pi} = \sum_{n = -\infty}^{\infty}f_n e^{in \omega t } $$ with $$ f_n = \frac{1}{T}\int_{-T/2}^{T/2}f(t)\exp(-in\omega t)\text{d}t $$

    - but we can also expand $y$ as a fourier series with the same period, $T$. $$ y(t)= \sum_{n = -\infty}^{\infty}y_n e^{in \omega t } $$ substituting and performing the straightforward derivative of the exponential we get $$ \sum_{n = -\infty}^{\infty} \left\{ \left[-(n \omega)^2 + \omega_0^2\right]y_n -f_n \right\}\exp(in\omega t) = 0 $$ which implies that $$ y_n = \frac{f_n}{\omega_0^2 - (n \omega^2)} $$ From which the full solution can be found. $$ y(t) = \sum_{n = -\infty}^{\infty} \frac{f_n}{\omega_0^2 - (n \omega^2)} \exp(in\omega t) $$

    Summary and Further Reading

    You should be reading additional material to provide a solid background to what we do in class

    All the textbooks contain sections on root finding and solving non-linear equations, for instance chapters 12 and 13 of Numerical Recipes.