So I'm doing some research which involves analyzing the convolution between two functions. I've been using scipy's built in convolve function, but I'm getting results that differ from when I just manually calculate the convolution integral.
Specifically, I've found that increasing the resolution of the input vectors also increases the output of the convolve function by that same factor. In other words, I have to scale the result back down by a factor of dx.
For example, I wish to convolve these two functions, f(x) and g(x):
After both calculating manually and using scipy's built-in function, I get these curves:
Multiplying the scipy curve by a factor of dx makes the two lie on each other:
Why is this the case?
Below is the code used to make the plots:
import numpy as np
import scipy as sp
from matplotlib import pyplot as plt
def func(x):
return a/(np.pi*w*(1+((x)/w)**2))
def gauss(x,mx,o,I):
return I*np.exp(-(((x-mx)/(o))**2))
def weightSignal(x,mx,o,I):
return gauss(x,mx,o,I)*func(x)
I = 1
o = 0.5
a = 1
w = 1
n = 1001
xi = -1
xf = 1
x = np.linspace(2*xi,2*xf,num=n)
dx = (2*xf-2*xi)/(n-1)
f = func(x)
g = gauss(x,0,o,I)
plt.figure(0)
plt.plot(x,f,label='f(x)')
plt.plot(x,g,label='g(x)')
plt.legend()
fg = sp.signal.convolve(f,g,'same')
fgm = np.zeros((len(x),1))
for i in range(len(fgm)):
mx = x[i]
avg = sp.integrate.quad(weightSignal,-np.inf,np.inf,args=(mx,o,I))
fgm[i] = avg[0]
plt.figure(1)
plt.plot(x,fg,label='Convolution Function')
plt.plot(x,fgm,label='Manual')
plt.xlim([xi, xf])
plt.legend()
plt.figure(2)
plt.plot(x,fg*dx,label='Convolution Function')
plt.plot(x,fgm,label='Manual')
plt.xlim([xi, xf])
plt.ylim([0, 1])
plt.legend()
plt.show()