Skip to content

Instantly share code, notes, and snippets.

@theodoregoetz
Created December 2, 2015 02:27
Show Gist options
  • Save theodoregoetz/bd3560d85452f75511e9 to your computer and use it in GitHub Desktop.
Save theodoregoetz/bd3560d85452f75511e9 to your computer and use it in GitHub Desktop.
using scipy to fit a gaussian signal
import numpy as np
from scipy import optimize as opt
from scipy import stats
from matplotlib import pyplot
xx,yy = np.array([
[760.0500, 195],
[760.1500, 193],
[760.2500, 187],
[760.3500, 185],
[760.4500, 201],
[760.5500, 194],
[760.6500, 177],
[760.7500, 206],
[760.8500, 202],
[760.9500, 162],
[761.0500, 194],
[761.1500, 169],
[761.2500, 185],
[761.3500, 199],
[761.4500, 211],
[761.5500, 185],
[761.6500, 189],
[761.7500, 210],
[761.8500, 206],
[761.9500, 217],
[762.0500, 204],
[762.1500, 205],
[762.2500, 189],
[762.3500, 199],
[762.4500, 193],
[762.5500, 188],
[762.6500, 198],
[762.7500, 204],
[762.8500, 160],
[762.9500, 189],
[763.0500, 171],
[763.1500, 181],
[763.2500, 164],
[763.3500, 200],
[763.4500, 198],
[763.5500, 165],
[763.6500, 189],
[763.7500, 196],
[763.8500, 190],
[763.9500, 185],
[764.0500, 197],
[764.1500, 187],
[764.2500, 199],
[764.3500, 179],
[764.4500, 190],
[764.5500, 203],
[764.6500, 192],
[764.7500, 191],
[764.8500, 170],
[764.9500, 194],
[765.0500, 180],
[765.1500, 177],
[765.2500, 202],
[765.3500, 181],
[765.4500, 186],
[765.5500, 203],
[765.6500, 224],
[765.7500, 194],
[765.8500, 166],
[765.9500, 169],
[766.0500, 195],
[766.1500, 158],
[766.2500, 192],
[766.3500, 191],
[766.4500, 173],
[766.5500, 199],
[766.6500, 155],
[766.7500, 184],
[766.8500, 179],
[766.9500, 184],
[767.0500, 163],
[767.1500, 182],
[767.2500, 181],
[767.3500, 187],
[767.4500, 187],
[767.5500, 179],
[767.6500, 190],
[767.7500, 174],
[767.8500, 194],
[767.9500, 176],
[768.0500, 181],
[768.1500, 177],
[768.2500, 192],
[768.3500, 179],
[768.4500, 213],
[768.5500, 197],
[768.6500, 163],
[768.7500, 152],
[768.8500, 185],
[768.9500, 185],
[769.0500, 186],
[769.1500, 145],
[769.2500, 178],
[769.3500, 191],
[769.4500, 188],
[769.5500, 147],
[769.6500, 158],
[769.7500, 181],
[769.8500, 165],
[769.9500, 186],
[770.0500, 180],
[770.1500, 179],
[770.2500, 213],
[770.3500, 173],
[770.4500, 176],
[770.5500, 182],
[770.6500, 162],
[770.7500, 170],
[770.8500, 170],
[770.9500, 183],
[771.0500, 182],
[771.1500, 207],
[771.2500, 167],
[771.3500, 192],
[771.4500, 174],
[771.5500, 199],
[771.6500, 171],
[771.7500, 171],
[771.8500, 183],
[771.9500, 192],
[772.0500, 173],
[772.1500, 178],
[772.2500, 186],
[772.3500, 186],
[772.4500, 193],
[772.5500, 192],
[772.6500, 190],
[772.7500, 209],
[772.8500, 204],
[772.9500, 192],
[773.0500, 212],
[773.1500, 219],
[773.2500, 242],
[773.3500, 227],
[773.4500, 258],
[773.5500, 279],
[773.6500, 256],
[773.7500, 309],
[773.8500, 301],
[773.9500, 333],
[774.0500, 344],
[774.1500, 408],
[774.2500, 404],
[774.3500, 443],
[774.4500, 474],
[774.5500, 490],
[774.6500, 542],
[774.7500, 630],
[774.8500, 656],
[774.9500, 691],
[775.0500, 700],
[775.1500, 789],
[775.2500, 905],
[775.3500, 876],
[775.4500, 1002],
[775.5500, 990],
[775.6500, 1078],
[775.7500, 1137],
[775.8500, 1164],
[775.9500, 1260],
[776.0500, 1189],
[776.1500, 1240],
[776.2500, 1309],
[776.3500, 1294],
[776.4500, 1295],
[776.5500, 1391],
[776.6500, 1464],
[776.7500, 1462],
[776.8500, 1511],
[776.9500, 1439],
[777.0500, 1460],
[777.1500, 1467],
[777.2500, 1506],
[777.3500, 1474],
[777.4500, 1441],
[777.5500, 1401],
[777.6500, 1332],
[777.7500, 1348],
[777.8500, 1281],
[777.9500, 1249],
[778.0500, 1167],
[778.1500, 1200],
[778.2500, 1108],
[778.3500, 1067],
[778.4500, 1002],
[778.5500, 902],
[778.6500, 901],
[778.7500, 796],
[778.8500, 737],
[778.9500, 738],
[779.0500, 685],
[779.1500, 652],
[779.2500, 571],
[779.3500, 547],
[779.4500, 499],
[779.5500, 468],
[779.6500, 457],
[779.7500, 429],
[779.8500, 366],
[779.9500, 356],
[780.0500, 315],
[780.1500, 309],
[780.2500, 289],
[780.3500, 261],
[780.4500, 247],
[780.5500, 244],
[780.6500, 208],
[780.7500, 207],
[780.8500, 198],
[780.9500, 208],
[781.0500, 197],
[781.1500, 179],
[781.2500, 178],
[781.3500, 183],
[781.4500, 178],
[781.5500, 169],
[781.6500, 172],
[781.7500, 159],
[781.8500, 150],
[781.9500, 133],
[782.0500, 163],
[782.1500, 152],
[782.2500, 149],
[782.3500, 149],
[782.4500, 149],
[782.5500, 159],
[782.6500, 166],
[782.7500, 147],
[782.8500, 144],
[782.9500, 161],
[783.0500, 158],
[783.1500, 162],
[783.2500, 157],
[783.3500, 138],
[783.4500, 144],
[783.5500, 137],
[783.6500, 158],
[783.7500, 174],
[783.8500, 173],
[783.9500, 159],
[784.0500, 149],
[784.1500, 140],
[784.2500, 150],
[784.3500, 162],
[784.4500, 157],
[784.5500, 152],
[784.6500, 166],
[784.7500, 174],
[784.8500, 160],
[784.9500, 155],
[785.0500, 150],
[785.1500, 173],
[785.2500, 147],
[785.3500, 159],
[785.4500, 166],
[785.5500, 137],
[785.6500, 121],
[785.7500, 130],
[785.8500, 128],
[785.9500, 132],
[786.0500, 144],
[786.1500, 151],
[786.2500, 167],
[786.3500, 140],
[786.4500, 124],
[786.5500, 135],
[786.6500, 174],
[786.7500, 157],
[786.8500, 162],
[786.9500, 134],
[787.0500, 162],
[787.1500, 134],
[787.2500, 126],
[787.3500, 139],
[787.4500, 118],
[787.5500, 149],
[787.6500, 151],
[787.7500, 132],
[787.8500, 130],
[787.9500, 144],
[788.0500, 124],
[788.1500, 151],
[788.2500, 132],
[788.3500, 145],
[788.4500, 143],
[788.5500, 161],
[788.6500, 142],
[788.7500, 138],
[788.8500, 142],
[788.9500, 148],
[789.0500, 170],
[789.1500, 153],
[789.2500, 140],
[789.3500, 134],
[789.4500, 157],
[789.5500, 157],
[789.6500, 140],
[789.7500, 146],
[789.8500, 138],
[789.9500, 136]]).astype(np.float64).transpose()
print yy
yyerr = np.sqrt(yy)
gauss = lambda x,*p: p[0]*stats.norm(p[1],p[2]).pdf(x) + p[3]
popt,pcov = opt.curve_fit(gauss, xx, yy, p0=[yy.sum(),xx.mean(),1,1])
print popt
print pcov
xxfit = np.linspace(xx.min(),xx.max(),300)
yyfit = gauss(xxfit,*popt)
fig,ax = pyplot.subplots()
pt0 = ax.errorbar(xx, yy, yerr=yyerr)
pt1 = ax.plot(xxfit, yyfit)
pyplot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment