scipy.signal.convolve 给出与手动计算卷积积分不同的结果

scipy.signal.convolve gives different result than manual computation of convolution integral

假设我有两个函数,

f(t) = sin(t)

g(t) = cost(t)

我知道fg的卷积是

0.5 * t * sin(t)

(手动解决 here),并通过下面的 sympy 解决:

import sympy as sp

t, tau = sp.symbols('t tau')
sol = sp.integrate(sp.sin(t - tau) * sp.cos(tau), (tau, 0, t))
print(sol)
# t*sin(t)/2

如何使用 scipy 的 convolve 得到这个结果?目前我得到两个非常不同的输出:

import numpy as np
from scipy import signal

t = np.linspace(-10, 10, 1000)
f = np.sin(t)
g = np.cos(t)
conv = 0.5 * t * np.sin(t)

print(conv)
# [-2.72  -2.63  -2.54  -2.449 -2.357 -2.264 -2.171 -2.078 -1.984 -1.89  ...,
#  -1.89  -1.984 -2.078 -2.171 -2.264 -2.357 -2.449 -2.54  -2.63  -2.72 ]

print(signal.convolve(f, g, mode='same'))
# [ 138.098  134.167  130.164  126.092  121.953  117.747  113.476  109.142
#   104.746  100.29  ...,  -95.775 -100.29  -104.746 -109.142 -113.476
#  -117.747 -121.953 -126.092 -130.164 -134.167]

首先,signal.convolve 方法执行求和。要使其近似积分,您需要乘以步长 dt。

其次,产生 0.5*t*sin(t) 的积分仅涉及从 0 开始的值:您将 f(tau)*g(t-tau) 与从 0 到 t 的 tau 积分。要用离散卷积模拟这一点,从 0 开始 linspace,并选择 mode='full',然后将结果截断到大小,从头开始。这样,卷积的第一个元素将涉及很少的项,因此它们基本上从 0 开始。

n = 1000
t = np.linspace(0, 10, n)
dt = t[1] - t[0]
f = np.sin(t)
g = np.cos(t)
conv = 0.5 * t * np.sin(t)
conv2 = signal.convolve(f, g, mode='full')[:n] * dt
plt.plot(t, conv)   # assuming import matplotlib.pyplot as plt
plt.show()
plt.plot(t, conv2)
plt.show()

要得到负数部分,所以同样的事情,但 t = -np.linspace(0, 10, n)。请注意,t 现在应该倒退,仍然从 0 开始——就像积分一样。