[Numpy-discussion] fast way to convolve a 2d array with 1d filter

Stéfan van der Walt stefan@sun.ac...
Mon Dec 1 23:42:27 CST 2008


Hi Frank


2008/12/2 frank wang <f.yw@hotmail.com>:
> I need to convolve a 1d filter with 8 coefficients with a 2d array of the
> shape (6,7). I can use convolve to perform the operation for each row. This
> will involve a for loop with a counter 6. I wonder there is
> an fast way to do this in numpy without using for loop. Does anyone know how
> to do it?

Since 6x7 is quite small, so you can afford this trick:

a) Pad the 6,7 array to 6,14.
b) Flatten the array
c) Perform convolution
d) Unflatten array
e) Take out valid values

Cheers
Stéfan


More information about the Numpy-discussion mailing list