cyberfue
cyberfue

Reputation: 18

Convolution of 3D array with 1D kernel in Python

Is it possible to convolve 1D array with 3D array? For example:

A is 8x2x2 matrix that I would like to convolve. Assume A has 2x2 sub-matrices of (A = A7 A6 A5... A0) each sub-matrix is 2x2. B is 5x1 array which contains the scalar weights (B0 B1 B2 B3 B4). What I am trying to do is to convolve B array with the first dimension of A array, which is 8 in this case. I know numpy.convolve is available but it doesn't support multidimensions. To clarify my example:Convolution example

Upvotes: 0

Views: 710

Answers (1)

Amit Vikram Singh
Amit Vikram Singh

Reputation: 2128

Use:

arr_out = np.apply_along_axis(
                lambda x: np.convolve(x, B.flatten(), mode = 'same'), 
                axis = 0, arr = A)

Upvotes: 1

Related Questions