Share page

Physics at the intersection between analytics and numerics.

Introduction: Fourier series and transform

Fourier series are commonly used in a wide variety of fields, as is the Fourier transform which will be the focus of the second part of this article. A Fourier series (or Fourier expansion) is simply a sum of $cos$ and $sin$ functions, and one of its most important properties is that it can be used to express any continuous bound function defined on a closed interval. In addition the $cos$ and $sin$ functions has the useful properties that they are eigenfunctions to the Laplace operator, which often helps in reducing the complexity of PDEs where the Laplace operator plays an important role. Fourier series are commonly used in a wide variety of fields, as is the Fourier transform which will be the focus of the second part of this article. A Fourier series (or Fourier expansion) is simply a sum of $cos$ and $sin$ functions, and one of its most important properties is that it can be used to express any continuous bound function defined on a closed interval. In addition the $cos$ and $sin$ functions has the useful properties that they are eigenfunctions to the Laplace operator, which often helps in reducing the complexity of PDEs where the Laplace operator plays an important role.

Definition: Fourier series

Let $f(x)$ be a one-dimensional bound and continuous function defined on $[-L, L]$. Then it's Fourier expansion can be defined as $f(x) = c_0 + \sum_{k}a_kcos(kx) + \sum_{k}b_k\sin(kx)$. where $k = \frac{\pi n}{L}$ for $n \in \mathbb{N}$. Let $f(x)$ be a one-dimensional bound and continuous function defined on $[-L, L]$. Then it's Fourier expansion can be defined aswhere $k = \frac{\pi n}{L}$ for $n \in \mathbb{N}$.

Example

As an example, consider the expressions

$f(x) = \sum_{n=1,3,5,...}\frac{\sin(nx)}{n}$, $g(x) = \sum_{n=1,3,5,...}(-1)^{(n+1)/2}\frac{\sin(nx)}{n^2}$, which are the Fourier expansions for square and triangle waves respectively. Because of the infinite number of terms in these sums it would take an infinite time to evaluate these sums numerically. In reality we therefore often instead define the functions $f_n(x)$ and $g_n(x)$ to be the corresponding sum up to $n$-th order, such that for example $f_5(x) = \sin(x) + \frac{sin(3x)}{3} + \frac{\sin(5x)}{5}$. Such functions can be calculated quickly, and as we see in the figures below a quite small number of terms suffices in order to get something that resembles square and triangle waves. Especially the triangle wave provides a very good approximation already at $n=7$. As can be seen the triangle wave converges almost immediately. This is a consequence of that the $sin$ terms are divided by $n^2$, and therefore only the very first few terms provides important contributions to the sum. In contrast the square waves terms are divided by $n$, which makes it converge much slower. In addition to this slower convergence rate the square wave suffers from another drawback when using Fourier series which also highlights it's limitations. Namely, the square wave is discontinuous and can therefore not be approximated arbitrarily well every where with any finite sum. Note the overshoot that appears close to the discontinuous jumps for $f_{49}(x)$ below. As an example, consider the expressionswhich are the Fourier expansions for square and triangle waves respectively. Because of the infinite number of terms in these sums it would take an infinite time to evaluate these sums numerically. In reality we therefore often instead define the functions $f_n(x)$ and $g_n(x)$ to be the corresponding sum up to $n$-th order, such that for exampleSuch functions can be calculated quickly, and as we see in the figures below a quite small number of terms suffices in order to get something that resembles square and triangle waves. Especially the triangle wave provides a very good approximation already at $n=7$.As can be seen the triangle wave converges almost immediately. This is a consequence of that the $sin$ terms are divided by $n^2$, and therefore only the very first few terms provides important contributions to the sum. In contrast the square waves terms are divided by $n$, which makes it converge much slower. In addition to this slower convergence rate the square wave suffers from another drawback when using Fourier series which also highlights it's limitations. Namely, the square wave is discontinuous and can therefore not be approximated arbitrarily well every where with any finite sum. Note the overshoot that appears close to the discontinuous jumps for $f_{49}(x)$ below.

Interactive example

This tool can be used to experiment with how $f(x)$ is affected by the first few terms in the Fourier expansion. $c_0 =$

$a_1 =$ $b_1 =$

$a_2 =$ $b_2 =$

$a_3 =$ $b_3 =$

$a_4 =$ $b_4 =$

$a_5 =$ $b_5 =$

$a_6 =$ $b_6 =$

$a_7 =$ $b_7 =$

$a_8 =$ $b_8 =$

$a_9 =$ $b_9 =$

This tool can be used to experiment with how $f(x)$ is affected by the first few terms in the Fourier expansion.

Code example: Fourier series

Here we take a look at a simple program that given Fourier coefficients builds the corresponding function. The functions that are built are two square waves $f(x)$ and $g(x)$, expanded to fifth and eleventh order respectively, and a triangle wave $h(x)$ expanded to seventh order. /** FourierExpansion.cpp Simple program that demonstrates how to build a function from it's Fourier coefficients. Two square waves f(x) and g(x) with different number of summed terms and one triangle wave h(x) is created. The result is written to the standard output in a format recognized by many plotting applications. First column is x coordinate. The three following columns contains the values of f(x), g(x) and h(x) respectively. Calculations are done on the interval [-5,5] with a resolution of 500 points.*/ #include <iostream> #include <math.h> using namespace std; //The discretization resolution is set to 500 points, and L=5. const int RESOLUTION = 500; const float L = 5; //Function declarations float* buildFromCoeficients(float c, float* a, float* b, int numCoeficients); int main(){ //Fourier coefficients for square wave. float c_square1 = 0; float a_square1[5] = {0, 0, 0, 0, 0}; float b_square1[5] = {1, 0, 0.33, 0, 0.2}; //Build square function from Fourier coefficients float* f = buildFromCoeficients(c_square1, a_square1, b_square1, 5); //Fourier coefficients for better square wave. float c_square2 = 0; float a_square2[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; float b_square2[11] = {1, 0, 0.33, 0, 0.2, 0, 0.14, 0, 0.11, 0, 0.09}; //Build square function from Fourier coefficients float* g = buildFromCoeficients(c_square2, a_square2, b_square2, 11); //Fourier coefficients for triangle wave. float c_triangle = 0; float a_triangle[7] = {0, 0, 0, 0, 0, 0, 0}; float b_triangle[7] = {1, 0, -0.11, 0, 0.02, 0, -0.01}; //Build triangle function from Fourier coefficients float* h = buildFromCoeficients(c_triangle, a_triangle, b_triangle, 7); //Output the results to the standard output. for(int n = 0; n < RESOLUTION; n++){ float x = 2*L*(n/(float)RESOLUTION) - L; cout << x << "\t" << f[n] << "\t" << g[n] << "\t" << h[n] << endl; } //Clean up delete [] f; delete [] g; delete [] h; return 0; } /** Builds a function from it's Fourier coefficients input: c = constant coefficient a = list of "numCoeficients" lowest coefficients for the cos terms b = list of "numCoeficients" lowest coefficients for the sin terms numCoeficients = the number of coefficients in a and b returns: f(x) c + sum(a_k*cos(kx)) + sum(b_k*sin(kx))*/ float* buildFromCoeficients(float c, float* a, float* b, int numCoeficients){ float* f; f = new float[RESOLUTION]; for(int n = 0; n < RESOLUTION; n++){ //Conversion between discrete index and actual x-value. float x = 2*L*(n/(float)RESOLUTION) - L; f[n] = c; for(int i = 0; i < numCoeficients; i++){ float k = (i+1)*M_PI/L; f[n] += a[i]*cos(k*x) + b[i]*sin(k*x); } } return f; } Here we take a look at a simple program that given Fourier coefficients builds the corresponding function. The functions that are built are two square waves $f(x)$ and $g(x)$, expanded to fifth and eleventh order respectively, and a triangle wave $h(x)$ expanded to seventh order.

The Fourier transformation

So far we have discussed a few examples of Fourier series. The square and triangle waves above nicely demonstrated this, and the square wave also showed on some of the limitations as the discontinuous nature of the square wave resulted in violent oscillations around the discontinuity. Now as mentioned in the introduction, one of the most important properties of the Fourier series is that any continuous bound function can be expressed as such a series. This fact alone would however not make the Fourier series very useful if there was no way of figuring out what the Fourier coefficients for a given function should be. This is where the Fourier transformation enters the stage. So far we have discussed a few examples of Fourier series. The square and triangle waves above nicely demonstrated this, and the square wave also showed on some of the limitations as the discontinuous nature of the square wave resulted in violent oscillations around the discontinuity. Now as mentioned in the introduction, one of the most important properties of the Fourier series is that any continuous bound function can be expressed as such a series. This fact alone would however not make the Fourier series very useful if there was no way of figuring out what the Fourier coefficients for a given function should be. This is where the Fourier transformation enters the stage.

Definition: Fourier transformation

Given a function $f(x)$ defined on the interval $[-L,L]$, its Fourier transformation can be defined as $a_k = \frac{1}{L}\int_{-L}^{L}dx \cos(kx)f(x)$, $b_k = \frac{1}{L}\int_{-L}^{L}dx \sin(kx)f(x)$, $c_0 = \frac{1}{2L}\int_{-L}^{L}dx f(x)$. Here the same symbols $a_k$, $b_k$ and $c_0$ as was used in the definition of the Fourier series have been used, and these are indeed the Fourier coefficients for $f(x)$. However, so far no motivation for why these should be related have been provided. This connection is provided next.



* Note that the exact definition of these, just like that for the Fourier series, can vary slightly from place to place. The only thing that is important when choosing which definition to work with is to make sure that these definitions are compatible in the sense that the proof in the next section still is valid. Given a function $f(x)$ defined on the interval $[-L,L]$, its Fourier transformation can be defined asHere the same symbols $a_k$, $b_k$ and $c_0$ as was used in the definition of the Fourier series have been used, and these are indeed the Fourier coefficients for $f(x)$. However, so far no motivation for why these should be related have been provided. This connection is provided next.

Proof: The Fourier transform gives the Fourier coefficients

*The difficulty of this section may be considered higher than that of the other and can be skipped if the reader is willing to simply accept the definitions above. Conceptually it does not provide anything new and is only of interest to the sceptical reader, or the reader who wants more insight into why the integrals above in fact gives the Fourier coefficients.



Assuming that any function can be expressed as a Fourier series and writing $f(x) = c_0 + \sum a_k\cos(kx) + \sum b_k\sin(kx)$, we can insert this expression into the definitions above for the Fourier transformation. For example $\frac{1}{2L}\int_{-L}^{L}dx(c_0 + \sum a_k\cos(kx) + \sum b_k\sin(kx)) = c_0 + \frac{1}{2L}\sum a_k\int dx\cos(kx) + \frac{1}{2L}\sum b_k\int dx\sin(kx) = c_0$, where in the last step it has been used that the $cos$ and $sin$ functions are periodic on the interval [-L,L] and the integrals over them therefore vanishes.



Now consider instead the right hand side of the first line in the definition of the Fourier transform. Splitting the expression into three parts we have $\frac{1}{L}\int_{-L}^{L}dx\cos(kx)c_0 + \frac{1}{L}\sum_{k'}a_{k'}\int_{-L}^{L}dx\cos(kx)cos(k'x) + \frac{1}{L}\sum_{k'}b_{k'}\int_{-L}^{L}dx\sin(kx)cos(k'x)$. Here the first term again is an integral over a single $cos$ function and therefore evaluates to zero. The second and third terms are a little bit trickier, but using the trigonometric identities $\cos(kx)\cos(k'x) = \frac{\cos((k-k')x) + \cos((k+k')x)}{2}$,

$\sin(kx)\cos(k'x) = \frac{\sin((k-k')x) + \sin((k+k')x)}{2}$, the integrals can once again be seen to be integrals over single $cos$ and $sin$ functions and therefore also disappears. That is with the important exception for the case where $k = k'$, because when $k = k'$ we have $\cos((k-k')x) = 1$ and $\sin((k-k')x) = 0$. Once again writing down the right hand side of the first line in the definition for the Fourier transform but this time ignoring terms we have concluded to be zero we now have $\frac{a_k}{L}\int_{-L}^{L}dx\frac{1}{2} = a_k$, which shows that also this line was an appropriate definition.



Finally the right hand side of the second line can be show to indeed give $b_k$. Using also the third trigonometric identity $\sin(kx)\sin(k'x) = \frac{\cos((k-k')x) - \cos((k+k')x)}{2}$,

the proof is completely analogous to that for the first line and is therefore omitted. Assuming that any function can be expressed as a Fourier series and writing $f(x) = c_0 + \sum a_k\cos(kx) + \sum b_k\sin(kx)$, we can insert this expression into the definitions above for the Fourier transformation. For examplewhere in the last step it has been used that the $cos$ and $sin$ functions are periodic on the interval [-L,L] and the integrals over them therefore vanishes.Now consider instead the right hand side of the first line in the definition of the Fourier transform. Splitting the expression into three parts we haveHere the first term again is an integral over a single $cos$ function and therefore evaluates to zero. The second and third terms are a little bit trickier, but using the trigonometric identitiesthe integrals can once again be seen to be integrals over single $cos$ and $sin$ functions and therefore also disappears. That is with the important exception for the case where $k = k'$, because when $k = k'$ we have $\cos((k-k')x) = 1$ and $\sin((k-k')x) = 0$. Once again writing down the right hand side of the first line in the definition for the Fourier transform but this time ignoring terms we have concluded to be zero we now havewhich shows that also this line was an appropriate definition.Finally the right hand side of the second line can be show to indeed give $b_k$. Using also the third trigonometric identitythe proof is completely analogous to that for the first line and is therefore omitted.