使用python进行FWHM计算

问题描述 投票:0回答:1

我正在尝试使用python计算光谱的FWHM。光谱描述(我说的是物理学)对我来说有点复杂,我无法使用一些简单的高斯或Lorentizian曲线来拟合数据。

到目前为止,我设法管理数据的插值并绘制一条平行于X轴的直线通过半最大值。

如何找到峰两侧两条线交点的坐标?

我知道如果我把光标放在那些点上它会给我坐标,但我想自动化这个过程,以便它变得更加用户友好。我怎样才能做到这一点?

enter image description here

python analysis spectral
1个回答
0
投票
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)

© www.soinside.com 2019 - 2024. All rights reserved.