-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaussian.py
45 lines (32 loc) · 919 Bytes
/
gaussian.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# simple script to fit a Gaussian to fake data
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
# Gaussian function
def gaussian(x, a, b, c, d):
val = d + (a * np.exp(-(x - b)**2 / c**2))
return val
# number of iterations
nit=1000
# array to store the result of each iteration
result = np.zeros(nit)
# fake x-data
x = np.arange(0.1,10.,0.1)
plt.ion()
plt.clf()
for i in range(0, nit):
# fake y-data
y = gaussian(x,5.,5.,1.,2.)+np.random.normal(1.,0.2,x.size)
# fit the data
popt, pcov = curve_fit(gaussian, x, y, p0=[5.,5.,1.,3.])
# fitted model
z=gaussian(x,popt[0],popt[1],popt[2],popt[3])
# save the fitted center of the gaussian
result[i] = popt[1]
print result[i]# plt.plot(x,y,'or')
#plt.plot(x,z)
#plt.draw()
#raw_input(':')
#plt.clf()
# plot a histogram of fitted centers of the gaussian
plt.hist(result,bins=20)