简体   繁体   English

Python numpy.convolve 积分限制

[英]Python numpy.convolve integral limits

I have a convolution integral that has a similar form described in this post: ( link )我有一个卷积积分,其形式与这篇文章中描述的类似:( 链接

Instead of integrating from 0 to t, I need to integrate from minus infinity to t.我需要从负无穷大积分到 t,而不是从 0 积分到 t。 I was not able to do this, however, using numpy.convolve , as it always return results from minus infinity to plus infinity.但是,我无法使用numpy.convolve执行此操作,因为它总是返回从负无穷大到正无穷大的结果。 Using scipy.integrate.quad would be very slow as I have to loop through every t , and it only works for integrands that have analytical expressions.使用scipy.integrate.quad会非常慢,因为我必须遍历每个t ,并且它仅适用于具有解析表达式的被积函数。

Is there a way to specify the lower and upper limits of numpy.convolve ?有没有办法指定numpy.convolve的下限和上限? Thank you very much.非常感谢。

Here is the code (I apologize for not be able to type LaTeX equation here):这是代码(很抱歉无法在此处输入 LaTeX 等式):

import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate

def gaussian(tau, mu, sigma):

    pdf = 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (tau - mu)**2 / (2 * sigma**2))
    return pdf

def gaussian_deriv(tau, mu, sigma):
    """
    derivative of gaussian function
    """
    pdf = -(tau - mu)/sigma**2 * gaussian(tau, mu, sigma)
    return pdf

def integral_kernel(tau):
    return np.cbrt(1/tau)

def integrand(tau, t, mu, sigma):

    return gaussian_deriv(tau, mu, sigma) * integral_kernel(t - tau + 1E-28)

tau = np.linspace(-7, 7, 1000)
dtau = tau[1] - tau[0]
lower_lim = tau[0]
g_deriv = gaussian_deriv(tau, mu=0, sigma=1)

result = np.zeros_like(tau)
for idx, t in np.ndenumerate(tau):
    result[idx], err = integrate.quad(integrand, lower_lim, t, args=(t, mu, sigma), points=[t])

result_convolve = np.convolve(g_deriv, integral_kernel(tau), mode='same') * dtau

fig, ax = plt.subplots(2,1, figsize=(10, 6))
ax[0].plot(tau, result, 'r-', label='scipy quad')
ax[1].plot(tau, result_convolve, '.', label='numpy convolve')
ax[0].legend()
ax[1].legend()
plt.show()

If I understand you correctly you want the integral 如果我正确理解您的要求,您需要积分

int{-inf...T} f(tau) g(t-tau) dtau int {-inf ... T} f(tau)g(t-tau)dtau

Writing H(t) = 1/2 (sign(t) + 1) this is the same as 写H(t)= 1/2(sign(t)+1)等于

int{-inf...inf} f(tau) g(t-tau) [1 - H(tau-T)] dtau int {-inf ... inf} f(tau)g(t-tau)[1-H(tau-T)] dtau

or 要么

int{-inf...inf} f^(tau) g(t-tau) dtau = f^*g(t) int {-inf ... inf} f ^(tau)g(t-tau)dtau = f ^ * g(t)

with f^(tau) = f(tau) * [1 - H(tau-T)]. 其中f ^(tau)= f(tau)* [1-H(tau-T)]。

So all you need to do is to zero out f right of T to get f^ and then compute the conventional convolution f^*g. 因此,您要做的就是将T的f右清零以获得f ^,然后计算常规卷积f ^ * g。

Update 更新资料

If t and T are the same number it's even slightly simpler: 如果t和T是相同的数字,它甚至会稍微简单一些:

int{-inf...t} f(tau)g(t-tau) dtau int {-inf ... t} f(tau)g(t-tau)dtau

In this case you can shift the integration variable by t, so you get: 在这种情况下,您可以将积分变量移动t,这样您将获得:

int{-inf...0} f(tau+t}g(-tau) dtau int {-inf ... 0} f(tau + t} g(-tau)dtau

next you replace tau with -tau 接下来用-tau替换tau

int{0...inf} g(tau) f(t-tau) dtau int {0 ... inf} g(tau)f(t-tau)dtau

and now you proceed more or less as above only T is now fixed and equal to zero and you don't zero out right of T but left of T. 现在您或多或少地如上所述进行操作,只有T现在固定且等于零,并且您不在T的右边而不是T的左边。

Again this is a rather old post, still I am presenting my analysis here.同样,这是一篇相当古老的帖子,但我仍然在这里展示我的分析。

Please see this post where I have answered the question cited by OP.请参阅这篇文章,我已经回答了 OP 引用的问题。 It is necessary to go through this post to under the notations and symbols used here.有必要通过此帖 go 了解此处使用的符号和符号。

What np.convolve(A, B) gives you is np.convolve(A, B)给你的是

and what you are looking for is你要找的是

Here the second term will be zero as B for negative coefficients is extrapolated to zero.这里第二项将为零,因为负系数的B被外推为零。 So you see you don't have to do anything as所以你看你不必做任何事情

. .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM