Fourier transform
It’s integeral transform that takes a function as input then ouputs another function that describes which frequenties are present in the original function
Definition
Allow you to transform any periodic function into a sum of sines and cosines.
Jean Baptiste Joseph Fourier (1768–1830)
\(F(u)\) holds the amplitude and phase of the sinusoid of frequency \(u\).
The amplitude is
and the phase is
Discrete Fourier Transform (DFT)
Recall the continuous Fourier transform in time:
The discrete version is
where
Here:
\(N\) is the total number of samples.
\(k\) is the frequency index, ranging from \(0\) to \(N-1\).
\(X_k\) is generally complex, representing both amplitude and phase of frequency component \(k\).
Expanding the sum using Euler’s formula \(e^{-i\theta} = \cos(\theta) - i\sin(\theta)\):
where \(A_k\) and \(B_k\) correspond to the real and imaginary parts, respectively.
Computing \(X_k\) in Practice
Imagine you have a simple \(\sin(x)\) function from \(0\) to \(2\pi\). You then sample, for example, 5 points — that is, \(N=5\).
Assume you want to compute \(X_1\) (so \(k=1\)). You know \(N\), and you have all values of \(x_n = \sin(x_n)\) for \(n=0,1,2,3,4\). You plug them into
At the end, you’ll get a complex number representing that frequency component.
You would then repeat the process for \(k=2,3,4,\) and so on.
How many \(k\) Values?
If you have \(N\) samples, then you can compute \(N\) discrete frequency components:
However:
Because the DFT of real-valued signals is symmetric, you typically only need to examine up to \(k = N/2\) (the “positive frequencies”).
The other half (\(k > N/2\)) contains the complex-conjugate mirror of those components.
So, for \(N = 5\), you can compute \(k = 0, 1, 2, 3, 4\), but often interpret only the first \(\lfloor N/2 \rfloor\) as unique frequency components.
How would you do this in 2D?
Let \(f(x,y)\) be a continuous function of spatial coordinates \(x, y\). Its 2D Fourier transform is
where \(u, v\) are the spatial frequencies (cycles per unit length) along \(x\) and \(y\).
The inverse transform is
Discrete 2D DFT (samples on an \(M\times N\) grid)
Assume you have discrete samples \(f[x,y]\) for
and you want their 2D DFT \(F[u,v]\) defined at the discrete frequency indices
DFT:
Inverse DFT (IDFT):
Example with numpy
Here is an example of 2D Fourier transform where frequencies/pixels are shown as dots below:
[ ]:
# Source: https://thepythoncodingbook.com/2021/08/30/2d-fourier-transform-in-python-and-fourier-synthesis-of-images/
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-500, 501, 1)
X, Y = np.meshgrid(x, x)
wavelength = 100
angle = np.pi/3
grating = np.sin(
2*np.pi*(X*np.cos(angle) + Y*np.sin(angle)) / wavelength
)
plt.set_cmap("gray")
plt.subplot(121)
plt.imshow(grating)
# Calculate Fourier transform of grating
# Shift the zero-frequency component to the center
ft = np.fft.fft2(grating)
ft = np.fft.fftshift(ft)
plt.subplot(122)
plt.imshow(abs(ft))
plt.xlim([480, 520])
plt.ylim([520, 480]) # Note, order is reversed for y
plt.show()
