1
$\begingroup$

Here is the output of Numpy

np.fft.ifft([0, 4, 0, 0])
array([ 1.+0.j,  0.+1.j, -1.+0.j,  0.-1.j]) # may vary

Here is the output of Matlab

res = fft([0, 4, 0, 0])

res =

   4.0000 + 0.0000i   0.0000 - 4.0000i  -4.0000 + 0.0000i   0.0000 + 4.0000i

ifft(res)

ans =

     0     4     0     0

What is the reason behind different outputs for composition of FFT & iFFT in Numpy and Matlab considering that both of them are used for scientific computation?
How to remedy the problem above in Numpy so I can get the expected result?(Same result as Matlab)

$\endgroup$
9
  • 2
    $\begingroup$ How about if you run np.fft.fft([0,4,0,0]) first, then run np.fft.ifft on the resulting array? Does that match with MATLAB? $\endgroup$ Mar 5, 2021 at 13:55
  • 1
    $\begingroup$ I think you were misinterpreting what np.fft does. np.fft access the fft class -- it does not perform a discreet fourier transform. I think you meant to try np.fft.ifft(np.fft.fft([0, 4, 0, 0])) $\endgroup$
    – Charlie S
    Mar 5, 2021 at 14:20
  • 1
    $\begingroup$ Not sure what the problem is. All the results are the same as matlab. ideone.com/4Emi16 $\endgroup$
    – Charlie S
    Mar 5, 2021 at 18:46
  • 1
    $\begingroup$ See my first comment, I believe you are misunderstanding what np.fft.fft is doing. The first .fft is accessing a set of instructions related to the FFT, including the forward FFT, the inverse FFT, and probably a bunch of other things if you read the documentation. np.fft.fft is only calling the FFT once. $\endgroup$
    – Charlie S
    Mar 5, 2021 at 19:03
  • 1
    $\begingroup$ @FreeMind No problem. It is confusing that the class has the same name as one of the routines. $\endgroup$
    – Charlie S
    Mar 5, 2021 at 19:19

1 Answer 1

1
$\begingroup$

The FFT is in principle a length-preserving operation. For that there has to be a factor 1/N to be distributed between the forward and inverse transform. It is a design decision where that is done or if it is done at all.

It appears that in both cited cases the division by N is performed for the inverse transform. You can interpret the ifft result as polynomial coefficients,

p(z) = 1 + j*z - z^2 - j*z^3

where you get the original vector as the polynomial values at [1,-j,-1,j].

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.