To be applied to discrete audio data, the Fourier transform must be rendered in a discrete form. This is given in the equation for the discrete Fourier transform below.

Equation 2.13 Discrete Fourier transform

Aside:

The second form of the discrete Fourier transform given in Equation 2.1, $\frac{1}{N}\left (\sum_{k=0}^{N-1}f_{k}e^{\frac{-i2\pi kn}{N}} \right )$, uses the constant e.  It is derivable from the first by application of Euler’s identify, $e^{i2\pi kn}=\cos \left ( 2\pi kn \right )+i\sin \left ( 2\pi kn \right )$. To see the derivation, see (Burg 2008).

Notice that we’ve switched from the function notation used in Equation 2.12 ( $F\left ( n \right )$ and F\left ( k \right )) to array index notation in Equation 2.13 ( F_{n} and f_{k}) to emphasize that we are dealing with an array of discrete audio sample points in the time domain. Casting this equation as an algorithm (Algorithm 2.1) helps us to see how we could turn it into a computer program where the summation becomes a loop nested inside the outer for loop.

/*Input:

f, an array of digitized audio samples
N, the number of samples in the array
Note:  $i=\sqrt{-1}$

Output:

F, an array of complex numbers which give the frequency components of the sound given by f */

for (n = 0 to N - 1 )

$F_{n}=\frac{1}{N}\left ( \sum \begin{matrix}N-1\\k=0 \end{matrix} f_{k}\, cos\frac{2\pi nk}{N}-if_{k}\, sin\frac{2\pi nk}{N}\right )$

Algorithm 2.1 Discrete Fourier transform

Each time through the loop, the magnitude and phase of the nth frequency component are computed, $F_{n}$. Each $F_{n}$ is a complex number with a cosine and sine term, the sine term having the factor i in it.

We assume that you're familiar with complex numbers, but if not, a short introduction should be enough so that you can work with the Fourier algorithm.

A complex number takes the form $a+bi$, where $i=\sqrt{-1}$. Thus,  $cos\frac{2\pi nk}{N}-if_{k}\: sin\left ( \frac{2\pi nk}{N} \right )$ is a complex number. In this case, a is replaced with $cos\frac{2\pi nk}{N}$ and b with $-f_{k}\: sin\left ( \frac{2\pi nk}{N} \right )$.  Handling the complex numbers in an implementation of the Fourier transform is not difficult. Although i is an imaginary number, $\sqrt{-1}$, and you might wonder how you’re supposed to do computation with it, you really don’t have to do anything with it at all except assume it’s there. The summation in the formula can be replaced by a loop that goes from 0 through N-1.   Each time through that loop, you add another term from the summation into an accumulating total. You can do this separately for the cosine and sine parts, setting aside i. Also, in object-oriented programming languages, you may have a Complex number class to do complex number calculations for you.

The result of the Fourier transform is a list of complex numbers F, each of the form $a+bi$, where the magnitude of the frequency component is equal to $\sqrt{a^{2}+b^{2}}$.

The inverse Fourier transform transforms audio data from the frequency domain to the time domain. The inverse discrete Fourier transform is given in Algorithm 6.2.

/*Input:

F, an array of complex numbers representing audio data in the frequency domain, the elements represented by the coefficients of their real and imaginary parts, a and b respectively N, the number of samples in the array

Note: $i=\sqrt{-1}$

Output: f, an array of audio samples in the time domain*/

for $(n=0 to N-1)$

$f_{k}=\sum \begin{matrix}N-1\\ n=0\end{matrix}\left ( a_{n}cos\frac{2\pi nk}{N}+ib_{n}sin\frac{2\pi nk}{N} \right )$

Algorithm 2.2 Inverse discrete Fourier transform