Andrew Sharpe
Andrew Sharpe

Reputation: 51

2D complex FFT implementation

I'm working in C with Dev-C++

I've created a 2D array of complex numbers as such:

#include<complex.h>

double complex **x;

x = malloc(Nx * sizeof *X);

if (x)
{
for (i = 0; i < Nx; i++) 
{
x[i] = malloc(Nx * sizeof *x[i]);

}   

And filled it with data, which I've plotted with real and imaginary parts which are verified and correct.

I'd simply like to perform an FFT on this data (hopefully with a function taking in just the array, its dimensions and fft direction) which will transform the array and also be able to perform the inverse.

I've looked at libraries such as FFTW but the implementation remains incomprehensible to me despite my efforts to understand.

Can someone please explain the best way for me to do this? Thanks

Upvotes: 0

Views: 900

Answers (2)

Andrew Sharpe
Andrew Sharpe

Reputation: 51

I ended up solving this problem by making separate real and imaginary 2D arrays. Functions were then written to take in the arrays, create double length 1D arrays with alternating real and imaginary values representing the rows and columns. This allowed me to use the simple in-code FFT four1 function (given in Numerical Recipes in C) by performing the transform on every row and column consecutively.

This does the job with no libraries needed!

Just remember to include normalisations after each transform.

Upvotes: 0

Roddey Frost
Roddey Frost

Reputation: 56

Starting from the beginning, you need to get the FFTW library for your system. Depending on what you are running the code on you may have to make it from the source code as shown here:

http://www.fftw.org/fftw3_doc/Installation-and-Customization.html

The configure, make, make install process could take quite a while.

Once this is complete you will need to include the library in your code using

    #include<fftw3.h>

This may change depending on which version you are using. To compile the code, you will need to link the library using -lfftw3.

Now to actually include the FFT code . This can be broken down into about three steps: Plan, fill the input array, and execute.

The planning stage for a 2d complex FFT can be shown here: http://www.fftw.org/fftw3_doc/Complex-Multi_002dDimensional-DFTs.html#Complex-Multi_002dDimensional-DFTs You only need to plan once, unless the dimensions of the FFT change.

The second stage requires that you use the provided FFTW arrays. A helpful link for formatting the arrays is shown in the previous link.

And the third stage is as simple as calling the "fftw_execute" routine. Once this is called the output array will be filled with the output of the FFT.

Upvotes: 2

Related Questions