GuyB
GuyB

Reputation: 33

Circular convolution in python

Is there a way with Python to perform circular convolution between two 1D arrays, like with Matlab function cconv? I tried numpy.convolve but it isn't the same, and I can’t find an equivalent.

Upvotes: 1

Views: 6786

Answers (1)

Essex
Essex

Reputation: 6128

You can try this command : scipy.ndimage.filters.convolve1d

You have an option which is named modeand you can write mode = wrap

With that, you get periodic boundary conditions as padding for the convolution

For example :

result = scipy.ndimage.convolve(image,kernel,mode='wrap')

import numpy as np
image = np.array([[0, 0, 0, 0],
                  [0, 0, 0, 1],
                  [0, 0, 0, 0]])
kernel = np.array([[1, 1, 1],
                    [1, 1, 1],
                    [1, 1, 1]])
from scipy.ndimage import convolve
convolve(image, kernel, mode='wrap')
array([[1, 0, 1, 1],   
       [1, 0, 1, 1],
       [1, 0, 1, 1]])

Upvotes: 3

Related Questions