简短摘要:如何快速计算两个数组的有限卷积?
我试图获得由
定义的两个函数f(x),g(x)的有限卷积
为实现这一目标,我采用了离散的函数样本,并将它们转换为长度为steps
的数组:
xarray = [x * i / steps for i in range(steps)]
farray = [f(x) for x in xarray]
garray = [g(x) for x in xarray]
然后我尝试使用scipy.signal.convolve
函数计算卷积。此函数提供与算法conv
建议here相同的结果。但是,结果与分析解决方案有很大不同。修改算法conv
以使用梯形规则可得到所需的结果。
为了说明这一点,我让
f(x) = exp(-x)
g(x) = 2 * exp(-2 * x)
结果是:
此处Riemann
表示简单的黎曼和,trapezoidal
是使用梯形法则的黎曼算法的修改版本,scipy.signal.convolve
是scipy函数,analytical
是分析卷积。
现在让g(x) = x^2 * exp(-x)
和结果成为:
此处'比率'是从scipy获得的值与分析值之比。以上表明,通过对积分进行重整化不能解决问题。
是否可以使用scipy的速度但保留更好的梯形规则结果,还是必须编写C扩展来实现所需的结果?
只需复制并粘贴以下代码即可查看我遇到的问题。通过增加steps
变量可以使两个结果更加一致。我认为问题是由于右手黎曼和的假象,因为当积分增加时积分被高估并且随着它逐渐减小而再次接近解析解。
编辑:我现在已将原始算法2作为比较,其结果与scipy.signal.convolve
函数相同。
import numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt
import math
def convolveoriginal(x, y):
'''
The original algorithm from http://www.physics.rutgers.edu/~masud/computing/WPark_recipes_in_python.html.
'''
P, Q, N = len(x), len(y), len(x) + len(y) - 1
z = []
for k in range(N):
t, lower, upper = 0, max(0, k - (Q - 1)), min(P - 1, k)
for i in range(lower, upper + 1):
t = t + x[i] * y[k - i]
z.append(t)
return np.array(z) #Modified to include conversion to numpy array
def convolve(y1, y2, dx = None):
'''
Compute the finite convolution of two signals of equal length.
@param y1: First signal.
@param y2: Second signal.
@param dx: [optional] Integration step width.
@note: Based on the algorithm at http://www.physics.rutgers.edu/~masud/computing/WPark_recipes_in_python.html.
'''
P = len(y1) #Determine the length of the signal
z = [] #Create a list of convolution values
for k in range(P):
t = 0
lower = max(0, k - (P - 1))
upper = min(P - 1, k)
for i in range(lower, upper):
t += (y1[i] * y2[k - i] + y1[i + 1] * y2[k - (i + 1)]) / 2
z.append(t)
z = np.array(z) #Convert to a numpy array
if dx != None: #Is a step width specified?
z *= dx
return z
steps = 50 #Number of integration steps
maxtime = 5 #Maximum time
dt = float(maxtime) / steps #Obtain the width of a time step
time = [dt * i for i in range (steps)] #Create an array of times
exp1 = [math.exp(-t) for t in time] #Create an array of function values
exp2 = [2 * math.exp(-2 * t) for t in time]
#Calculate the analytical expression
analytical = [2 * math.exp(-2 * t) * (-1 + math.exp(t)) for t in time]
#Calculate the trapezoidal convolution
trapezoidal = convolve(exp1, exp2, dt)
#Calculate the scipy convolution
sci = signal.convolve(exp1, exp2, mode = 'full')
#Slice the first half to obtain the causal convolution and multiply by dt
#to account for the step width
sci = sci[0:steps] * dt
#Calculate the convolution using the original Riemann sum algorithm
riemann = convolveoriginal(exp1, exp2)
riemann = riemann[0:steps] * dt
#Plot
plt.plot(time, analytical, label = 'analytical')
plt.plot(time, trapezoidal, 'o', label = 'trapezoidal')
plt.plot(time, riemann, 'o', label = 'Riemann')
plt.plot(time, sci, '.', label = 'scipy.signal.convolve')
plt.legend()
plt.show()
感谢您的时间!
答案 0 :(得分:1)
或者,对于那些喜欢numpy到C的人来说。它会比C实现慢,但它只是几行。
>>> t = np.linspace(0, maxtime-dt, 50)
>>> fx = np.exp(-np.array(t))
>>> gx = 2*np.exp(-2*np.array(t))
>>> analytical = 2 * np.exp(-2 * t) * (-1 + np.exp(t))
在这种情况下,这看起来像梯形(但我没有检查数学)
>>> s2a = signal.convolve(fx[1:], gx, 'full')*dt
>>> s2b = signal.convolve(fx, gx[1:], 'full')*dt
>>> s = (s2a+s2b)/2
>>> s[:10]
array([ 0.17235682, 0.29706872, 0.38433313, 0.44235042, 0.47770012,
0.49564748, 0.50039326, 0.49527721, 0.48294359, 0.46547582])
>>> analytical[:10]
array([ 0. , 0.17221333, 0.29682141, 0.38401317, 0.44198216,
0.47730244, 0.49523485, 0.49997668, 0.49486489, 0.48254154])
最大绝对误差:
>>> np.max(np.abs(s[:len(analytical)-1] - analytical[1:]))
0.00041657780840698155
>>> np.argmax(np.abs(s[:len(analytical)-1] - analytical[1:]))
6
答案 1 :(得分:0)
简短回答:用C写下来!
使用关于numpy arrays的食谱我在C中重写了梯形卷积方法。为了使用C代码,需要三个文件(https://gist.github.com/1626919)
代码应在下载时运行,执行以下操作
performancemodule.c
。运行以下
python performancemodulesetup.py构建 python performancetest.py
您可能需要将库文件performancemodule.so
或performancemodule.dll
复制到与performancetest.py
相同的目录中。
结果如下所示:
C方法的性能甚至比scipy的convolve方法更好。运行阵列长度为50的10k卷积需要
convolve (seconds, microseconds) 81 349969
scipy.signal.convolve (seconds, microseconds) 1 962599
convolve in C (seconds, microseconds) 0 87024
因此,C实现比python实现快 1000次,并且比scipy实现快20倍(诚然,scipy实现更通用)。
编辑:这并不能完全解决原始问题,但足以达到我的目的。