Scipy correlation function slow

I compared different convolution / correlation methods of two signals using numpy / scipy. It turns out that there are huge differences in speed. I compared the following methods:

  • correlate from numpy package (np.correlate in plot)
  • match from scipy.signal package (sps.correlate in the plot)
  • fftconvolve from scipy.signal (sps.fftconvolve in the plot)

Now, of course, I understand that there is a significant difference between fftconvolve and two other functions. I do not understand why sps.correlate is much slower than np.correlate. Does anyone know why Scipi uses an implementation that is much slower?

Speed ​​comparison

For completeness, here is the code that creates the plot:

import time import numpy as np import scipy.signal as sps from matplotlib import pyplot as plt if __name__ == '__main__': a = 10**(np.arange(10)/2) print(a) results = {} results['np.correlate'] = np.zeros(len(a)) results['sps.correlate'] = np.zeros(len(a)) results['sps.fftconvolve'] = np.zeros(len(a)) ii = 0 for length in a: sig = np.random.rand(length) t0 = time.clock() for jj in range(3): np.correlate(sig, sig, 'full') t1 = time.clock() elapsed = (t1-t0)/3 results['np.correlate'][ii] = elapsed t0 = time.clock() for jj in range(3): sps.correlate(sig, sig, 'full') t1 = time.clock() elapsed = (t1-t0)/3 results['sps.correlate'][ii] = elapsed t0 = time.clock() for jj in range(3): sps.fftconvolve(sig, sig, 'full') t1 = time.clock() elapsed = (t1-t0)/3 results['sps.fftconvolve'][ii] = elapsed ii += 1 ax = plt.figure() plt.loglog(a, results['np.correlate'], label='np.correlate') plt.loglog(a, results['sps.correlate'], label='sps.correlate') plt.loglog(a, results['sps.fftconvolve'], label='sps.fftconvolve') plt.xlabel('Signal length') plt.ylabel('Elapsed time in seconds') plt.legend() plt.grid() plt.show() 
+8
source share
1 answer

According to the documentation, numpy.correlate was designed for 1D arrays, and scipy.correlate can accept ND arrays.

A transparent implementation, which is more general and therefore complex, apparently carries additional computational overhead. You can compare C code between numpy and scipy implementations.

Another difference may be, for example, that the numpy implementation becomes better than the vectorized compiler on modern processors, etc.

+3
source

Source: https://habr.com/ru/post/988447/


All Articles