I was running a simple experiment when I noticed a difference between R's and Python's FFT.
First, Python:
import numpy as np
from pyfftw.interfaces.numpy_fft import fft
a = np.array([1, 2, 3])
fft(a)
>> array([ 6. +0.j , -1.5+0.8660254j, -1.5-0.8660254j])
b = np.array([[1, 2, 3], [4, 5, 6]])
fft(b)
>> array([[ 6. +0.j , -1.5+0.8660254j, -1.5-0.8660254j],
[15. +0.j , -1.5+0.8660254j, -1.5-0.8660254j]])```
Now R:
> a = c(1, 2, 3)
> fft(a)
[1] 6.0+0.000000i -1.5+0.866025i -1.5-0.866025i
> b = rbind(c(1, 2, 3), c(4, 5, 6))
> fft(b)
[,1] [,2] [,3]
[1,] 21+0i -3+1.732051i -3-1.732051i
[2,] -9+0i 0+0.000000i 0+0.000000i
I notice that the first row of the R result corresponds to the element-wise sum of the first and second row of the Python result, whereas the second row of the R result corresponds to the subtraction.
What am I doing wrong? I run the same experiment using np.matrix and R matrix, but got the same results. Which one should be the correct result when applying the FFT to a matrix or multidimensional array?
Following the suggestion in the comments, I did the following:
from pyfftw.interfaces.numpy_fft import fftn
b = np.array([[1, 2, 3], [4, 5, 6]])
fftn(b)
>> array([[21.+0.j , -3.+1.73205081j, -3.-1.73205081j],
[-9.+0.j , 0.+0.j , 0.+0.j ]])
which works with fft2
too.
Indeed, Python FFT is 1D (over each row) unless fft2
or fftn
are used.