拟合高斯是一个好方法。如果您对初始参数值有正确的猜测,您可以尝试一次全部猜测。一个大问题是噪声,实际上您可能希望单独拟合每个峰(即一次只考虑给定峰所在的范围),或者在数据中获取基线噪声曲线并将其减去。
这是一些尝试拟合多个高斯的代码。我为我认为是 8 个最突出的峰输入了一些非常宽松的参数,加上一个额外的非常宽的 Guassian 来尝试捕捉背景噪声。在处理之前,我稍微清理了您发布的图像,以帮助从中获取数据(删除鼠标光标和轴边缘,并反转图像)。
代码:
import Image
from scipy import *
from scipy.optimize import leastsq
import numpy as np
im = Image.open("proc.png")
im = im.convert('L')
h, w = im.size
#Extract data from the processed image:
im = np.asarray(im)
y_vals, junk = np.mgrid[w:0:-1, h:0:-1]
y_vals[im < 255] = 0
y_vals = np.amax(y_vals,axis=0)
def gaussian(x, A, x0, sig):
return A*exp(-(x-x0)**2/(2.0*sig**2))
def fit(p,x):
return np.sum([gaussian(x, p[i*3],p[i*3+1],p[i*3+2])
for i in xrange(len(p)/3)],axis=0)
err = lambda p, x, y: fit(p,x)-y
#params are our intitial guesses for fitting gaussians,
#(Amplitude, x value, sigma):
params = [[50,40,5],[50,110,5],[100,160,5],[100,220,5],
[50,250,5],[100,260,5],[100,320,5], [100,400,5],
[30,300,150]] # this last one is our noise estimate
params = np.asarray(params).flatten()
x = xrange(len(y_vals))
results, value = leastsq(err, params, args=(x, y_vals))
for res in results.reshape(-1,3):
print "amplitude, position, sigma", res
import pylab
pylab.subplot(211, title='original data')
pylab.plot(y_vals)
pylab.subplot(212, title='guassians fit')
y = fit(results, x)
pylab.plot(x, y)
pylab.savefig('fig2.png')
pylab.show()
这些是拟合的输出高斯参数:
#Output:
amplitude, position, sigma [ 23.47418352 41.27086097 5.91012897]
amplitude, position, sigma [ 16.26370263 106.39664543 3.67827824]
amplitude, position, sigma [ 59.74500239 163.11210316 2.89866545]
amplitude, position, sigma [ 57.91752456 220.24444939 2.91145375]
amplitude, position, sigma [ 39.78579841 251.25955921 2.74646334]
amplitude, position, sigma [ 86.50061756 260.62004831 3.08367483]
amplitude, position, sigma [ 79.26849867 319.64343319 3.57224402]
amplitude, position, sigma [ 127.97009966 399.27833126 3.14331212]
amplitude, position, sigma [ 20.21224956 379.41066063 195.47122312]