matplotlib 求峰的半高全宽

u4vypkhs  于 2023-08-06  发布在  其他
关注(0)|答案(9)|浏览(139)

我一直试图计算出蓝色峰值的半高宽(FWHM)(见图)。绿色峰值和洋红峰值组合构成蓝色峰值。我一直在使用以下公式来计算绿色和洋红峰值的半高宽:其中sd =标准偏差。我创建了绿色和洋红色的峰值,我知道标准差,这就是为什么我可以使用这个方程。
我使用以下代码创建了绿色和洋红色峰值:

def make_norm_dist(self, x, mean, sd):
    import numpy as np

    norm = []
    for i in range(x.size):
        norm += [1.0/(sd*np.sqrt(2*np.pi))*np.exp(-(x[i] - mean)**2/(2*sd**2))]
    return np.array(norm)

字符串
如果我不知道蓝峰是由两个峰组成的,并且我的数据中只有蓝峰,那么我如何找到FWHM?


的数据
我一直在用这段代码来寻找峰顶:

peak_top = 0.0e-1000
for i in x_axis:
    if i > peak_top:
        peak_top = i


我可以将peak_top除以2以找到半高,然后尝试找到对应于半高的y值,但是如果没有x值完全匹配半高,我就会遇到麻烦。
我很确定有一个更优雅的解决方案,我正在尝试的一个。

omtl5h9j

omtl5h9j1#

您可以使用样条曲线拟合[blue curve - peak/2],然后找到它的根:

import numpy as np
from scipy.interpolate import UnivariateSpline

def make_norm_dist(x, mean, sd):
    return 1.0/(sd*np.sqrt(2*np.pi))*np.exp(-(x - mean)**2/(2*sd**2))

x = np.linspace(10, 110, 1000)
green = make_norm_dist(x, 50, 10)
pink = make_norm_dist(x, 60, 10)

blue = green + pink   

# create a spline of x and blue-np.max(blue)/2 
spline = UnivariateSpline(x, blue-np.max(blue)/2, s=0)
r1, r2 = spline.roots() # find the roots

import pylab as pl
pl.plot(x, blue)
pl.axvspan(r1, r2, facecolor='g', alpha=0.5)
pl.show()

字符串
结果如下:
x1c 0d1x的数据

nhaq1z21

nhaq1z212#

这在iPython中对我有效(快速而肮脏,可以减少到3行):

def FWHM(X,Y):
    half_max = max(Y) / 2.
    #find when function crosses line half_max (when sign of diff flips)
    #take the 'derivative' of signum(half_max - Y[])
    d = sign(half_max - array(Y[0:-1])) - sign(half_max - array(Y[1:]))
    #plot(X[0:len(d)],d) #if you are interested
    #find the left and right most indexes
    left_idx = find(d > 0)[0]
    right_idx = find(d < 0)[-1]
    return X[right_idx] - X[left_idx] #return the difference (full width)

字符串
可以进行一些添加以使分辨率更准确,但是在X轴沿着有许多样本并且数据没有太多噪声的限制下,这很有效。
即使当数据不是高斯的并且有点嘈杂时,它也对我有效(我只是取第一次和最后一次半最大值穿过数据)。

ltskdhd1

ltskdhd13#

如果您的数据有噪声(在真实的世界中总是如此),则更可靠的解决方案是将高斯拟合到数据并从中提取FWHM:

import numpy as np
import scipy.optimize as opt

def gauss(x, p): # p[0]==mean, p[1]==stdev
    return 1.0/(p[1]*np.sqrt(2*np.pi))*np.exp(-(x-p[0])**2/(2*p[1]**2))

# Create some sample data
known_param = np.array([2.0, .7])
xmin,xmax = -1.0, 5.0
N = 1000
X = np.linspace(xmin,xmax,N)
Y = gauss(X, known_param)

# Add some noise
Y += .10*np.random.random(N)

# Renormalize to a proper PDF
Y /= ((xmax-xmin)/N)*Y.sum()

# Fit a guassian
p0 = [0,1] # Inital guess is a normal distribution
errfunc = lambda p, x, y: gauss(x, p) - y # Distance to the target function
p1, success = opt.leastsq(errfunc, p0[:], args=(X, Y))

fit_mu, fit_stdev = p1

FWHM = 2*np.sqrt(2*np.log(2))*fit_stdev
print "FWHM", FWHM

字符串


的数据
可通过以下方式生成打印图像:

from pylab import *
plot(X,Y)
plot(X, gauss(X,p1),lw=3,alpha=.5, color='r')
axvspan(fit_mu-FWHM/2, fit_mu+FWHM/2, facecolor='g', alpha=0.5)
show()


更好的近似方法是在拟合之前滤除低于给定阈值的噪声数据。

lc8prwob

lc8prwob4#

这里有一个很好的小函数使用样条方法。

from scipy.interpolate import splrep, sproot, splev

class MultiplePeaks(Exception): pass
class NoPeaksFound(Exception): pass

def fwhm(x, y, k=10):
    """
    Determine full-with-half-maximum of a peaked set of points, x and y.

    Assumes that there is only one peak present in the datasset.  The function
    uses a spline interpolation of order k.
    """

    half_max = amax(y)/2.0
    s = splrep(x, y - half_max, k=k)
    roots = sproot(s)

    if len(roots) > 2:
        raise MultiplePeaks("The dataset appears to have multiple peaks, and "
                "thus the FWHM can't be determined.")
    elif len(roots) < 2:
        raise NoPeaksFound("No proper peaks were found in the data set; likely "
                "the dataset is flat (e.g. all zeros).")
    else:
        return abs(roots[1] - roots[0])

字符串

kxxlusnw

kxxlusnw5#

你应该使用scipy来解决它:先find_peaks,然后peak_widths。默认值为 rel_height(0.5),您测量的是峰的半峰宽。

ryhaxcpt

ryhaxcpt6#

如果您更喜欢插值而不是拟合:

import numpy as np

def get_full_width(x: np.ndarray, y: np.ndarray, height: float = 0.5) -> float:
    height_half_max = np.max(y) * height
    index_max = np.argmax(y)
    x_low = np.interp(height_half_max, y[:index_max+1], x[:index_max+1])
    x_high = np.interp(height_half_max, np.flip(y[index_max:]), np.flip(x[index_max:]))

    return x_high - x_low

字符串

nhjlsmyf

nhjlsmyf7#

对于具有许多数据点的单调函数,如果不需要完美的精度,我会用途:

def FWHM(X, Y):
    deltax = x[1] - x[0]
    half_max = max(Y) / 2.
    l = np.where(y > half_max, 1, 0)

    return np.sum(l) * deltax

字符串

6ovsh4lw

6ovsh4lw8#

我在haggis.math.full_width_half_max中实现了一个经验解决方案,它可以很好地处理噪声和非高斯数据。用法非常简单:

fwhm = full_width_half_max(x, y)

字符串
该函数具有鲁棒性:它使用所请求的内插方案简单地找到数据的最大值和与“向下一半”阈值交叉的最近点。
这里有几个例子使用其他答案的数据。

@HYRY的平滑数据

def make_norm_dist(x, mean, sd):
    return 1.0/(sd*np.sqrt(2*np.pi))*np.exp(-(x - mean)**2/(2*sd**2))

x = np.linspace(10, 110, 1000)
green = make_norm_dist(x, 50, 10)
pink = make_norm_dist(x, 60, 10)

blue = green + pink   

# create a spline of x and blue-np.max(blue)/2 
spline = UnivariateSpline(x, blue-np.max(blue)/2, s=0)
r1, r2 = spline.roots() # find the roots

# Compute using my function
fwhm, (x1, y1), (x2, y2) = full_width_half_max(x, blue, return_points=True)

# Print comparison
print('HYRY:', r2 - r1, 'MP:', fwhm)

plt.plot(x, blue)
plt.axvspan(r1, r2, facecolor='g', alpha=0.5)
plt.plot(x1, y1, 'r.')
plt.plot(x2, y2, 'r.')


对于平滑数据,结果非常精确:

HYRY: 26.891157007233254 MP: 26.891193606203814


x1c 0d1x的数据

@Hooked's Noisy Data

def gauss(x, p): # p[0]==mean, p[1]==stdev
    return 1.0/(p[1]*np.sqrt(2*np.pi))*np.exp(-(x-p[0])**2/(2*p[1]**2))

# Create some sample data
known_param = np.array([2.0, .7])
xmin,xmax = -1.0, 5.0
N = 1000
X = np.linspace(xmin,xmax,N)
Y = gauss(X, known_param)

# Add some noise
Y += .10*np.random.random(N)

# Renormalize to a proper PDF
Y /= ((xmax-xmin)/N)*Y.sum()

# Fit a guassian
p0 = [0,1] # Inital guess is a normal distribution
errfunc = lambda p, x, y: gauss(x, p) - y # Distance to the target function
p1, success = opt.leastsq(errfunc, p0[:], args=(X, Y))

fit_mu, fit_stdev = p1

FWHM = 2*np.sqrt(2*np.log(2))*fit_stdev

# Compute using my function
fwhm, (x1, y1), (x2, y2) = full_width_half_max(X, Y, return_points=True)

# Print comparison
print('Hooked:', FWHM, 'MP:', fwhm)

plt.plot(X, Y)
plt.plot(X, gauss(X, p1), lw=3, alpha=.5, color='r')
plt.axvspan(fit_mu - FWHM / 2, fit_mu + FWHM / 2, facecolor='g', alpha=0.5)
plt.plot(x1, y1, 'r.')
plt.plot(x2, y2, 'r.')


对于噪声数据(具有有偏基线),结果并不一致。

Hooked: 1.9903193212254346 MP: 1.5039676990530118


一方面,高斯拟合对于数据不是非常优的,但另一方面,拾取与半最大阈值相交的最近点的策略可能也不是最优的。


pbossiut

pbossiut9#

一个有效的python实现是values是一个列表:

def calculate_FWHM(values):

    # Find the maximum value and its index
    max_value = max(values)
    max_index = values.index(max_value)

    # Find the value that is half the maximum
    half_max = max_value / 2

    # Find the indices where the values are closest to half the maximum on both sides of the peak
    left_index =  next((i for i in range(max_index, -1, -1) if values[i] <= half_max), 0)
    right_index = next((i for i in range(max_index, len(values)) if values[i] <= half_max), len(values) - 1)

    # return the FWHM
    return right_index - left_index

字符串

相关问题