modified on 15 February 2016 at 14:40 ••• 8,771 views


From Predictive Chemistry

Jump to: navigation, search

The Fourier transform is such a useful fundamental tool, it deserves its own howto (as opposed to a simple Code). The Fourier transform is a transformation of a function into another function. The transformation can (in almost all cases) be reversed.



Before getting started, a few analogies are helpful.

Sound comes from acoustic pressure waves -- so you hear middle C when your ear feels a time-varying pressure at precisely 261.6 Hz (1 Hz = 1 cycle per second). The pressure signal would be P(t) \propto cos(2 \pi (261.6 t)) -- so that 1/261.6 second goes in a complete cycle, from 0 to radians. Fourier transforming that signal gives a function that is zero everywhere except for k = 261.6 -- a delta function. The transform of a pure sin or cos wave is always a delta function -- peaked at one particular frequency. If you shift the phase of the sin or cos wave (say by Δθ = 2π(261.6Δt)) this shows up in Fourier space as a phase factor, exp(iΔθ), which does not change the magnitude.

Light comes from electromagnetic waves. In parking lots, you can usually see the grungy yellow/orange color of 589.29 nm light from cheap high-pressure sodium lights. This is because your eye is experiencing a time-varying electric field at a frequency of 3\cdot 10^{17} nm/s / 589.29 nm \simeq 510 THz.


The Fourier transform of f(x) is defined as

 F(k) = \int_{-\infty}^\infty f(x) e^{-2\pi i k x} \; dx

You can immediately see why the Fourier transform of a pure wave, f(x) = eisx, is centered on k = s. All the other test frequencies, k, create oscillatory integrands,


so the integral always cancels, unless s = k, where it shoots to infinity (F(k) = δ(ks)).

The Fourier transform `picks up' all these pure frequencies. You can see this by noting the transform of a bunch of pure waves,

 g(x) = \sum_j a_j e^{2\pi i k_j x}

which is a sum over all the pure frequencies,

G(k) = ajδ(kkj)

The reverse of the Fourier transform is just another Fourier transform. This time, the direction of oscillation has to change, though:

 f(x) = \int_{-\infty}^\infty F(k) e^{2\pi i k x} \; dk

For visually understanding the Fourier transform, it's helpful to plot both sides of a table of transforms. We already showed that the transform of a wave is a delta function, and vice-versa. This means the transform of a constant is a delta function centered at zero. As another example, the transform of a Gaussian function is another Gaussian function.

Other Definitions

Note that there are other definitions of the Fourier transform. These are all related to the above, by scaling k \to \nu/a, so

 F_a(\nu) = \int_{-\infty}^\infty f(x) e^{-\frac{2\pi}{a} i\nu x} \; dx


 f(x) = |a^{-1}| \int_{-\infty}^\infty F_a(\nu) e^{\frac{2\pi}{a} i\nu x} \; d\nu

Common variations are a = − 1 and a = \pm 2\pi. Note that these formulas aren't applicable for imaginary a. If you do that, you'll end up translating the Fourier theory into its equivalent -- analytic continuations of Laplace transforms and their inverse via Bromwich contours.

Discrete Version

The discrete Fourier transform is one limit of the continuous one, where the integral is replaced by the sum. Before introducing this, however, it's helpful to introduce the Fourier transform on a circle.

 F_L(k) = \int_0^L f(x) e^{-2\pi i k x / L} \; dx

The integral runs over 0 to L, at which point the function is considered to be periodic -- i.e. f(0) = f(L). This definition is consistent with the `other definitions' above, for a = L.

Now, because the function is periodic, the test functions should be also. This means we only need to collect the values of FL(k) at integer values of k = n = 0, \pm 1, \ldots -- those for which the test functions are periodic. Conversely, given a finite set of frequency values, we can make good approximations of the function using the inverse Fourier transform,

 f(x) \simeq \sum_{n=-M/2}^{M/2} a_n e^{2\pi i n x / L}

Now we have a computational way to represent the Fourier transform -- a set of coefficients, \{a_n\}_{-M/2}^{M/2}. Each coefficient belongs to a basis function -- called a `plane-wave' basis in physics because of its oscillation as x varies along the direction of travel of the wave.

To complete the picture, let's introduce a set of M equally spaced sampling points, xm / L = m / M. The inverse Fourier transform at these points is then a polynomial,

f(x_m) \simeq \sum_{n=0}^{M-1} a_n \omega^{nm}

where ω = ei / M. Moreover, given the set f(xm) = bm, the inverse of the inverse is

a_n = \frac{1}{M}\sum_{m=0}^{M-1} b_m \omega^{-nm}

These last two are the discrete Fourier transform. They are polynomials in the Mth roots of unity, ω. For one last caveat, note that the scaling by 1 / M is not done at all in the fftw library, whereas it is done on the reverse transform in the numpy library (fft.ifft).

Efficient DFT

There are efficient ways of computing the discrete Fourier transform. They derive from noting that ωn + M / 2 = ωn (for even M).

Wiener–Khinchin Theorem

Remember that shifting a function f(x) \to f(x-y) in real space translates to multiplying by a phase factor in Fourier space? This makes it really easy to compute integrals of the form:

(f*g)(x) = \int_0^L f(x-y) g(y) \; dy

in Fourier space. They are called convolutions, and represent for example, the correlation between f and g at 2 points separated by the distance x, or the probability distribution of the sum of 2 numbers -- one drawn from f and the other from g, or the matrix multiplication between Circulant matrices. The first interpretation leads to the Wiener–Khinchin Theorem, which states that the power spectrum (in Fourier space) is the transform of the autocorrelation function (in real space).

Computing the convolution at points xM looks at first like it will take O(M2) time, since the integral becomes a discrete dot product and we have M of them to compute. For 3D data, this is prohibitively expensive. In Fourier-land, the convolution is a direct product, and takes only M operations!

We'll derive it in the discrete case by Fourier transforming the result, f * g,

FG(n) = \frac{1}{M}\sum_{m=0}^{M-1}\sum_{j=0}^{M-1} f(x_{m-j})g(x_j) \omega^{-nm}

Since f is periodic, we can shift m \to m+j and the sum still runs over m=-j,\ldots,M-j-1, which is the same as 0,\ldots,M-1.

FG(n) = \frac{1}{M}\sum_{m=0}^{M-1}\sum_{j=0}^{M-1} f(x_m)g(x_j) \omega^{-nm}\omega^{-nj}
 = M F(n) G(n)

Because the product takes M operations, the Fourier transformations of f,g,FG are the time-limiting steps in this process. Nevertheless, we've reduced M2 operations to M + 3Mlog M operations. Notice how the starting formula looks a lot like matrix multiplication with fmj = f(xmxj)? This type of matrix multiplication is one of the major computational uses of the Fourier transform.

FFT-ing with Numpy

Finally, some code.

You can verify the particular definition of the DFT used by numpy by, what else, Fourier transforming a delta function:

from numpy import *
x = zeros(16)
x[1] = 1.0
y = fft.fft(x)
print y
print exp(-2j*pi/16.0) # omega
print exp(-2j*pi*arange(16)/16.0)

You should also double-check that the inverse, fft.ifft, works as advertised. Note that this means it must divide by M = 16 here.

Now for some thought-provoking questions:

  1. What is the special significance of the zero-frequency component, y[0]?
  2. What is the relationship between the transform at frequencies k and Mk?
    1. How does this relationship depend on functions that are real vs. contain some imaginary numbers?
  3. What special property does the transform have if the function is even (f(x) = f(Lx))?
    1. Construct an even function numerically and test this out.
    2. ... if the function is odd (f(x) = − f(Lx))?
  4. Why does it make sense to look at the magnitude (y.real**2+y.imag**2) and the phase (arctan2(y.imag,y.real)) of the Fourier transform separately?


The following properties make the discrete Fourier transform useful:

Convolution: F[f * g](n) = F[f](n)F[g](n)

Differentiation: F[fm + 1fm](n) = (ωn − 1)F[f](n)

Representation of Delta: Fmj] = ωjn

Parseval: \sum_{m=0}^{M-1} f(m) g(m) = \frac{1}{M}\sum_{n=0}^{M-1} F[f](n) F[g](n)