Responsive Navbar with Google Search
☰ Menu
Home
Python
LaTeX
GNUPlot
Arduino
Feedback
Contact Us
Numerical Integration: Convolution of two Gaussian Functions
Numerical Integration
Trapezoidal Method
Simpson’s 1/3rd Rule
Gaussian Quadrature method
Convolution of two Gaussian Functions
Convolution of two Gaussian Functions: Program 1
Convolution of Two Gaussian is a Gaussian.
# Convolution of Two Gaussian is a Gaussian. import numpy as np from scipy.integrate import simps import matplotlib.pyplot as plt from scipy.signal import convolve def f(t):return 1.0/(np.sqrt(2*np.pi)*s1)*np.exp(-(t-m1)**2/(2*s1**2)) def g(t):return 1.0/(np.sqrt(2*np.pi)*s2)*np.exp(-(t-m2)**2/(2*s2**2)) m1, s1, m2, s2 = 2, 0.5, -3, 0.9 def fg(t): tau = np.linspace(-100, 100, 1000) return simps(f(tau)*g(t-tau), tau) t, dt = np.linspace(-10, 10, 1000, retstep = True) fgi = np.array([fg(i) for i in t]) fgcon = convolve(f(t), g(t), mode = 'same')*dt plt.plot(t, f(t), '-.', label = 'f(t)') plt.plot(t, g(t), '--', label = 'g(t)') plt.plot(t[::10], fgi[::10], 'o', label = 'By integration') plt.plot(t, fgcon, label = 'By convolve') plt.legend() plt.savefig('plot.png') plt.show()
Run Code
Output 1
Convolution of two Gaussian Functions: Program 2
Convolution of Two Gaussian is a Gaussian check by curve fitting
import numpy as np from scipy.integrate import simps import matplotlib.pyplot as plt from scipy.optimize import curve_fit def f(t, m1, s1):return 1.0/(np.sqrt(2*np.pi)*s1)*np.exp(-(t-m1)**2/(2*s1**2)) def g(t, m2, s2):return 1.0/(np.sqrt(2*np.pi)*s2)*np.exp(-(t-m2)**2/(2*s2**2)) m1, s1, m2, s2 = 2, 0.9, 1, 0.7 def fg(t): tau = np.linspace(-100, 100, 1000) return simps(f(tau, m1, s1)*g(t-tau, m2, s2), tau) t, dt = np.linspace(-10, 10, 1000, retstep = True) fgi = np.array([fg(i) for i in t]) par, var = curve_fit(f, xdata = t, ydata = fgi, p0 = [2, 0.5]) p1, q1 = par[0], par[1] print(p1, q1) plt.plot(t, f(t, m1, s1), '-.', label = 'f(t)') plt.plot(t, g(t, m2, s2), '--', label = 'g(t)') plt.plot(t[::10], fgi[::10], 'o', label = 'convolution of f(t) and g(t)') plt.plot(t, f(t, p1, q1), lw = 2, label = 'Gaussian fit') plt.legend() plt.savefig('plot.png') plt.show()
Run Code
Output 2