Skip to content

Instantly share code, notes, and snippets.

@cpascual
Created December 21, 2017 12:31
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cpascual/a03d0d49ddd2c87d7e84b9f4ad2df466 to your computer and use it in GitHub Desktop.
Save cpascual/a03d0d49ddd2c87d7e84b9f4ad2df466 to your computer and use it in GitHub Desktop.
A simple example on fitting a gaussian
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def gauss(x, H, A, x0, sigma):
return H + A * np.exp(-(x - x0) ** 2 / (2 * sigma ** 2))
def gauss_fit(x, y):
mean = sum(x * y) / sum(y)
sigma = np.sqrt(sum(y * (x - mean) ** 2) / sum(y))
popt, pcov = curve_fit(gauss, x, y, p0=[min(y), max(y), mean, sigma])
return popt
# generate simulated data
np.random.seed(123) # comment out if you want different data each time
xdata = np.linspace(3, 10, 100)
ydata_perfect = gauss(xdata, 20, 5, 6, 1)
ydata = np.random.normal(ydata_perfect, 1, 100)
H, A, x0, sigma = gauss_fit(xdata, ydata)
FWHM = 2.35482 * sigma
print('The offset of the gaussian baseline is', H)
print('The center of the gaussian fit is', x0)
print('The sigma of the gaussian fit is', sigma)
print('The maximum intensity of the gaussian fit is', H + A)
print('The Amplitude of the gaussian fit is', A)
print('The FWHM of the gaussian fit is', FWHM)
plt.plot(xdata, ydata, 'ko', label='data')
plt.plot(xdata, ydata_perfect, '-k', label='data (without_noise)')
plt.plot(xdata, gauss(xdata, *gauss_fit(xdata, ydata)), '--r', label='fit')
plt.legend()
plt.title('Gaussian fit, $f(x) = A e^{(-(x-x_0)^2/(2sigma^2))}$')
plt.xlabel('Motor position')
plt.ylabel('Intensity (A)')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment