How to properly normalize convolution of Gaussian and Lorentzian

Click For Summary
SUMMARY

This discussion focuses on the proper normalization of the convolution of a Gaussian and a Lorentzian function using Python's NumPy and Matplotlib libraries. The user encountered an issue where the integral of the convolution, calculated with np.trapz(), returned approximately 0.2 instead of the expected value of 1. The conversation highlights the necessity of incorporating normalization factors based on the full width at half maximum (fwhm) when changing parameters, suggesting that the distributions should be divided by their respective normalization factors during convolution.

PREREQUISITES
  • Understanding of convolution in signal processing
  • Familiarity with Gaussian and Lorentzian functions
  • Proficiency in Python programming, specifically with NumPy and Matplotlib
  • Knowledge of numerical integration techniques, particularly np.trapz()
NEXT STEPS
  • Implement normalization factors for both the Gaussian and Lorentzian functions in the convolution process
  • Explore the impact of varying fwhm on the convolution results
  • Learn about advanced plotting techniques in Matplotlib for better visualization of convolution results
  • Investigate other numerical integration methods for validating the results of np.trapz()
USEFUL FOR

Researchers, data scientists, and engineers working with signal processing, particularly those interested in the convolution of probability distributions and normalization techniques in Python.

schniefen
Messages
177
Reaction score
4
TL;DR
The title sums it up and this is something I'd like to plot. I'd also like for the Gaussian to be centered at ##x=0## whereas the Lorentzian should be centered at ##x=1##.
I'd like to plot the normalized convolution of a Gaussian with a Lorentzian (see the definitions in terms of full width half maximum (fwhm) in the attached image). Here is my attempt, but the print statements with np.trapz() do not return 1 in both cases, but rather ##\approx##0.2. I'd also like to change the fwhm and plot different convolutions. Is there a normalization factor missing when the parameter fwhm is changed?

Python:
import numpy as np
import matplotlib.pyplot as plt

#lorentzian
def loren(x,x0,fwhm):
    a=fwhm/(2*np.pi)
    y=a*1/((x-x0)**2+(fwhm/2)**2)
    return y
# gaussian
def gaussian(x, x0, fwhm):
    a = 2. / fwhm / np.sqrt(np.pi / np.log(2))
    y = a * np.exp(-4 * np.log(2) * (x-x0)**2 / fwhm**2)
    return y
# define x-axis
x = np.linspace(-5, 5, 1000)
dx = x[1] - x[0]
# the 'main distribution' centered at x0, 'broad distrbution' at x1
x0 = 1
x1=0
dist_main = loren(x, x0, 0.05)
dist_broad = gaussian(x, x1, 0.005)
# calculate the convolution, multiplication with dx is for normalization
dist_conv = np.convolve(dist_main, dist_broad * dx, mode='same')
# plotting
fig, ax = plt.subplots()
ax.plot(x, dist_main, label='Lorentzian')
ax.plot(x, dist_broad, label='Gaussian')
ax.plot(x, dist_conv, label='Convolution')
ax.set_title('$\Gamma_G=0.005$')
ax.legend()
plt.show()

print(np.trapz(dist_broad,x,dx))
print(np.trapz(dist_conv,x,dx))

Screen Shot 2021-03-04 at 12.28.54.png

plotconv.png
 
Last edited:
Technology news on Phys.org
Since np.trapz() returns the normalization factor, maybe divide the dist_main and dist_broad within np.convolve() by the respective factors?
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K