On the Fourier transform

The fourier transform is this black box operation which takes in a function in terms of time and returns a function in terms of frequency. However treating the fourier transform as a pure black box is foolish, especially when what's inside isn't necessarily something that we need to be scared of.

Sailor Moon

Linear Algebra and the Orthonormal Basis Vectors

I have seen a few "intuitive" explanations of the mathematics behind the fourier transform and although they do a good job of explaining the intuition behind the fourier transform at a high level, I still feel that they fail capture the essence of what the fourier transform really is doing at a low level. Which I feel is a shame because it is actually very cool, so let's talk about the mathematics foundational to fourier analysis.


Suppose that we had some function f:DCf:D\to\mathbb{C} mapping some domain DD to the complex numbers. The set of such functions ff forms a vector space. We can add and scale functions as we would expect:

(f+g)(x)f(x)+g(x)and(af)(x)af(x)(f + g)(x) \equiv f(x) + g(x) \quad \text{and} \quad (af)(x) \equiv af(x)

It is reasonable then to expect there to be a set of basis vectors, as in there is some minimal set of vectors {e1,e2,e3,}\{e_1, e_2, e_3, \dots \} for which any function can be written as a linear combination of our basis vectors.

f=a1e1+a2e2+f = a_1\cdot e_1 + a_2 \cdot e_2 + \dots

Where a1,a2,a_1, a_2, \dots are complex number themselves. Much like how every point in 33 dimensional space can be written as a linear combination of (1,0,0)(1,0,0), (0,1,0)(0,1,0) and (0,0,1)(0,0,1).

The Familiar 33 Dimensional Vector Space

Notice the three vectors (1,0,0)(1,0,0), (0,1,0)(0,1,0) and (0,0,1)(0,0,1) are normal which means that their respective magnitudes are exactly 11.

Also notice that these three vectors are orthogonal, what this means in the context of 33 dimensional space is that the angle between each of the respective vectors is exactly 9090^\circ or in other words cosθ=0\cos \theta = 0 where θ\theta is the angle between the two vectors. Which is also to say that two vectors v1v_1 and v2v_2 in 33 dimensional spaces are orthogonal if and only if v1v2cosθ=0|v_1|\cdot|v_2|\cdot\cos \theta = 0.

We may recognize this as the dot product between the vectors v1v_1 and v2v_2, so we can say that two vectors are normal if the "dot" product of the two vectors is 00.

0=[x1x2][y1y2]=x1y1+x2y2+0 = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \end{bmatrix} \cdot \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ \end{bmatrix} = x_1 y_1 + x_2 y_2 + \dots

Whats interesting about the dot product is that for any vector we can "extract" out one of the components of the vector by using the dot product.

If we had the vector [23]\begin{bmatrix} 2 \\ 3 \end{bmatrix} we could extract out the x\vec x component of the vector by dotting it with one of our orthonormal basis vectors [10]\begin{bmatrix} 1 \\ 0 \end{bmatrix}, we could also extract out the y\vec y component of this vector by dotting it with [01]\begin{bmatrix} 0 \\ 1 \end{bmatrix}. Note that by extracting out the x\vec x and y\vec y components of 22 and 33, we have enough information to fully reconstruct the original vector. This is like a sommelier, who is able to pick out the individual components of a wine, you could imagine that this process if extended to arbitrary functions could be quite useful.

The fourier transform comes from extending the idea of the dot product to the vector space of functions and we will now refer to this dot product as the inner product.

The Inner Product

An inner product is simply a function ,:V×VC\braket {\cdot, \cdot} : V \times V \to \mathbb{C}, taking in two elements of a vector space and outputting a complex number.

The inner product is not just any function however, it is a function satisfying the following rules:

  1. 0,x=0\braket{\bold{0}, x} = 0
  2. x,y=y,x\braket {x,y} = \overline{\braket{y,x}}
  3. ax+by,z=ax,z+by,z\braket{ax+by, z} = a\braket{x, z} + b\braket{y, z} where aa and bb are scalers
  4. x,x0\braket{x,x} \ge 0 with equality only when xx itself is 00

If we have a set of orthonormal basis vectors {e1,e2,}\{e_1, e_2, \dots\} then we have that ei,ei=1\braket{e_i, e_i} = 1 and ei,ej=0\braket{e_i, e_j} = 0 when iji \neq j

Now finally if we have a function

f=a1e1+a2e2+a3e3+=aieif = a_1 e_1 + a_2 e_2 + a_3 e_3 + \dots = \sum a_i e_i

We have that f,ej=aiei,ej=ajej,ej=aj\braket{f, e_j} = \sum{a_i\braket{e_i,e_j}} = a_j\braket{e_j, e_j} = a_j

This is in essence what fourier analysis is about, we are able to split any function up into components that make it up as long as we find a suitable inner product and our set of components are orthonormal basis vectors.

A Set of Orthonormal Basis Vectors

I claim that the set of functions B={,eix,e0,eix,}B = \{\dots, e^{-ix} , e^0 , e^{ix}, \dots\} forms a set of orthonormal basis vectors over the vector-space of continuous functions with period 2π2\pi with the inner product

f,g=12πππf(x)g(x)dx\braket{f,g} = \frac{1}{2\pi} \int_{-\pi}^{\pi} f(x) \overline{g(x)} dx

I would like to think that these are a very natural choice for a set of basis functions for the vector-space of continuous functions with period 2π2\pi as eixe^{ix} is pretty much the most fundamental periodic function (you cannot have sin\sin, cos\cos or any of the other trig functions without eixe^{ix})

We can prove that for any function f:RCf : \mathbb{R} \to \mathbb{C} which is periodic with period 2π2\pi and for any large nZ+n \in \mathbb{Z}^+ that we pick there is a linear combination of functions in BB equal to gg for which f(x)=g(x)f(x) = g(x) for at least nn different values of xx. This is not too hard to prove although a little tedious so is left as an exercise to the reader. (Hint: Linear Simultaneous Equation)

Note that is is also useful to think of these functions as mapping (π,π)(-\pi, \pi) to C\mathbb{C}

We now show that the functions in BB are normal.

einx,einx=12πππeinxeinxdx=12πππ1dx=2π2π=1\braket{e^{inx}, e^{inx}} = \frac{1}{2\pi} \int_{-\pi}^{\pi} e^{inx} \cdot e^{-inx} dx = \frac{1}{2\pi} \int_{-\pi}^{\pi} 1 dx = \frac{2\pi}{2\pi} = 1

If you are observant then you might remember that the word orthonormal contains both ortho and normal. This means that we also have to show that our vectors are orthogonal.

einx,eimx=12πππei(nm)xdx=12iπ(nm)[ei(nm)x]ππ\braket{e^{inx}, e^{imx}} = \frac{1}{2\pi}\int_{-\pi}^{\pi} e^{i(n-m)x} dx = \frac{1}{2i\pi(n-m)}\left[e^{i(n-m)x}\right]_{-\pi}^{\pi}

Which is just going to be 00.

So indeed our basis vectors are ortho and normal.

The Basel Problem

We will now use fourier analysis to solve the following problem using the set of orthonormal basis vectors described above.

112+122+132=  ??\frac{1}{1^2} + \frac{1}{2^2} + \frac{1}{3^2} \dots = \; ??

What we want is a function such that

f,f=112+122+132+\braket{f,f} = \frac{1}{1^2} + \frac{1}{2^2} + \frac{1}{3^2} + \dots

Okay well lets take the function f(x)=xf(x) = x. I now claim that

f,einx=i(1)nn\braket{f, e^{inx}} = \frac{i(-1)^n}{n}

With the exception of n=0n = 0 where the inner product evaluates to 00. Again this is relatively easy to prove, but slightly tedious.

Then we know that when f=aieif = \sum a_ie_i

f,f=aiai=f,einx2\braket{f,f} = \sum a_i\cdot\overline{a_i} = \sum |\braket{f, e^{inx}}|^2

This is actually really really good, using the inner product we are able to write the same expression in two different ways.

12πππx2dx=2i(1)nn2=21n2\frac{1}{2\pi} \int_{-\pi}^{\pi}x^2 dx = 2\sum \left|\frac{i(-1)^n}{n}\right|^2 = 2\sum \frac{1}{n^2}

This gives us that

n=11n2=112π[x3]ππ=π26\sum_{n=1}^\infin \frac{1}{n^2} = \frac{1}{12\pi}\left[ x^3 \right]^{\pi}_{-\pi} = \frac{\pi^2}{6}

Yay! we have manage to achieve what Euler did 1735.


Fourier Series

If you've managed to follow along then you might realise this means that if f(x)=xf(x) = x for x[π,π]x \in [-\pi, \pi] then we can also write

f(x)=n0i(1)nneinxf(x) = \sum_{n \neq 0} \frac{i(-1)^n}{n} e^{inx}

Let's try and approximate the value of this infinite sum and plot the result.

import numpy as np
import matplotlib.pyplot as plt

k = 100
def f(x):
    y = 0
    for n in range(-k, k+1):
        if n == 0:
            continue
        y += (1j * (-1)**n / n) * np.exp(1j*n*x)
    return y

X = np.linspace(-np.pi*1.1, np.pi*1.1, 1000)
Y = np.array([f(x) for x in X])

plt.figure(figsize=(10,10))
plt.plot(X,Y)

xticks = np.arange(-np.pi, np.pi + np.pi/2, np.pi/2)
xtick_labels = [
   r'$-\pi$', 
   r'$-\frac{\pi}{2}$',
   r'$0$', 
   r'$\frac{\pi}{2}$', 
   r'$\pi$'
]

plt.xticks(xticks, xtick_labels)

plt.xlabel("$x$")
plt.ylabel("$f(x)$")


plt.show() 

Plot of f(x)f(x) approximated with 200200 terms

Plot of f(x)

We call the above infinite sum the fourier series of f(x)=xf(x) = x

The Jump from Fourier Series to the Fourier Transform

We see that we can write any periodic function with period 2π2\pi as a linear combination of exp(inx)\exp(inx) terms. This means that we can actually write any periodic with a period TT as a combination of exp(2iπnxT)\exp(\frac{2i\pi nx}{T}) terms. Can we use the same technique as we used before to extract these components out of a periodic function ff with period TT?

Let's consider the set of orthonormal basis vectors {exp(2iπnT)nZ}\{ \exp(\frac{2i \pi n}{T}) \mid n \in \mathbb{Z} \}, of course it's not really a set of orthonormal basis vectors unless they are normal and pairwise orthogonal and for that we need an inner product because how can you even claim the inner product between two vectors is 00 without defining an inner product first.

Well I claim that the following choice of inner product is what we are looking for

f,g=1TT2T2f(x)g(x)dx\braket{f, g} = \frac{1}{T}\int_{\frac{T}{2}}^{\frac{T}{2}} f(x)\overline{g(x)} dx

Again the details are not too bad but are the exact same as what we have previously done, so I'll skip over them.

Now the idea of the fourier transform is to take the limit of TT as TT approaches infinity. What this means is that our vector space approaches the set of functions which have an infinite period. Which just means any function. We can also think of this as expanding the function domain (T2,T2)(\frac{-T}2, \frac{T}2) to (,)(-\infin, \infin)

Now our set of orthonormal basis vectors becomes {exp(nX)nZ}\{ \exp(\frac{n}{X}) \mid n \in \mathbb{Z} \} where XX is very large. This allows us to get nX\frac{n}{X} to be arbitrarily close to any real number and so we can think of the basis vectors of this new vector-space to be the set of functions e2πitxe^{2\pi i \cdot tx} for any real number tt. Which corresponds to the component of a function ff with frequency tt.

Using XX to be a very large number

f,e2πitx=1Xf(x)e2πitxdx\braket{f, e^{2\pi itx}} = \frac{1}{X}\int^{\infty}_{-\infty} f(x) \cdot e^{2\pi i \cdot tx} dx

We can say that the "magnitude" of the component of function ff with frequency tt is proportional to

F(t)=f(x)exp(2πitx)dxF(t) = \int^\infin_{-\infin} f(x) \cdot \exp(2\pi i \cdot tx) dx

This last part is very hand-wavy but it is a very natural extension of the mathematics used to create fourier series. It can also be seen that we turn the 1T\frac{1}{T} ito two 1T\frac{1}{\sqrt{T}}'s which we shift into the basis functions.

But more or less we now see exactly why it is the case that the fourier transform can transform a function in terms of time into a function in terms of frequency. It is because the fourier transform is just the limit of an inner product which extracts out discrete frequency components from a function.

While you can intuitively think that the fourier transform has constructive interference when tt aligns with the "frequency" of ff and destructive interference otherwise I think that this is a very useful way to think about the fourier transform but also misses a very cool detail which I think is very sad.