Python: 1d array circular convolution

喜夏-厌秋 提交于 2019-12-06 01:26:41

问题


I wonder if there's a function in numpy/scipy for 1d array circular convolution. The scipy.signal.convolve() function only provides "mode" but not "boundary", while the signal.convolve2d() function needs 2d array as input.

I need to do this to compare open vs circular convolution as part of a time series homework.


回答1:


Since this is for homework, I'm leaving out a few details.

By the definition of convolution, if you append a signal a to itself, then the convolution between aa and b will contain inside the cyclic convolution of a and b.

E.g., consider the following:

import numpy as np
from scipy import signal

%pylab inline

a = np.array([1] * 10)
b = np.array([1] * 10)

plot(signal.convolve(a, b));

That is the standard convolution. Now this, however

plot(signal.convolve(a, np.concatenate((b, b))));

In this last figure, try to see where is the result of the circular convolution, and how to generalize this.




回答2:


By convolution theorem, you can use Fourier Transform to get circular convolution.

import numpy as np
def conv_circ( signal, ker ):
    '''
        signal: real 1D array
        ker: real 1D array
        signal and ker must have same shape
    '''
    return np.real(np.fft.ifft( np.fft.fft(signal)*np.fft.fft(ker) ))


来源:https://stackoverflow.com/questions/35474078/python-1d-array-circular-convolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!