用scipy曲线拟合背景信号

2024-09-24 22:27:11 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我当前问题的玩具模型。我有一个宽高斯形状的背景和一个尖锐的信号峰值,它与背景平均值有点偏离中心。 我想提取尖峰的属性(即宽度、峰值位置等)。以下是一个链接,指向与峰值拟合失败的绘图:

graph of background + offset signal

奇怪的是,对于参数的初始猜测,当使用比分布的实际标准偏差大得多的值时,拟合效果更好……有些地方出错了,但无法确定。如果您能给我一些关于如何将山峰与背景搭配的建议,我将不胜感激

下面是我试过的

#Fake Data
data  = np.random.normal(loc=3.25, scale=0.01, size=15000)
data2 = np.random.normal(loc=3.0, scale=0.3, size=25000)

#Bins
bins  = np.arange(0, 6.1, 0.1)

#Hitogram with its defined bins
data_entries_1, bins = np.histogram(data, bins=bins)
data_entries_2, bins = np.histogram(data2, bins=bins)

#Add two generated histograms - Final y data
data_entries = data_entries_1 + data_entries_2

#Cetner of each bins - Final x data
bin_centers = np.array([0.5*(bins[i] + bins[i+1]) for i in range(len(bins)-1)])

#fit func 1
def fit_func1(x, A, mu, sigma):
    #Define functions here
    first_func  = A*np.exp(-1.0*(x - mu)**2 / (2*sigma**2))
    return first_func

#fit func 2
def fit_func2(x, B, mu2, sigma2):
    #Define functions here
    second_func = B*np.exp(-1.0*(x - mu2)**2 / (2*sigma2**2))
    return second_func

#total fit function
def fit_func(x, A, mu, sigma, B, mu2, sigma2):
    #Define functions here
    first_func  = A*np.exp(-1.0*(x - mu)**2 / (2*sigma**2))
    second_func = B*np.exp(-1.0*(x - mu2)**2 / (2*sigma2**2))
    final_func  = first_func + second_func
    return final_func

#Fit it
popt1, pconv1 = curve_fit(fit_func1, xdata=bin_centers, ydata=data_entries_1, p0=[20000, 3.25, 1.])
popt2, pconv2 = curve_fit(fit_func2, xdata=bin_centers, ydata=data_entries_2, p0=[2000, 3.0, 0.3])
popt, pconv   = curve_fit(fit_func, xdata=bin_centers, ydata=data_entries, p0=[20000, 3.25, 1.,\
                                                                           2000, 3.0, 0.3])

# Generate enough x values to make the curves look smooth.
xspace = np.linspace(0, 6, 100)

# Plot the histogram and the fitted function.
plt.step(bin_centers, data_entries, label=r'Histogram entries')
plt.plot(xspace, fit_func1(xspace, *popt1), label='Fitted function1')
plt.plot(xspace, fit_func2(xspace, *popt2), label='Fitted function2')
plt.plot(xspace, fit_func(xspace, *popt), label='Fitted function', linestyle="--")
plt.xlim(1, 5)
plt.legend()
plt.show()

更新 根据所选答案的建议,料仓宽度减小到小于拟拟合实际峰值的西格玛。此外,为了减少拟合自由参数的#,将拟合高斯函数的μ固定为常数(分布的实际平均值)

#Generate Fake Data
data  = np.random.normal(loc=3.25, scale=0.04, size=15000)
data2 = np.random.normal(loc=3.0, scale=0.3, size=25000)

#Bins
bins  = np.arange(0, 6.1, 0.02)

#Compute mean to use as contraints when fitting
mids = np.array([0.5*(bins[i] + bins[i+1]) for i in range(len(bins)-1)])
mean_1 = np.average(mids, weights=data_entries_1)
mean_2 = np.average(mids, weights=data_entries_2)

#fit func 1
def fit_func1(x, A, sigma):
    #Define functions here
    first_func  = A*np.exp(-1.0*(x - mean_1)**2 / (2*sigma**2))
    return first_func

#fit func 2
def fit_func2(x, B, sigma2):
    #Define functions here
    second_func = B*np.exp(-1.0*(x - mean_2)**2 / (2*sigma2**2))
    return second_func

#total fit function
def fit_func(x, A, sigma, B, sigma2):
    #Define functions here
    first_func  = A*np.exp(-1.0*(x - mean_1)**2 / (2*sigma**2))
    second_func = B*np.exp(-1.0*(x - mean_2)**2 / (2*sigma2**2))
    final_func  = first_func + second_func
    return final_func

enter image description here


Tags: datadefnppltmeansigmafitfirst
2条回答

有几个问题

  1. plt.step函数的作用与您认为的不同。它取垃圾箱的边缘,而不是垃圾箱的中心
  2. rarrow峰值为正态分布,σ远小于料仓宽度。本质上,您尝试在单个(x,y)值上拟合一个三参数函数(a,mu,sigma);这注定要失败。这种拟合的结果在我的系统上是不可复制的;根据随机生成器的输出,重新运行相同的代码有时甚至会产生错误

有关这些点的说明,请参见下文

Close-up of multi-gauss fit

def normal(x, sigma):
    a = 0.5/sigma**2
    return np.sqrt(a/np.pi) * np.exp(-a*x**2)
    
def truefunc(x):
    return 1500*normal(x-3.25, 0.01) + 2500*normal(x-3.0, 0.3)

plt.close('all')
xspace = np.linspace(3, 3.5, 200)
plt.plot(bin_centers, data_entries, 'ko', label=r'Histogram entries')
plt.plot(xspace, truefunc(xspace), label='True distribution')
plt.plot(xspace, fit_func1(xspace, *popt1), label='Fitted function1')
plt.plot(xspace, fit_func2(xspace, *popt2), label='Fitted function2')
plt.plot(xspace, fit_func(xspace, *popt), label='Fitted function', linestyle=" ")
plt.xlim(3, 3.5)
plt.legend()
plt.show()

因此,您需要减小存储单元大小,以便实际解析窄峰的宽度,或者重新定义fit_func1以采用两个参数:峰高和峰位置(mu)。将sigma固定到bin_width/6。您必须对拟合结果进行后处理,以使峰值下的面积与直方图一致

如果减小存储单元大小,直方图将有噪声。使用curve_fitsigma参数可能是个好主意;设定为

np.sqrt(data_entries + 0.25)

这在统计上并不完全正确,但比假设所有直方图条目都存在固定误差要好得多。(您可以使用0.25值;它应该是>;0和<;1)

如果您的目标是分析峰值特性,则可以不进行拟合。要获得峰值位置,只需执行以下操作:

peak_pos = bin_centers[data_entries.argmax()]

如果有多个这样的峰值,也可以使用^{}

要获得峰值宽度,可以使用^{}。或者,您可以在分析峰值之前拟合背景并进行减法

相关问题 更多 >