Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improving FFT performance in Python

What is the fastest FFT implementation in Python?

It seems numpy.fft and scipy.fftpack both are based on fftpack, and not FFTW. Is fftpack as fast as FFTW? What about using multithreaded FFT, or using distributed (MPI) FFT?

like image 472
Charles Brunet Avatar asked Jun 15 '11 23:06

Charles Brunet


People also ask

How do you fast Fourier transform in Python?

Use the FFT function to calculate the Fourier transform of the above signal. Plot the amplitude spectrum for both the two-sided and one-side frequencies. TRY IT! Generate a simple signal for length 2048, and time how long it will run the FFT and compare the speed with the DFT.

Is numpy fft multithreaded?

Unlike many of the array operations in numpy , the fft operation is not threaded for execution across multiple processors.


2 Answers

You could certainly wrap whatever FFT implementation that you wanted to test using Cython or other like-minded tools that allow you to access external libraries.

GPU-based

If you're going to test FFT implementations, you might also take a look at GPU-based codes (if you have access to the proper hardware). There are several: reikna.fft, scikits.cuda.

CPU-based

There's also a CPU based python FFTW wrapper pyFFTW.

(There is pyFFTW3 as well, but it is not so actively maintained as pyFFTW, and it does not work with Python3. (source))

I don't have experience with any of these. It's probably going to fall to you to do some digging around and benchmark different codes for your particular application if speed is important to you.

like image 198
JoshAdel Avatar answered Oct 05 '22 06:10

JoshAdel


For a test detailed at https://gist.github.com/fnielsen/99b981b9da34ae3d5035 I find that scipy.fftpack performs fine compared to my simple application of pyfftw via pyfftw.interfaces.scipy_fftpack, except for data with a length corresponding to a prime number.

There seems to be some setup cost associated with evoking pyfftw.interfaces.scipy_fftpack.fft the first time. The second time it is faster. Numpy's and scipy's fftpack with a prime number performs terribly for the size of data I tried. CZT is faster in that case. Some months ago an issue was put up at Scipy's Github about the problem, see https://github.com/scipy/scipy/issues/4288

20000 prime=False   padded_fft : 0.003116    numpy_fft : 0.003502    scipy_fft : 0.001538          czt : 0.035041     fftw_fft : 0.004007 ------------------------------------------------------------ 20011 prime=True   padded_fft : 0.001070    numpy_fft : 1.263672    scipy_fft : 0.875641          czt : 0.033139     fftw_fft : 0.009980 ------------------------------------------------------------ 21803 prime=True   padded_fft : 0.001076    numpy_fft : 1.510341    scipy_fft : 1.043572          czt : 0.035129     fftw_fft : 0.011463 ------------------------------------------------------------ 21804 prime=False   padded_fft : 0.001108    numpy_fft : 0.004672    scipy_fft : 0.001620          czt : 0.033854     fftw_fft : 0.005075 ------------------------------------------------------------ 21997 prime=True   padded_fft : 0.000940    numpy_fft : 1.534876    scipy_fft : 1.058001          czt : 0.034321     fftw_fft : 0.012839 ------------------------------------------------------------ 32768 prime=False   padded_fft : 0.001222    numpy_fft : 0.002410    scipy_fft : 0.000925          czt : 0.039275     fftw_fft : 0.005714 ------------------------------------------------------------ 
like image 22
Finn Årup Nielsen Avatar answered Oct 05 '22 07:10

Finn Årup Nielsen