首页 > 解决方案 > 如何使用 np.convolve() 在 Python 中创建 Voigt 函数?

问题描述

我想绘制高斯与柯西洛伦兹分布的归一化卷积,以不同的点为中心。我正在使用以下定义:

这是我的尝试。当 fwhm 减小到 0.005 时,np.trapz() 在两种情况下都不会返回 1,而是大约 0.2。这与 x 轴的分区方式有关吗?我想改变 fwhm 并绘制不同的卷积。我不确定是否dx需要/它做什么以及它是否正确放置。

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))

标签: pythonnumpyconvolution

解决方案


推荐阅读