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. I was not able to do this, however, using numpy.convolve
, as it always return results from minus infinity to plus infinity. 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.
Is there a way to specify the lower and upper limits of numpy.convolve
? Thank you very much.
Here is the code (I apologize for not be able to type LaTeX equation here):
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
Writing H(t) = 1/2 (sign(t) + 1) this is the same as
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)
with 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.
Update
If t and T are the same number it's even slightly simpler:
int{-inf...t} f(tau)g(t-tau) dtau
In this case you can shift the integration variable by t, so you get:
int{-inf...0} f(tau+t}g(-tau) dtau
next you replace tau with -tau
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.
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. It is necessary to go through this post to under the notations and symbols used here.
What np.convolve(A, B)
gives you is
and what you are looking for is
Here the second term will be zero as B
for negative coefficients is extrapolated to zero. So you see you don't have to do anything as
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.