Python How to properly normalize convolution of Gaussian and Lorentzian

AI Thread Summary
The discussion focuses on plotting the normalized convolution of a Gaussian function with a Lorentzian function, specifically addressing issues with normalization when changing the full width at half maximum (fwhm) parameters. The user attempts to implement this using Python's NumPy and Matplotlib libraries but finds that the output of the np.trapz() function does not yield a normalized result of 1, instead returning approximately 0.2. The user questions whether a normalization factor is missing when adjusting the fwhm values. Suggestions include dividing the Lorentzian and Gaussian distributions by their respective normalization factors before performing the convolution. The conversation emphasizes the importance of proper normalization in convolution operations to achieve accurate results.
schniefen
Messages
177
Reaction score
4
TL;DR Summary
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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
hi; i purchased 3 of these, AZDelivery 3 x AZ-MEGA2560-Board Bundle with Prototype Shield and each is reporting the error message below. I have triple checked every aspect of the set up and all seems in order, cable devices port, board reburn bootloader et al . I have substituted an arduino uno and it works fine; could you help please Thanks Martyn 'avrdude: ser_open(): can't set com-state for "\\.\COM3"avrdude: ser_drain(): read error: The handle is invalid.avrdude: ser_send(): write...

Similar threads

Replies
9
Views
5K
Replies
15
Views
2K
Replies
16
Views
2K
Replies
4
Views
5K
Replies
6
Views
2K
Replies
1
Views
4K
Replies
4
Views
5K
Replies
1
Views
1K
Back
Top