使用python计算FWHM [英] FWHM calculation using python
问题描述
我正在尝试使用 python 计算光谱的 FWHM.光谱描述(我说的是物理学)对我来说有点复杂,我无法使用一些简单的高斯或洛伦兹分布来拟合数据.
I am trying to calculate the FWHM of spectra using python. The spectral description (I'm talking in terms of the physics) for me it's bit complicated and I can't fit the data using some simple Gaussian or Lorentizian profile.
到目前为止,我设法管理数据的插值并通过半最大值绘制一条平行于 X 轴的直线.
So far I managed to manage interpolation of the data and draw a straight line parallel to the X axis through the half maxima.
我怎样才能找到峰两侧的两条线的交点坐标?
How can I find the coordinates of the intersection of the two lines on both sides of the peak?
我知道如果我将光标放在那些点上,它会给我坐标,但我想自动化这个过程,以便它变得更加用户友好.我该怎么做?
I know if I take the cursor in those points it will give me the coordinates but I want to automate this process so that it becomes much more user friendly. How can I do that?
推荐答案
from matplotlib import pyplot as mp
import numpy as np
def peak(x, c):
return np.exp(-np.power(x - c, 2) / 16.0)
def lin_interp(x, y, i, half):
return x[i] + (x[i+1] - x[i]) * ((half - y[i]) / (y[i+1] - y[i]))
def half_max_x(x, y):
half = max(y)/2.0
signs = np.sign(np.add(y, -half))
zero_crossings = (signs[0:-2] != signs[1:-1])
zero_crossings_i = np.where(zero_crossings)[0]
return [lin_interp(x, y, zero_crossings_i[0], half),
lin_interp(x, y, zero_crossings_i[1], half)]
# make some fake data
x=np.linspace(0,20,21)
y=peak(x,10)
# find the two crossing points
hmx = half_max_x(x,y)
# print the answer
fwhm = hmx[1] - hmx[0]
print("FWHM:{:.3f}".format(fwhm))
# a convincing plot
half = max(y)/2.0
mp.plot(x,y)
mp.plot(hmx, [half, half])
mp.show()
两点的(x, y)
坐标为(hmx[0], half)
和(hmx[1], half)代码>.
这篇关于使用python计算FWHM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!