Created
September 17, 2020 05:39
-
-
Save jinensetpal/fcbfb5cce088b2f34b2a73242d887bb7 to your computer and use it in GitHub Desktop.
boilerplate gaussian fit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import numpy as np | |
from scipy import optimize | |
from astropy import modelling | |
import matplotlib.pyplot as plt | |
def gaussian(x, amplitude, mean, stddev): | |
return amplitude * np.exp(-((x - mean) / 4 / stddev)**2) | |
m = modeling.models.Gaussian1D(amplitude=10, mean=30, stddev=5) ### Varies | |
x = np.linspace(0, 100, 2000) ### Varies | |
data = m(x) | |
data = data + np.sqrt(data) * np.random.random(x.size) - 0.5 | |
data -= data.min() | |
plt.plot(x, data) | |
popt, _ = optimize.curve_fit(gaussian, x, data) | |
fitter = modeling.fitting.LevMarLSQFitter() | |
model = modeling.models.Gaussian1D() # initial values may be necessary; data dependent | |
fitted_model = fitter(model, x, data) | |
plt.plot(x, data) | |
plt.plot(x, gaussian(x, *popt)) | |
### to predict | |
fitted_model(100) # fitted_model(<val>) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment