fast convolution of short 1d vectors (2 lists) in Python

742 Views Asked by At

I would like to implement the fastest possible convolution of two very short vectors (1d) in Python (or in C with a Python interface). The convolution results are reported only for non-zero values of the first vector.

Example:

main_vector = [0,0,0,1,1,1,0,0,0] # usually < 250 elements long
mask = [1,1,1]                    # usually 31 elements long
result = [0,0,0,2,3,2,0,0,0]      # result of convolution

The result is the convolution of the main_vector with the mask, however the result is reported only on non-zero values of the main_vector.

My fastest solution until now:

result = numpy.convolve(main_vector, mask, "same")
result = numpy.multiply(main_vector, result)

Would there be a faster way to implement this in Cython or some other interface? Any ideas very much appreciated. I am using this to do motif searches in bioinformatics, and i perform many of these convolutions.

0

There are 0 best solutions below