1

The Fast Fourier Transform (fft; documentation) transforms 'a' into its fourier, spectral equivalent:

numpy.fft.fft(a, n=None, axis=-1, norm=None) 

The parameter, n represents—so far as I understand it—how many samples are in the output, where the output is either cropped if n is smaller than the number of samples in a, or padded with zeros if n is larger.

What does axis do? What does it mean exactly? I haven't been able to find any clear examples of its use.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
MBurianyk
  • 11
  • 1
  • 3
  • 1
    Does this answer your question? [In Python NumPy what is a dimension and axis?](https://stackoverflow.com/questions/19389910/in-python-numpy-what-is-a-dimension-and-axis) – Péter Leéh May 14 '20 at 11:51
  • I've added a link to the documentation, alongside some formatting. I'm assuming you've already read the documentation, but it'd be useful to update your question to discuss why the documentation doesn't clarify the parameter's use. – Jeremy Caney May 14 '20 at 19:56

2 Answers2

4

np.fft.fft computes the one-dimensional discrete Fourier transform. If you give a one dimensional input (a vector), it will just compute the transform for that input. However, if your input has more than one dimension, like a 2D matrix, or higher, NumPy assumes you are giving many vectors and you want to compute the transform of each of them. The axis parameter indicates the dimension corresponding to those vectors, and by default it is the last one (-1). So, for example, for a 2D matrix m, if axis=0 then each column m[:, 0], m[:, 1], etc. would be the vectors for which the transform is computed, while passing axis=1 (equivalent to the default axis=-1), each row m[0, :], m[1, :], etc. would be considered a vector for the transform. If you want to compute the transform of all values in the input, regardless of the dimensions, you would have to flatten the input, for example with np.ravel.

Btw, this is a very common convention in NumPy (and many other algebra packages), where a one-dimensional operation can work on multidimensional inputs by receiving an axis parameter that indicates the dimension over which the operation is performed.

jdehesa
  • 58,456
  • 7
  • 77
  • 121
2

numpy.fft.fft() returnes a one-dimensional fourier-transform of your array. That means if you have an array of shape (N,M) it will not give you a two-dimensional fft (np.fft.fft2() does) but return the fft along the last axis. If you like to have the fft calculated rather along the columns than the rows you should pass axis=0.

Tinu
  • 2,432
  • 2
  • 8
  • 20