November 13, 2013

This is the first of a series of posts about implementing the Fast Fourier Transform in Haskell (and trying to make it go fast). This part is going to be pretty pedestrian, but we need to lay some groundwork before the interesting stuff.

Fourier transforms turn up everywhere. They’re important in pure mathematics, but they’re also used in pretty much any application that deals with time series: filtering, signal analysis, numerical solution of differential equations and many others. While the classical theory of Fourier analysis is based on functions, in applications we most often deal with discrete series of values sampled from a function. The discrete Fourier transform is how we do Fourier analysis in this setting.

In this series of posts, we’re not going to look very much at applications (well, maybe we’ll have one little exception a bit later) and we’re not going to talk a lot about the theory behind the Fourier transform. Instead, after briefly introducing the discrete Fourier transform, we’re going to use the fast Fourier transform algorithm and some variations to explore some aspects of Haskell programming. In particular, we’ll look at how Haskell deals with complex numbers, vectors, profiling and benchmarking, meta-programming and finally, as our pièce de résistance, we’ll use all of this to build a Haskell fast Fourier transform package that does compile-time empirical optimisation for arbitrary-sized transforms.

In practice, if you want to do Fourier transforms of discrete data, you use something called FFTW (“the Fastest Fourier Transform in the West”). Our code has no chance of competing with FFTW, which is incredibly clever1, but we can demonstrate some of the methods that they use and learn some techniques along the way that are applicable to more complicated problems. (There are Haskell bindings for FFTW – the vector-fftw package is a good choice.)

The Discrete Fourier Transform

According to the Fourier theorem, any piecewise-continuous complex-valued function of a real variable, h(t)h(t), periodic on the interval [0,T][0, T], can be written as a Fourier series

In data analysis applications, we most commonly don’t have continuous functions, but discretely sampled data. Suppose that we have NN data values from a function h(t)h(t) sampled at a regular interval Δ\Delta:

There are a few things to note here. First, like the classical Fourier transform, the discrete Fourier transform is a linear operation. That will prove to be important! Second, we have NN input values and NN output values, which is as many independent values as we can generate from NN inputs with a linear operation.

If we think of our input data as sampling a continuous function h(t)h(t) at NN discrete times, nΔn\Delta with n=0,1,2,…,N−1n = 0, 1, 2, \dots, N-1, then we can think of the discrete Fourier transform as sampling the “full” Fourier transform at a finite set of frequencies

fn=n/(NΔ)n=−N/2,…,N/2.f_n = n/(N\Delta) \qquad n = -N/2, \dots, N/2.

Because we our sampling at an interval Δ\Delta, we cannot represent any functions containing frequency components above the Nyquist frequency

fc=12Δ,f_c = \frac{1}{2\Delta},

which is just the frequency at the limits of the frequency range given above. (There are N+1N+1 values listed in the range above because the Fourier components for ±fc\pm f_c are identical.)

From the HnH_n, we can recover the original samples hkh_k using the inverse discrete Fourier transform

which is identical to the forward transform except for the sign in the complex exponential and the factor of 1/N1/N. We’ll get a better understanding of this in the next article when we think about the DFT as a matrix multiplying our data vector.

A simple implementation

The transform in (*)(*) is easy to implement directly in Haskell. Here’s a very naïve and inefficient implementation (we’re going to be spending a lot of time thinking about optimisation and efficiency later, so let’s not worry about efficiency right now):

We’re going to be manipulating complex numbers throughout this set of articles, so we import the Data.Complex module, part of the Haskell 2010 standard, to make this easy. The Complex data type is parameterised over the type of the real and imaginary parts of the number and has a constructor :+ to create a complex number from its real and imaginary parts. Data.Complex provides instances of all the expected numeric classes so that you can do arithmetic with complex values just as for other numeric values. Definitions are also provided for transcendental functions (sin\sin, cos\cos, log\log, etc.), and the standard explicitly mandates definitions of these so you can see how they deal with branch cuts and other issues. Finally, Data.Complex provides some convenience functions for working with complex numbers, in particular for converting between Cartesian and polar representations. It also provides the cis function: 𝚌𝚒𝚜θ=cosθ+isinθ\mathtt{cis}\, \theta = \cos \theta + i \sin \theta.

We’re also going to be dealing with vectors of numbers a lot. For that, we use the vector package, which provides arrays indexed by Ints with O(1)O(1) indexing and powerful fusion rules for loop optimisation. It also has vector equivalents of a lot of the standard Prelude list functions (length, take, drop, map, zipWith, etc.). To avoid name clashes, we’ll mostly hide the Prelude versions of these functions, although you can also use a qualified import of Data.Vector if you prefer.

Take a look at the type of dft in the listing above. It both takes and returns a Vector (Complex Double). Throughout this series of posts, we’re only going to be dealing with complex-to-complex DFTs and we’re going to do everything in double precision. In production-quality library code, we would parameterise over the floating point type that we use, and we would also implement the full range of real-to-complex and real-to-real versions of the DFT. The data management issues involved in that aren’t too interesting for us here though, so we’ll stick with complex-to-complex transforms.

The algorithm used above is basically just a direct transcription of the sum in (*)(*), using

Data.Vector.generate ::Int-> (Int-> a) ->Vector a

to calculate vector entries for each integer vector index. It’s very inefficient, not only because it does an O(N2)O(N^2) matrix multiplication (which the FFT aims to fix!), but also because of repeated computation of the complex exponential factors. Anyway, efficiency doesn’t matter here: we just want a simple and clear implementation to compare our more complicated FFT implementations to later on. We use the dft' function as a helper to allow us to implement the forward (dft) and inverse (idft) transforms using the same code.

We can do a couple of quick tests to see if it works. In GHCi (the defuzz function just sets any small components of numbers to zero for display):

First, we look at the DFT of a single “spike”, which corresponds (more or less) to a Dirac δ\delta-function in classical Fourier analysis. The Fourier transform of a δ\delta-function is a constant, and we see that the result of the DFT is constant for our spike, as we would hope. Applying the inverse DFT to the transform gets us back our original data (although as a vector of Complex Double). Next, we look at a sine curve. Recall that

so, for a complex Fourier transform, we would expect to see a negative imaginary component of magnitude 12\frac12 at a positive frequency nn and a positive imaginary component of magnitude 12\frac12 at negative frequency nn. Here, n=1n = 1 (we’re fitting the biggest whole sine curve into our data array that we can). Because of the data layout conventionally used in the DFT, that means that our positive frequency component is at index 1 of the output array, and the negative frequency component is at index 7. The magnitudes of the Fourier components are scaled with respect to those of classical Fourier analysis by a factor of NN, the size of the input array, so we expect our entries to have magnitude 4. This is indeed what we see, except that the signs appear to be wrong – but remember that the sign in the complex exponential in the inverse DFT is the opposite of the forward transform, and it’s the inverse transform that we’d be plugging these values into. So all is well, and the code above appears to work.

What next?

So, that wasn’t very exciting, but it sets the scene for what comes next, which is more fun. In the next article, we’ll examine how the fast Fourier transform algorithm (specifically, the Cooley-Tukey decimation-in-time algorithm) allows us to replace the O(N2)O(N^2) DFT calculation with one that scales as O(NlogN)O(N \log N) for vectors whose lengths are powers of two. We’ll use a clever matrix decomposition to represent the key step in the FFT algorithm, and we’ll write some “toy algebra” code that will enable us to experiment, check some assumptions and generally get a better idea of just what’s going on.