-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLorentzian_Fit_ESR_2.py
70 lines (57 loc) · 2.46 KB
/
Lorentzian_Fit_ESR_2.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
####################### IMPORTING REQUIRED MODULES ######################
import pandas as pd
import numpy as np
import matplotlib.pyplot as pl
from scipy.optimize import leastsq # Levenberg-Marquadt Algorithm #
############################# LOADING DATA ##############################
Data = pd.read_csv('/home/dwi/ownCloud/Anlabshared/Dwi/Data and Analysis/160525/2016_05_25_magparticle_.post5.csv')
Data.columns = ['Microwave Frequency [GHz]', 'Intensity']
x = Data['Microwave Frequency [GHz]']
y = Data['Intensity']/Data['Intensity'].max()
#HWHM = 3550000
#dip = 0.04
########################### DEFINING FUNCTIONS ##########################
#p = [8.5*10**-4, 0, 0, 7000000, 0.023] # [B, theta, phi, HWHM, dip]
def lorentzian(x,p):
numerator = p[3]**2
denumerator = ( x - (2.87*10**9 + (28*10**9 * p[0] * np.sqrt((np.sin (p[1]) * np.cos(p[2]))**2 + (np.sin(p[1]) * np.sin(p[2]))**2 + (np.cos(p[2]))**2))))**2 + p[3]**2
return 0.995 - p[4]*(numerator/denumerator)
def lorentzian2(x,p):
numerator = p[3]**2
denumerator = ( x - (2.87*10**9 - (28*10**9 * p[0] * np.sqrt((np.sin(p[1]) * np.cos(p[2]))**2 + (np.sin(p[1]) * np.sin(p[2]))**2 + (np.cos(p[2]))**2))))**2 + p[3]**2
return 0.995 - p[4]*(numerator/denumerator)
def residuals(p,y,x):
err = y - lorentzian(x,p)
return err
def residuals2(p,y,x):
err = y - lorentzian2(x,p)
return err
############################# FITTING DATA ## ###########################
# initial values #
p = [1*10**-4, 0, 0, 3550000, 0.02] # [B, theta, phi,HWHM, dip]
# optimization #
pbest = leastsq(residuals, p ,args=(y[70:143], x[70:143]),full_output=1)
best_parameters1 = pbest[0]
pbest2 = leastsq(residuals2, p ,args=(y[144:300], x[144:300]),full_output=1)
best_parameters2 = pbest2[0]
# fit to data #
fit = lorentzian(x,best_parameters1)
fit2 = lorentzian2(x,best_parameters2)
############################## PLOTTING #################################
pl.rc('text', usetex=True)
pl.rc('font', family='serif')
pl.plot(x,y, lw = 1.5, color='g', label='Measured ESR spectrum')
pl.plot(x, fit, lw=1.5, color = 'k', label = r"Least-square fit")
pl.plot( x, fit2, lw=1.5, color = 'k')
#pl.plot(x, lorentzian(x,p), x, lorentzian2(x,p))
pl.xlabel('Microwave frequency (GHz)', fontsize=18)
pl.ylabel('Intensity (norm.)', fontsize=18)
#pl.plot(x[143:222], y[143:222], color='r')
pl.legend()
pl.show()
print abs(best_parameters1)
print abs(best_parameters2)
#print Data
#print x[:68]
#print residuals(p,y, x)
#print lorentzian (x, p)