python - 如何使用 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))
解决方案
推荐阅读
- python-3.x - 如何在 onvif python 中扩展库创建的变量?
- c# - 如何以固定频率运行应用程序?
- dataframe - GtkTreeView 停止更新,除非我改变窗口的焦点
- javascript - JS/jQuery:使用下拉框在另一个字段中设置值,是否正在更改以前的字段?
- javascript - 无法在我的 javascript 文件中显示获取的数据
- javascript - 如何在 PWA 中显示“离线”和“更新”消息?
- redis - 在 Redis 中有没有办法使用 Redis api 删除密钥
- xamarin.forms - 如何在 Xamarin.Forms 中访问自定义控件的 BindingContext
- spring-boot - 默认肯定不使用 Spring Boot 执行测试
- javascript - 我需要创建一个颜色选择器,但我不知道如何将它添加到我的代码中