I'm trying to use Kiss FFT on some 3D matrices, but my results do not match those I get when using Matlab. I was wondering if anyone could help me determine if I'm using the library wrong.
If doing fftn on the following 2x3x2 matrix in Matlab
A2 =
ans(:,:,1) =
1 2 3
4 5 6
ans(:,:,2) =
7 8 9
10 11 12
I get this result
ans(:,:,1) =
78.00000 + 0.0i -6.0 + 3.46410i -6.0 - 3.46410i
-18.0 + 0.0i 0.0 + 0.0i 0.0 + 0.0i
ans(:,:,2) =
-36 0 0
0 0 0
When performing FFT using kiss_fftnd in I get this result (when formatting the result manually). Note the difference in the second row, second column.
78 + 0i -6 + 0i -12 + 6.9282i
0 + 0i -12 -6.9282i 0 + 0i
-36 0 0
0 0 0
I use row major ordering when representing the matrix in C++, thus the input array is the following
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
This is the code I use to perform the FFT with Kiss FFT:
int ni = 2;
int nj = 3;
int nk = 2;
std::vector<double> values = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 };
int *dims = new int[3];
dims[0] = ni;
dims[1] = nj;
dims[2] = nk;
int total = ni * nj * nk;
kiss_fftnd_cfg cfg = kiss_fftnd_alloc(dims, 3, false, NULL, NULL);
kiss_fft_cpx *in = new kiss_fft_cpx[total];
kiss_fft_cpx *out = new kiss_fft_cpx[total];
for (int i = 0; i < total; ++i) {
in[i].r = values[i];
in[i].i = 0.0;
}
kiss_fftnd(cfg, in, out);
The output I get is the following (with added i
to mark complex part):
[78 + 0i, -6 + 0i, -12 + 6.9282i, 0 + 0i, -12 + -6.9282i, 0 + 0i, -36 + 0i, 0 + 0i, 0 + 0i, 0 + 0i, 0 + 0i, 0 + 0i]
Am I doing something wrong when using the library? I would think Kiss FFT uses row major ordering (I have tried with column major ordering, but I still get wrong results compared to Matlab.)