Q3D-Calibration/qdx/fit.py

55 lines
1.1 KiB
Python

import numpy as np
from astropy.modeling import models, fitting
from .utils import get_hist
def fit_line(model, x, y):
"""
Line fitting kx+b
Parameters
----------
model : astropy.modeling.Model
fit model
x : array
x data, can have two columns representing two independent variables
y : array
y data
Returns
-------
fitted_model : astropy fitting model
fitting gaussian model
"""
fitter = fitting.LevMarLSQFitter()
if np.ndim(x) == 1:
fitted_model = fitter(model, x, y)
else:
fitted_model = fitter(model, x[:, 0], x[:, 1], y)
return fitted_model
def fit_hist_gaussian(x):
"""
Gaussian fitting is performed on the histogram
Parameters
----------
x : array
data point
Returns
-------
fitted_model : astropy fitting model
fitting gaussian model
"""
fitter = fitting.LMLSQFitter()
count, center = get_hist(x)
model = models.Gaussian1D(amplitude=count.max(), mean=x.mean(), stddev=x.std())
fitted_model = fitter(model, center, count)
return fitted_model