gauss

mdhelper.fit.gaussian.gauss(x: ndarray, *args: float) ndarray[source]

General Gaussian model.

\[y=\sum_{k=1}^na_k\exp{\left[ -\left(\frac{x-b_k}{c_k}\right)^2\right]}\]
Parameters:
xnumpy.ndarray

\(x\)-values.

*argsfloat

Fitting parameters for the Gaussian term(s), ordered as \(a_1,\,b_1,\,c_1,\,a_2,\,b_2,\,c_2,\ldots,\,a_n,\,b_n,\,c_n\), where \(n\) is the number of terms in the model. As such, the number of variable positional arguments must be divisible by \(3\).

Returns:
fitnumpy.ndarray

Fitted \(y\)-values.

Examples

Generate \(x\)- and \(y\)-values (with error), and then use scipy.optimize.curve_fit() to fit coefficients for a one-term Gaussian model.

>>> from scipy import optimize
>>> rng = np.random.default_rng()
>>> x = np.linspace(-3, 7, 10)
>>> err = (2 * rng.random(x.shape) - 1) / 10
>>>y=np.exp(-((x - 2) / 3) ** 2) + err
>>> pk, _ = optimize.curve_fit(lambda x, a1, b1, c1: gauss(x, a1, b1, c1), x, y)
>>> pk
array([1.07262377, 1.90290018, 2.90033242])

Evaluate the fitted \(y\)-values using the coefficients.

>>> gauss(x, *pk)
array([0.06157175, 0.19415629, 0.45650326, 0.80031095, 1.04615509,
       1.01966105, 0.74103382, 0.40155281, 0.16224441, 0.04887866])