Installation of FFTW3 is very similar to the installation of libsndfile, in that you need to use macports (or homebrew) to install a universal build of the library for use with OpenFrameworks. As discussed earlier, a 'normal' install will most likely build an X86 64-bit library, where we need a 32-bit i386 library build.
Assuming you are using macports, type this into a terminal window:
> sudo port install fftw-3 +universal
Aside - A handy trick for seeing which architecture your library is built for is to use the 'lipo' command. Navigate to the folder where your library lives, and type this into a terminal window:
> lipo -info libfftw3.a
Anyways, the port install command will place the fftw3.h header file in your_HD/opt/local/include folder, and the libfftw3.a library in your_HD/opt/local/lib folder. To use the library in your OF project, add these two files to your project via the menu option Project -> Add to project...
Now, you can include the library in your source by adding the following include statement:
#include "fftw3.h"
The FFTW3 documentation is very thorough, but if you'd like some boiler plate code to see how to calculate a simple FFT, it can be hard to come by. As a warning, copy/pasting example code with this library seems pretty dicey as older versions of the library used considerably different syntax. Thankfully, this guy posted some very nice code for testing the operation of the library. I'm reposting it here after some simple explanation:
int SIZE : This is the size or length of the transform. FFTs are designed to work with transform sizes that are a power of 2. In this case, we just transform a simple signal that is 4 samples long that looks like this:
[1.0, 1.0, 1.0, 1.0]
data: This structure holds the data we want an FFT of. The FFTW library is built to allow for transforms of complex data, but audio data is real-valued only (no imaginary component). As such, we will only fill one 'side' of the structure with our real valued samples (check out the first for loop).
When you run this example, you should see the following output in your xcode console (Run->console) if everything is working with your install:
data[0] = { 1.00, 0.00 }
data[1] = { 1.00, 0.00 }
data[2] = { 1.00, 0.00 }
data[3] = { 1.00, 0.00 }
fft_result[0] = { 4.00, 0.00 }
fft_result[1] = { 0.00, 0.00 }
fft_result[2] = { 0.00, 0.00 }
fft_result[3] = { 0.00, 0.00 }
ifft_result[0] = { 1.00, 0.00 }
ifft_result[1] = { 1.00, 0.00 }
ifft_result[2] = { 1.00, 0.00 }
ifft_result[3] = { 1.00, 0.00 }
You can check the correctness by performing the equivalent commands in MATLAB:
>> data = [1.0, 1.0, 1.0, 1.0];
>> fft(data)
ans =
4 0 0 0
>> ifft(ans)
1 1 1 1
Here's the example C code:
int SIZE = 4;
fftw_complex *data, *fft_result, *ifft_result;
fftw_plan plan_forward, plan_backward;
int i;
data = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SIZE);
fft_result = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SIZE);
ifft_result = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SIZE);
plan_forward = fftw_plan_dft_1d(SIZE, data, fft_result,
FFTW_FORWARD, FFTW_ESTIMATE);
FFTW_FORWARD, FFTW_ESTIMATE);
plan_backward = fftw_plan_dft_1d(SIZE, fft_result, ifft_result,
FFTW_BACKWARD, FFTW_ESTIMATE);
FFTW_BACKWARD, FFTW_ESTIMATE);
for( i = 0 ; i < SIZE ; i++ ) {
data[i][0] = 1.0; // stick your audio samples in here
data[i][1] = 0.0; // use this if your data is complex valued
}
for( i = 0 ; i < SIZE ; i++ ) {
fprintf( stdout, "data[%d] = { %2.2f, %2.2f }\n",
i, data[i][0], data[i][1] );
i, data[i][0], data[i][1] );
}
fftw_execute( plan_forward );
for( i = 0 ; i < SIZE ; i++ ) {
fprintf( stdout, "fft_result[%d] = { %2.2f, %2.2f }\n",
i, fft_result[i][0], fft_result[i][1] );
}
fftw_execute( plan_backward );
for( i = 0 ; i < SIZE ; i++ ) {
fprintf( stdout, "ifft_result[%d] = { %2.2f, %2.2f }\n",
i, ifft_result[i][0] / SIZE, ifft_result[i][1] / SIZE );
}
fftw_destroy_plan( plan_forward );
fftw_destroy_plan( plan_backward );
fftw_free( data );
fftw_free( fft_result );
fftw_free( ifft_result );