What is the difference between numpy.fft.fft and numpy.fft.rfft?

MichaelAndroidNewbie picture MichaelAndroidNewbie · Sep 18, 2018 · Viewed 12.6k times · Source

The documentation says that np.fft.fft does this:

Compute the one-dimensional discrete Fourier Transform.

and np.fft.rfft does this:

Compute the one-dimensional discrete Fourier Transform for real input.

I also see that for my data (audio data, real valued), np.fft.fft returns a 2 dimensional array of shape (number_of_frames, fft_length) containing complex numbers.

For np.fft.rfft returns a 2 dimensional array of shape (number_of_frames, ((fft_length/2) + 1)) containing complex numbers. I am led to believe that this only contains nonredundant FFT bins.

Can someone explain in more depth the difference between the commands and why the shape of the returned array is different. Thank you.

Answer

B. M. picture B. M. · Sep 18, 2018

the reason is explained in the docs:

When the DFT is computed for purely real input, the output is Hermitian-symmetric, i.e. the negative frequency terms are just the complex conjugates of the corresponding positive-frequency terms, and the negative-frequency terms are therefore redundant. This function does not compute the negative frequency terms, and the length of the transformed axis of the output is therefore n//2 + 1.

As a consequence, the algorithm is optimized and rfft is twice as fast. Furthermore, the spectrum is easier to plot :

In [124]: s=abs(sin(arange(0,2**13,3)))

In [125]: sp=rfft(s)

In [126]: plot(abs(sp))

enter image description here