Blame view

fit_waist.py 1.92 KB
fc102800d   bmarechal   better method to ...
1
  #!/usr/bin/python
d0c0cc957   mer0m   Add files via upload
2
3
4
5
6
7
8
  from scipy.optimize import curve_fit
  import csv, numpy, glob
  from scipy.special import erf
  import matplotlib.pyplot as plt
  
  '''power function to optimize'''
  def P(x, Po, Pmax, xo, w):
c81fde478   bmarechal   replace 4 spaces ...
9
  	return Po+0.5*Pmax*(1.-erf(2.**0.5*(x-xo)/w))
d0c0cc957   mer0m   Add files via upload
10
11
12
13
14
15
16
17
18
  
  
  '''load and fit beam section'''
  files = glob.glob('*.dat')
  files.sort()
  data_waist = []
  plt.close()
  fig, p = plt.subplots(2, 1)
  for f in files:
c81fde478   bmarechal   replace 4 spaces ...
19
20
21
22
23
24
  	with open(f, 'r') as dest_f:
  		raw = csv.reader(dest_f, delimiter = '\t', quotechar = '"')
  		data = [value for value in raw]
  	data = numpy.asarray(data, dtype = float)
  	xmes = data[:,0]
  	Pmes = data[:,1]
d0c0cc957   mer0m   Add files via upload
25

c81fde478   bmarechal   replace 4 spaces ...
26
  	'''optimization with non-linear least squares method'''
f808f4392   bmarechal   use more robust t...
27
  	Ppopt, Pcov = curve_fit(P, xmes, Pmes, method = 'trf')
b869c4f31   bmarechal   more explicit dis...
28
29
30
31
32
  	z = int(f.split('-')[-1].split('.')[0])
  	w = Ppopt[3]
  	w_sig = numpy.sqrt(numpy.diag(Pcov))[3]
  	data_waist.append([z, w, w_sig])
  	print('z = %.3f mm\t w = %.3f mm (+-%.3f mm)'%(z, w, 1.96*w_sig))
d0c0cc957   mer0m   Add files via upload
33

c81fde478   bmarechal   replace 4 spaces ...
34
35
36
  	'''plot'''
  	p[0].plot(xmes, Pmes, 'o')
  	p[0].plot(numpy.linspace(xmes[0], xmes[-1], 100), P(numpy.linspace(xmes[0], xmes[-1], 100), *Ppopt))
d0c0cc957   mer0m   Add files via upload
37
38
39
40
41
  
  p[0].grid()
  
  '''return waist(z) table'''
  data_waist = numpy.asarray(data_waist, dtype = float)
d0c0cc957   mer0m   Add files via upload
42
43
44
  
  '''waist function to optimize'''
  def W(z, w0, z0):
c81fde478   bmarechal   replace 4 spaces ...
45
  	return w0*(1.+((z-z0)*1542e-6/(numpy.pi*w0**2))**2)**0.5
d0c0cc957   mer0m   Add files via upload
46
47
  
  popt, cov = curve_fit(W, data_waist[:,0], data_waist[:,1])
b869c4f31   bmarechal   more explicit dis...
48
49
50
51
52
53
  w0 = popt[0]
  w0_sig = numpy.sqrt(numpy.diag(cov))[0]
  z0 = popt[1]
  z0_sig = numpy.sqrt(numpy.diag(cov))[1]
  print('
  z0 = %.3f mm (+-%.3f mm)\t w0 = %.3f mm (+-%.3f mm)'%(z0, 1.96*z0_sig, w0, 1.96*w0_sig))
d0c0cc957   mer0m   Add files via upload
54
55
56
  
  p[1].plot(data_waist[:,0], data_waist[:,1], 'bo')
  p[1].plot(data_waist[:,0], -data_waist[:,1], 'bo')
f4aa0fa9a   silicium   min-max for plotting
57
58
  p[1].plot(numpy.linspace(min(data_waist[:,0]), max(data_waist[:,0]), 100), W(numpy.linspace(min(data_waist[:,0]), max(data_waist[:,0]), 100), *popt), 'r')
  p[1].plot(numpy.linspace(min(data_waist[:,0]), max(data_waist[:,0]), 100), -W(numpy.linspace(min(data_waist[:,0]), max(data_waist[:,0]), 100), *popt), 'r')
d0c0cc957   mer0m   Add files via upload
59
  p[1].grid()
55e48965f   bmarechal   abs
60
  plt.show()