-
Notifications
You must be signed in to change notification settings - Fork 0
/
fit.py
197 lines (148 loc) · 5.77 KB
/
fit.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 28 13:42:28 2021
@author: chris
"""
from lmfit import Model, minimize
import numpy as np
from PyQt5 import QtCore
import time
from data import Data, DataCollector
class FitCollector(QtCore.QRunnable):
def __init__(self):
super(FitCollector, self).__init__()
def run(self):
try:
Fit.fitter()
except Exception as e:
print(e)
class Fit(object):
result = []
f_fit=[]
scaling=0
data=[]
f=[]
center=1000
span=20
A=0
g=0
data_imag = []
r=0
n=0
B=0
phi=0
imag=False
timer=0.01
def C(n):
result=[8,14,18,20,20,18,14,8]
return result[n]
def lorentzian(x,x0,G,d,n):
return np.array(G/(d*n+x0-x-1j*G/2))
def lorentzian2(x,x0,G,d,n):
omega = (d*n+x0)
return np.array(2*G*x0/(omega**2-x**2-1j*x*G+(G/2)**2))
def phase(phi,i):
if i==0:
return np.exp(-1j*phi)
else:
return 1
def population(r,n):
return r**n-r**(n+1)
def morsABS(x,x0,phi,A,d,n,B,G_0,G_1,G_2,G_3,G_4,G_5,G_6,G_7,A_0,A_1,A_2,A_3,A_4,A_5,A_6,A_7):
model=np.zeros(len(x))
for i in range(n):
model = model + eval("A_%d"%i)*Fit.C(i)*Fit.phase(phi,i)*Fit.lorentzian(x,x0,eval("G_%d"%i),d,i)
model=np.abs(model)
model+=B
return np.log10(model)
def morsAngle(x,x0,phi,A,d,n,B,G_0,G_1,G_2,G_3,G_4,G_5,G_6,G_7,A_0,A_1,A_2,A_3,A_4,A_5,A_6,A_7,Angle):
model=np.zeros(len(x))
for i in range(n):
model = model + eval("A_%d"%i)*Fit.C(i)*Fit.phase(phi,i)*Fit.lorentzian(x,x0,eval("G_%d"%i),d,i)
model=np.angle(model*np.exp(-1j*Angle))
return model
def mors_ABS_Angle(x,x0,phi,A,d,n,B,G_0,G_1,G_2,G_3,G_4,G_5,G_6,G_7,A_0,A_1,A_2,A_3,A_4,A_5,A_6,A_7,Angle):
x = np.array_split(x,2)
R = Fit.morsABS(x[0],x0,phi,A,d,n,B,G_0,G_1,G_2,G_3,G_4,G_5,G_6,G_7,A_0,A_1,A_2,A_3,A_4,A_5,A_6,A_7)
I = Fit.morsAngle(x[1],x0,phi,A,d,n,B,G_0,G_1,G_2,G_3,G_4,G_5,G_6,G_7,A_0,A_1,A_2,A_3,A_4,A_5,A_6,A_7,Angle)
return np.hstack([R,I])
def morsPSDthermal(x,x0,r,phi,A,d,n,B,G_0,G_1,G_2,G_3,G_4,G_5,A_0,A_1,A_2,A_3):
model=np.zeros(len(x))
for i in range(n):
model = model + A*Fit.C(i)*Fit.population(r,i)*Fit.phase(phi,i)*Fit.lorentzian(x,x0,eval("G_%d"%i),d,i)
model=np.abs(model)**2
model+=B
return np.log10(model)
#A_0,A_1,A_2,A_3,A_4,A_5
def morsPSD(x,x0,phi,A,d,n,B,G_0,G_1,G_2,G_3,G_4,G_5,G_6,G_7,A_0,A_1,A_2,A_3,A_4,A_5,A_6,A_7):
model=np.zeros(len(x))
for i in range(n):
model = model + eval("A_%d"%i)*Fit.C(i)*Fit.phase(phi,i)*Fit.lorentzian(x,x0,eval("G_%d"%i),d,i)
model=np.abs(model)**2
model+=B
return np.log10(model)
def fitter():
start = time.time()
f_start = Fit.center-Fit.span
f_end = Fit.center+Fit.span
data_fit = Fit.data[(Fit.f>=f_start) & (Fit.f<=f_end)]
f_fit = Fit.f[(Fit.f>=f_start) & (Fit.f<=f_end)]
Fit.f_fit = f_fit
scaling = np.average(data_fit[:100]+data_fit[-100:])/2
data_fit=np.log10(data_fit/scaling)
w0 = f_fit[np.argmax(data_fit)]
d=2*w0**2/9.19e6
if Fit.imag == True:
data_fit_imag = Fit.data_imag[(Fit.f>=f_start) & (Fit.f<=f_end)]
data_fit = np.hstack([data_fit,data_fit_imag])
f_fit = np.hstack([f_fit,f_fit])
if Fit.imag == False:
model = Model(Fit.morsPSD)
else:
model = Model(Fit.mors_ABS_Angle)
model.set_param_hint('Angle', value=0)
model.set_param_hint('A', value=Fit.A,min=0)
# for i in range(int(0)):
# A=Fit.A*Fit.population(Fit.r,i)
# model.set_param_hint('A_%d'%i, value=A, min=0)
model.set_param_hint('phi', value=Fit.phi*np.pi,vary=True,min=-4*np.pi,max=4*np.pi)
model.set_param_hint('dphi', value=0,vary=True)
model.set_param_hint('x0', value=w0)
model.set_param_hint('d', value=d, vary=True,min=d*0.5,max=d*1.5)
for i in range(int(Fit.n)):
if i<3:
model.set_param_hint('G_%d'%i, value=Fit.g, min=0)
else:
model.set_param_hint('G_%d'%i, expr='G_2')
for i in range(int(Fit.n)):
model.set_param_hint('A_%d'%i, value=Fit.A*Fit.population(Fit.r,i)/Fit.population(Fit.r,0)*(i+1)**2, min=0)
# model.set_param_hint('r', value=Fit.r,min=0,max=1)
model.set_param_hint('B', value=Fit.B,vary=True,min=0)
model.set_param_hint('n', value=int(Fit.n),vary=False)
# sigma=np.sqrt(data_fit)
# sigma = np.log(data_fit)
# weights=1/sigma**1
# weights = 1/np.log(data_fit)
weights=np.ones(len(data_fit))
Fit.result = model.fit(data_fit, x=f_fit,weights=weights)
Fit.scaling = scaling
end = time.time()
Fit.timer=end-start+0.01
def spinpol(r):
F=4;
S44=1./(1+r+r**2+r**3+r**4+r**5+r**6+r**7+r**8);
m=np.linspace(4,-4,num=9)
p=sum(m*r**(4-m))*S44/F
return p
def spinpol3peak(n=0,A_0=0,A_1=0,A_2=0,A_3=0,A_4=0,A_5=0,A_6=0,A_7=0):
norm = 0
vec = np.arange(int(n))
S=np.zeros(int(n))
for i in range(int(n)):
S[i] = eval("A_%d"%i)
norm += eval("A_%d"%i)*(i+1)
sig = []
for i in range(int(n)):
sig.append(np.sum(S[i:])*(4-i))
p = 1/4 * np.sum(sig)/norm
return p