我正在设置一个代码,该代码对光谱数据进行遍历并找到连续体并删除该连续体,以便法向通量为0,并且我可以正确计算发射线的通量。
问题在于,这个连续体并不是真正的常数,并且大多数时候看起来像是根函数(并非总是如此)。我如何才能找到连续体?
这是我的光谱数据,这就是我发现峰的方式
光谱数据:
m1 = modeling.models.Gaussian1D(amplitude=10, mean=3726, stddev=8)
m2 = modeling.models.Gaussian1D(amplitude=10, mean=3126, stddev=8)
x = np.linspace(3000, 4500, 1000)
continuum_sqrt = np.sqrt(x)*0.2
mock_spectrum = continuum_sqrt + m1(x) + m2(x)
mock_spectrum = mock_spectrum + np.sqrt(mock_spectrum) * np.random.random(x.size) - 0.5
mock_spectrum -= mock_spectrum.min()
这是波斯人的发现:
def gaussfcn(x, *p):
""" gauss function to be used for fits to the data"""
A, mu, sigma = p
return A*np.exp(-(x-mu)**2/(2.*sigma**2))
class Gauss:
"""A class to hold coefficients for Gaussian distributions"""
def __init__(self, A, mu, sigma):
self.A = A
self.mu = mu
self.sigma = sigma
def value(self, x):
return gaussfcn(x, self.A, self.mu, self.sigma)
def as_string(self, ndigits=4):
return str("A: {}, mu: {}, sigma: {}".format(round(self.A, ndigits),
round(self.mu, ndigits),
round(self.sigma, ndigits)))
## set up a new plot
fig, axes = plt.subplots() # makes new plot
plt.grid(True)
plt.title('test')
## plot the data
plt.plot(x, mock_spectrum, linestyle='steps')
## Initial guess A, mu, sigma
P0 = [12,3127,1]
coeff, var_matrix = scipy.optimize.curve_fit(gaussfcn,
x,
mock_spectrum,
P0)
## create a Gauss object with the fitted coefficients for better code readability
g = Gauss(*coeff)
perr = np.sqrt(np.diag(var_matrix))
print("Fit result: {}".format(g.as_string()))
print("Fit uncertainties [%]: {}".format(100*perr/coeff))
## plot the result
axes.plot(x, g.value(x),
label = r'Gaussian fit, $\mu={}$, $\sigma={}$'.format(round(g.mu),round(g.sigma)))
plt.show()