computer science, math, programming and other stuff
a blog by Christopher Swenson

Python (F)FTW!

Some of the work I have been doing involves taking the Fast Fourier Transform of a set of samples. I originally thought that this needed to be done in C, using FFTW. However, it leads to messes like

fftw_complex *in, *out;
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
fftw_plan p;
p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);

/* read in samples */

fftw_execute(p);

/* do stuff */

fftw_destroy_plan(p);
fftw_free(in);
fftw_free(out);

In Python with numpy/scipy installed, this is so much easier.

from numpy import *

# read in samples

fftout = fft(samples)

And that's it! I should have been using nothing but Python for nearly everything from the start, and screw C.