{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"# Discrete Fourier Transform and Fast Fourier Transform\n",
"\n",
"### Examples - Astrophysics\n",
"\n",
"By Jonas Tjemsland, Andreas Krogen, Håkon Ånes and Jon Andreas Støvneng\n",
"\n",
"Last edited: March 11th 2018 \n",
"___"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This Notebook gives a brief introduction to Discrete Fourier Transform (DFT) and the Fast Fourier Transform (FFT). The radix-2 Cooley-Tukey FFT algorithm is implemented and toward the end the physical meaning is explained.\n",
"\n",
"These concepts have a wide area of applications in many different areas in both physics and mathematics, such as signal processing, sound and image filtering, data compression, partial differential equations and multiplication of large integers.\n",
"\n",
"Before reading this notebook one should have an idea of what a Fourier transform and a Fourier series is, but it is not nessecery.\n",
"\n",
"We start by importing the needed packages."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from numpy import random as rnd\n",
"import timeit\n",
"from scipy import fftpack as fft"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Discrete Fourier Transform (DFT)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let $\\vec x = [x_0,x_1,...,x_{n-1}]$ be a vector with $n$ complex (or real) elements. The DFT of $\\vec x$ is the complex vector $\\vec y = [y_0,y_1,...,y_{n-1}]$, where the elements are defined as\n",
"$$y_k=\\sum_{j=0}^{n-1}x_j\\omega^{k\\cdot j},$$\n",
"where $\\omega = \\exp(-2\\pi i /n)$ ($i$ is the imaginary unit) [1]. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def DFT(x):\n",
" \"\"\" Calculates the one dimensional discrete Fourier transform of\n",
" a vector.\n",
" \n",
" :x: double arr. The vector that is being transformed.\n",
" :returns: double arr. The Fourier transform of x.\n",
" \"\"\"\n",
" n = len(x)\n",
" y = [0]*n\n",
" omega = np.exp(-2.0j*np.pi/n)\n",
" for k in range(0,n):\n",
" y[k] = np.sum(x*omega**(np.arange(0,n)*k))\n",
" return y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is easy to realize that the inverse DFT is given by\n",
"$$x_k = \\sum_{j=0}^{n-1} y_j\\omega^{k\\cdot j},$$\n",
"where $\\omega = \\exp(2\\pi i/n)$."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def inverseDFT(y):\n",
" \"\"\" Calculates the inverse one dimensional discrete Fourier\n",
" transform of a vector.\n",
" \n",
" :y: double arr. The vector that is being transformed.\n",
" :returns: double arr. The inverse Fourier transform of y.\n",
" \"\"\"\n",
" n = len(y)\n",
" x = [0]*n\n",
" omega = np.exp(2.0j*np.pi/n)\n",
" for k in range(0,n):\n",
" x[k] = np.sum(y*omega**(np.arange(0,n)*k))/float(n)\n",
" return x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us try with a small example where we simply transform and inverse transform an arbitrary vector."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x = [7 4 7 5 0 7 0 2]\n",
"y = [ 32.00+0.j 2.76-7.j 0.00-4.j 11.24+7.j -4.00-0.j 11.24-7.j\n",
" -0.00+4.j 2.76+7.j]\n",
"x = [ 7.-0.j 4.-0.j 7.-0.j 5.+0.j -0.+0.j 7.+0.j 0.+0.j 2.+0.j]\n"
]
}
],
"source": [
"# Defining an array that is being transformed.\n",
"x = rnd.randint(8,size=8)\n",
"print('x =', x)\n",
"# The Fourier transform\n",
"y = DFT(x)\n",
"print('y =', np.round(y,2))\n",
"# The invese Fourier transform. \n",
"x = inverseDFT(y)\n",
"print('x =', np.round(x,2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you already might have noticed, this DFT-algorithm is quite inefficient. There are many subcalculations that are performed more than once, and as a consequence the complexity of this algorithm is $\\mathcal O(n^2)$. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fast Fourier Transform (FFT)\n",
"The FFT algorithms exploits symmetries and that many operations are similar. In this notebook we are going to discuss the Cooley–Tukey algorithm [2].\n",
"\n",
"Assume that $N$ is composite. This means that $N=n_1\\cdot n_2$, where $N$, $n_1$ and $n_2$ are integers. Rewrite the two indicies as \n",
"$$k=n_2k_1+k_2,$$\n",
"$$j = n_1j_2 + j_1,$$\n",
"where $k_{1,2} = 0,1,...,n_{1,2}-1$ and $j_{1,2} = 0,1,...,j_{1,2}-1$. If we insert these new indicies into the DFT, some cross terms vanish, and the final result is\n",
"$$y_{n_2k_1+k_2}=\\sum_{j_1=0}^{n_1-1}\\sum_{j_2=0}^{n_2-1}x_{n_1j_2+n_1}\\exp\\left[\\frac{-2\\pi i}{n_1n_2}(n_1j_2+j_1)(n_2k_1+k_2)\\right]$$\n",
"$$=\\sum_{j_1=0}^{n_1-1}\\exp\\left[-\\frac{2\\pi i}{n}j_1k_2\\right]\\left(\\sum_{j_2=0}^{n_2-1}x_{n_1j_2+j_1}\\exp\\left[-\\frac{2\\pi i}{n_2}j_2k_2\\right]\\right)\\exp\\left[-\\frac{2\\pi i}{n_1}j_1k_1\\right].$$\n",
"In this equation each inner sum is a DFT of size $n_2$ and each outer sum is a DFT og size $n_1$. This yields a recursive formula for computing the DFT, which is explained in more detail in [1] and [4]. For simplicity, let us use the radix-2 algorithm. The complexity of the FFT algorithm is $\\mathcal O (n\\log n)$, which makes it almost linear for large data sets!"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def CooleyTukeyRadix2FFT(x):\n",
" \"\"\" Calculates the one dimensional discrete Fourier transform of\n",
" a vector using the radix-2 Cooley-Tukey FFT algorithm. The vector\n",
" that is being transformed must have a power of 2 number of elements.\n",
" \n",
" :x: double arr. The vector that is being transformed.\n",
" :returns: double arr. The Fourier transform of x.\n",
" \"\"\"\n",
" # Check if n is a power of 2.\n",
" if ( len(x) & (len(x) - 1)):\n",
" raise Exception(\"The number of elements in x has to be a power of 2!\")\n",
" # Recursive formula for calculating the FFT.\n",
" def foo(x):\n",
" n = len(x)\n",
" if n == 1:\n",
" y = x\n",
" else:\n",
" y2 = foo(x[0:n:2])\n",
" y1 = foo(x[1:n + 1:2])\n",
" d = np.exp(-2j*np.pi/n)**np.arange(0,n/2)\n",
" y = np.append(y2 + d*y1,y2 - d*y1)\n",
" return y\n",
" return foo(x)\n",
"\n",
"def inverseCooleyTukeyRadix2FFT(y):\n",
" \"\"\" Calculates the one-dimensional inverse discrete Fourier transform of\n",
" a vector using the radix-2 Cooley-Tukey FFT algorithm. The vector\n",
" that is being transformed must have a power of 2 number of elements.\n",
" Parameters:\n",
" x: double arr. The vector that is being transformed.\n",
" Returns:\n",
" y: double arr. The Fourier transform of x.\n",
" \"\"\"\n",
" # Check if n is a power of 2.\n",
" if (len(y) & (len(y) - 1)):\n",
" raise Exception(\"The number of elements in x has to be a power of 2!\")\n",
" # Recursive formula for calculating the FFT.\n",
" def foo(y):\n",
" n = len(y)\n",
" if n == 1:\n",
" x = y\n",
" else:\n",
" x2 = foo(y[0:n:2])\n",
" x1 = foo(y[1:n + 1:2])\n",
" d = np.exp(2j*np.pi/n)**np.arange(0,n/2)\n",
" x = np.append(x2 + d*x1,x2 - d*x1)\n",
" return x\n",
" return foo(y)/len(y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us try with a small example where we simply transform and inverse transform an arbitrary vector as before."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x = [4 6 9 9 4 1 9 2]\n",
"y = [ 44.00+0.j -1.41-8.49j -10.00+4.j 1.41-8.49j 8.00+0.j\n",
" 1.41+8.49j -10.00-4.j -1.41+8.49j]\n",
"x = [ 4.+0.j 6.+0.j 9.+0.j 9.-0.j 4.+0.j 1.-0.j 9.+0.j 2.+0.j]\n"
]
}
],
"source": [
"# Defining the array that is being transformed.\n",
"x = rnd.randint(10,size=8)\n",
"print('x =', x)\n",
"# The Fourier transform.\n",
"y = CooleyTukeyRadix2FFT(x)\n",
"print('y =', np.round(y,2))\n",
"# The invese Fourier transform. \n",
"x = inverseCooleyTukeyRadix2FFT(y)\n",
"print('x =', np.round(x,2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To demonstrate the superiority of the FFT we calculate the Fourier transform of a lot bigger data set. Let us also compare with the fft function from scipy.fftpack."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loop, best of 3: 17.3 s per loop\n",
"10 loops, best of 3: 94.6 ms per loop\n",
"10000 loops, best of 3: 82.7 µs per loop\n"
]
}
],
"source": [
"x = rnd.rand(8192)\n",
"# Time the loop time for DFT, CooleyTukeyRadix2FFT and scipy.fftpack.fft.\n",
"% timeit y = DFT(x)\n",
"% timeit y = CooleyTukeyRadix2FFT(x)\n",
"% timeit y = fft.fft(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Physical meaning\n",
"\n",
"The DFT maps a finite equally spaced sample sequence from its original domain to its frequency domain. In other words, a discrete time data set are transformed into a discrete frequency data set.\n",
"\n",
"To illustrate this, we need to figure out what the DFT-formula physically means. We start by rewriting it as\n",
"$$x_k=\\sum_{j=0}^{n-1}y_j\\exp\\left(2\\pi i\\frac{k}{n\\Delta t}j\\Delta t\\right).$$\n",
"What the expression tells us is simply that $\\vec x$ is a superposition of exponential functions with different frequencies $f_j = \\frac{j}{n\\Delta t}$ and amplitudes $y_j$. Therefore, we can view the magnitude of the amplitudes $|y_k|^2$ as a measure of the \"weight of the frequency $f_j$\" in $\\vec x$!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Multidimensional DFT\n",
"Let $\\vec j = (j_1,j_2,...,j_d)$ and $\\vec k = (k_1,k_2,...,k_d)$ be $d$-dimensional vectors of indicies from $\\vec 0$ to $\\vec n-1 = (n_1-1,n_2,...,n_d-1)$. Then, the $d$-dimensinal DFT is given by\n",
"$$y_\\vec{k}=\\sum_{\\vec j=\\vec 0}^{\\vec n-1}x_\\vec{j}\\exp\\left[-2\\pi\\vec k\\cdot\\vec \\xi\\right],$$\n",
"where $\\vec \\xi$ is the elementwise division $(j_1/n_1,...,j_d/n_d)$ [4]. For example, the two dimensional DFT is given by\n",
"$$\\vec y_{k_1,k_2}=\\sum_{j_1=0}^{n_1-1}\\sum_{j_2=0}^{n_2-1}x_{j_1,j_2}\\exp\\left[-2\\pi i\\left(\\frac{ k_1j_1}{n_1}+\\frac{k_2j_2}{n_2}\\right)\\right].$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"References: \n",
"[1] T. Sauer: Numerical Analysis, second edition, Pearson 2014 \n",
"[2] James W. Cooley and John W. Tukey: An Algorithm for the Machine Calculation of Complex Fourier Series, Math. Comp. 19 (1965), p. 297-301 \n",
"[3] Wikipedia: https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm, 03.28.2016 (acquired: April 2016) \n",
"[4] Wikipedia: https://en.wikipedia.org/wiki/Discrete_Fourier_transform, 04.28.2016 (acquired: April 2016)\n",
"\n",
"Check out the FFT pack in SciPy:\n",
"http://docs.scipy.org/doc/scipy/reference/fftpack.html "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}