Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 453
MichaelAndroidNewbie Avatar asked Sep 18 '18 13:09

MichaelAndroidNewbie


People also ask

What is the difference between fft and RFFT?

fft library is between different types of input. fft() accepts complex-valued input, and rfft() accepts real-valued input.

What is numpy fft fft?

Numpy fft. fft() is a function that computes the one-dimensional discrete Fourier Transform. The numpy fft. fft() method computes the one-dimensional discrete n-point discrete Fourier Transform (DFT) with the efficient Fast Fourier Transform (FFT) algorithm [CT].

How does numpy Fftfreq work?

Return the Discrete Fourier Transform sample frequencies. The returned float array f contains the frequency bin centers in cycles per unit of the sample spacing (with zero at the start). For instance, if the sample spacing is in seconds, then the frequency unit is cycles/second.

What does fft do in Python?

fft. Compute the one-dimensional discrete Fourier Transform. This function computes the one-dimensional n-point discrete Fourier Transform (DFT) with the efficient Fast Fourier Transform (FFT) algorithm [CT].

How to perform a FFT using NumPy FFT?

The numpy fft.fft () method computes the one-dimensional discrete n-point discrete Fourier Transform (DFT) with the efficient Fast Fourier Transform (FFT) algorithm [CT]. If you have already installed numpy and scipy and want to create a simple FFT of the dataset, you can use the numpy fft.fft () function. Input array can be complex.

What does FFT stand for?

This function computes the one-dimensional n -point discrete Fourier Transform (DFT) with the efficient Fast Fourier Transform (FFT) algorithm [CT]. Input array, can be complex. Length of the transformed axis of the output.

What is the n-dimensional FFT?

The n -dimensional FFT of real input. Frequency bins for given FFT parameters. FFT (Fast Fourier Transform) refers to a way the discrete Fourier Transform (DFT) can be calculated efficiently, by using symmetries in the calculated terms.

What does NP FFT do?

The documentation says that np.fft.fft does this: Compute the one-dimensional discrete Fourier Transform. 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.


2 Answers

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

like image 51
B. M. Avatar answered Oct 14 '22 14:10

B. M.


Basic difference is explained here via example. As it says:

import numpy as np

data = [0, 1, 2, 1, 0]

print("FFT output\n", np.fft.fft(data))
print("RFFT output\n", np.fft.rfft(data))

will result in:

FFT output
 [ 4.        +0.j         -2.11803399-1.53884177j  0.11803399+0.36327126j
  0.11803399-0.36327126j -2.11803399+1.53884177j]
RFFT output
 [ 4.        +0.j         -2.11803399-1.53884177j  0.11803399+0.36327126j]

Notice how the final element of the fft output is the complex conjugate of the second element, for real input. For rfft, this symmetry is exploited to compute only the non-negative frequency terms.

like image 29
Mohammad Zain Abbas Avatar answered Oct 14 '22 13:10

Mohammad Zain Abbas