Fft numpy and octaves are different on transport

I first know there is an identical question with an answer in SO here: FFT in Matlab and numpy / scipy give different results but the answer I got there doesn't work on the test I did:

when i do fft from numpy.fft i get the following result:

In [30]: numpy.fft.fft(numpy.array([1+0.5j, 3+0j, 2+0j, 8+3j]))
Out[30]: array([ 14.+3.5j,  -4.+5.5j,  -8.-2.5j,   2.-4.5j])

      

which is identical to the output in my octave scale)

octave:39> fft([1+0.5j,3+0j,2+0j,8+3j])
ans =
Columns 1 through 3:
14.0000 +  3.5000i   -4.0000 +  5.5000i   -8.0000 -  2.5000i
Column 4:
2.0000 -  4.5000i

      

but if I traverse the list in octave and python I get:

In [9]: numpy.fft.fft(numpy.array([1+0.5j, 3+0j, 2+0j, 8+3j]).transpose())
Out[9]: array([ 14.+3.5j,  -4.+5.5j,  -8.-2.5j,   2.-4.5j])

      

and for the octave:

octave:40> fft([1+0.5j,3+0j,2+0j,8+3j]')
ans =

14.0000 -  3.5000i
2.0000 +  4.5000i
-8.0000 +  2.5000i
-4.0000 -  5.5000i

      

I also tried changing the form to python, but this results in:

In [33]: numpy.fft.fft(numpy.reshape(numpy.array([1+0.5j,3+0j,2+0j,8+3j]), (4,1)))
Out[33]: 
array([[ 1.+0.5j],
   [ 3.+0.j ],
   [ 2.+0.j ],
   [ 8.+3.j ]])

      

how to get the same result in python as octave? + I don't have a matlab test to check, otherwise I would check if it returns the same as an octave just to be sure.

+3


source to share


1 answer


Why NumPy and Octave gave different results:

The entrances were different. '

octave returns the complex conjugate transpose , not transpose , .'

:

octave:6> [1+0.5j,3+0j,2+0j,8+3j]'
ans =

   1.0000 - 0.5000i
   3.0000 - 0.0000i
   2.0000 - 0.0000i
   8.0000 - 3.0000i

      

So to make the NumPy result match the octave:

In [115]: np.fft.fft(np.array([1+0.5j, 3+0j, 2+0j, 8+3j]).conj()).reshape(-1, 1)
Out[115]: 
array([[ 14.-3.5j],
       [  2.+4.5j],
       [ -8.+2.5j],
       [ -4.-5.5j]])

octave:7> fft([1+0.5j,3+0j,2+0j,8+3j]')
ans =

   14.0000 -  3.5000i
    2.0000 +  4.5000i
   -8.0000 +  2.5000i
   -4.0000 -  5.5000i

      


In NumPy, the transpose of a 1D array is the same 1D array. Therefore it fft(np.array([1+0.5j, 3+0j, 2+0j, 8+3j]).transpose())

returns a 1D array.

Permutation after performing FFT of 1D array:

You can take the FFT first and then change the shape. To make a 2-dimensional 1D array, you can use reshape

to get a columnar array of shape (4,1), or use np.atleast_2d

followed by transpose

:

In [115]: np.fft.fft(np.array([1+0.5j, 3+0j, 2+0j, 8+3j]).conj()).reshape(-1, 1)
Out[115]: 
array([[ 14.-3.5j],
       [  2.+4.5j],
       [ -8.+2.5j],
       [ -4.-5.5j]])

      

or



In [116]: np.atleast_2d(np.fft.fft(np.array([1+0.5j, 3+0j, 2+0j, 8+3j]).conj())).T
Out[116]: 
array([[ 14.-3.5j],
       [  2.+4.5j],
       [ -8.+2.5j],
       [ -4.-5.5j]])

      

Performing FFT of a 2D array:

np.fft.fft

takes FFT on the last axis by default. This is why rebuilding the shape (4, 1) didn't work. Change the array to (1, 4) instead:

In [117]: np.fft.fft(np.reshape(np.array([1+0.5j,3+0j,2+0j,8+3j]), (1,4)).conj()).T
Out[117]: 
array([[ 14.-3.5j],
       [  2.+4.5j],
       [ -8.+2.5j],
       [ -4.-5.5j]])

      

Or you can use np.matrix

to make a 2D matrix of the shape (1, 4). Again the FFT is taken along the last axis, returns an array of the shape (1, 4), which can then be transposed to get the desired result:

In [121]: np.fft.fft(np.matrix([1+0.5j, 3+0j, 2+0j, 8+3j]).conj()).T
Out[121]: 
array([[ 14.-3.5j],
       [  2.+4.5j],
       [ -8.+2.5j],
       [ -4.-5.5j]])

      

This probably gives you the simplest syntax. But be aware that this passes np.matrix

as input but returns np.ndarray

as output.


As Warren Walker pointed out, if you already have a 2D NumPy array and want to take the FFT of its columns, you can pass axis=0

to the np.fft.fft call. In addition, the class matrix

(unlike the ndarray class) has a property H

that returns a complex conjugate transposition. Thus,

In [114]: np.fft.fft(np.matrix([1+0.5j, 3+0j, 2+0j, 8+3j]).H, axis=0)
Out[114]: 
array([[ 14.-3.5j],
       [  2.+4.5j],
       [ -8.+2.5j],
       [ -4.-5.5j]])

      

+5


source







All Articles