[SciPy-User] [SciPy-user] 2d convolution with 'full' in one dimension and 'valid' in another
Tom K.
tpk at kraussfamily.org
Sat Sep 19 23:04:18 EDT 2009
Paul-405 wrote:
>
> I have a n x m matrix A and n x k matrix B where k >> m. I would like to
> compute
> the 'full' convolution of A and B along 2nd dimension and get a result
> that is n
> x (m + k - 1).
>
> If I select mode='full' in scipy.signal.convolve, I get a result that is
> 'full'
> in both dimensions and a result that is (n + n - 1) x (m + k - 1).
> Currently, I
> do this:
>
> C = np.array([sp.signal.correlate(A[i], B[i], mode='full') for i in
> range(n)])
>
> I do this a lot with fixed A and varying B. I was wondering if there is a
> faster
> way. Perhaps I should be using fft's instead.
>
Paul, What you describe does not sound to me like a 'valid' convolution in
the other dimension - it sounds like no convolution at all. You want a 1D
convolution along only one dimension of 2 ND arrays, not an ND convolution
of 2 ND arrays, right?
One thing that may be faster is pre-allocating the output array and
assigning it in a loop:
def convolve1(x, y):
n1 = x.shape[-1]
n2 = y.shape[-1]
z = np.empty((x.shape[0], n1+n2-1), dtype=x.dtype)
for i in xrange(x.shape[0]):
z[i] = np.convolve(x[i], y[i])
return z
This was about 2.5x faster than the one-liner that you provided for a simple
example (n=1000, m=100, k=1000).
Anyone else have thoughts about fast ways to do 1D convolution along only
one dimension of 2 ND arrays?
--
View this message in context: http://www.nabble.com/2d-convolution-with-%27full%27-in-one-dimension-and-%27valid%27-in-another-tp25500595p25527399.html
Sent from the Scipy-User mailing list archive at Nabble.com.
More information about the SciPy-User
mailing list