Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SciPy convolve vs fftconvolve

I know generally speaking FFT and multiplication is usually faster than direct convolve operation, when the array is relatively large. However, I'm convolving a very long signal (say 10 million points) with a very short response (say 1 thousand points). In this case the fftconvolve doesn't seem to make much sense, since it forces a FFT of the second array to the same size of the first array. Is it faster to just do direct convolve in this case?

like image 746
LWZ Avatar asked Feb 22 '13 06:02

LWZ


People also ask

What is convolve in Scipy?

7 months ago. by Kalsoom Bibi. The basic concept of convolving is to combine two signals using some mathematical function to make the third signal. It is the most commonly used digital signal processing technique.

What is Fftconvolve?

fftconvolve(in1, in2, mode='full', axes=None)[source] Convolve two N-dimensional arrays using FFT. Convolve in1 and in2 using the fast Fourier transform method, with the output size determined by the mode argument.

What is convolve Python?

convolve() is a built-in numpy library method used to return discrete, linear convolution of two one-dimensional vectors. The numpy convolve() method accepts three arguments which are v1, v2, and mode, and returns discrete the linear convolution of v1 and v2 one-dimensional vectors.


2 Answers

Take a look at the comparison I did here:

http://scipy-cookbook.readthedocs.io/items/ApplyFIRFilter.html

Your case might be near the transition between using a plain convolution and using the FFT-based convolution, so your best bet (as suggested by @Dougal in a comment) is to time it yourself.

(Note that I didn't do overlap-add or overlap-save in that comparison.)

like image 110
Warren Weckesser Avatar answered Sep 19 '22 13:09

Warren Weckesser


thank you for your help. Now I did the test myself, I did convolution with 2 arrays, size of 2^20 and 2^4, and this is the result:

numpy.convolve: 110 ms
scipy.signal.convolve: 1.0 s
scipy.signal.fftconvolve: 2.5 s

So we have a winner, numpy convolve is is much faster than the others. I still don't know why though.


Now I tried 2 longer arrays, size of 2^22 and 2^10. The result is:

numpy.convolve: 6.7 s
scipy.signal.convolve: 221 s
scipy.signal.fftconvolve: MemoryError

The difference just gets bigger.

like image 34
LWZ Avatar answered Sep 20 '22 13:09

LWZ