# HowTo:Fourier

### From Predictive Chemistry

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.

## Contents |

# Analogies

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 -- so that 1/261.6 second goes in a complete cycle, from 0 to 2π 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 nm/s / 589.29 nm 510 THz.

# Definition

The Fourier transform of *f*(*x*) is defined as

You can immediately see why the Fourier transform of a pure wave, *f*(*x*) = *e*^{2πisx}, is centered on *k* = *s*. All the other test frequencies, *k*, create oscillatory integrands,

*e*^{2πi(s − k)x}

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

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

which is a sum over all the pure frequencies,

G(k) = | ∑ | a_{j}δ(k − k_{j}) |

j |

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

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 , so

and

Common variations are *a* = − 1 and . 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.

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 *F*_{L}(*k*) at integer values of
-- 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,

Now we have a computational way to represent the Fourier transform -- a set of coefficients, . 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, *x*_{m} / *L* = *m* / *M*. The inverse Fourier transform at these points is then a polynomial,

where ω = *e*^{2πi / M}. Moreover, given the set *f*(*x*_{m}) = *b*_{m}, the inverse
of the inverse is

These last two are the discrete Fourier transform. They are polynomials in the *M*^{th} 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 in real space translates to multiplying by a phase factor in Fourier space? This makes it really easy to compute integrals of the form:

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 *x*_{M} looks at first like it will take *O*(*M*^{2}) 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*,

Since *f* is periodic, we can shift and the sum still runs over , which is the same as .

Because the product takes *M* operations, the Fourier transformations of *f*,*g*,*F**G* are the time-limiting steps in this process. Nevertheless, we've reduced *M*^{2} operations to *M* + 3*M*log *M* operations.
Notice how the starting formula looks a lot like matrix multiplication with *f*_{mj} = *f*(*x*_{m} − *x*_{j})? 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:

- What is the special significance of the zero-frequency component,
**y[0]**? - What is the relationship between the transform at frequencies
*k*and*M*−*k*?- How does this relationship depend on functions that are real vs. contain some imaginary numbers?

- What special property does the transform have if the function is even (
*f*(*x*) =*f*(*L*−*x*))?- Construct an even function numerically and test this out.
- ... if the function is odd (
*f*(*x*) = −*f*(*L*−*x*))?

- 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?

# Properties

The following properties make the discrete Fourier transform useful:

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

Differentiation:
*F*[*f*_{m + 1} − *f*_{m}](*n*) = (ω^{n} − 1)*F*[*f*](*n*)

Representation of Delta:
*F*[δ_{m − j}] = ω^{ − jn}

Parseval: