Skip to content

Fit a curve#

fitting.fit picks a sensible algorithm and starting guess for you. Pass your data and a model from the models catalogue:

result = fitting.fit(state.spectrum, models.exponential_decay)
print("parameters:", result.parameters)
print("fit error:", result.loss)

The result also carries diagnostics — result.data_info.snr_estimate, result.model_info.function_type, and more.

Overlay the fitted curve by evaluating it on a fine grid with fitting.predict (np is already available — no import needed):

x = np.linspace(state.spectrum.frequencies.min(), state.spectrum.frequencies.max(), 500)
y = fitting.predict(x, models.exponential_decay, result.parameters)
viz.annotate((x, y), title="fit")

Any function that takes (x, *params) works as a model:

def my_model(x, a, b, c):
    return a * np.exp(-x / b) + c

result = fitting.fit((xdata, ydata), my_model)

The models catalogue covers exponential, Gaussian/Lorentzian, stretched, power-law, sigmoid, and NMR relaxation-dispersion shapes — each documented with its formula in the Backend APIs reference.