-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterpolation_k2.py
153 lines (99 loc) · 3.21 KB
/
interpolation_k2.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
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 4 07:20:37 2022
@author: A.Kuzmin
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
from scipy.interpolate import make_lsq_spline
from scipy.interpolate import InterpolatedUnivariateSpline
#, BSpline
#from scipy.interpolate import make_interp_spline
##########################################################
#
# Zero line for atomic absorption by LSQ spline
#
##########################################################
#
# Absorption coefficient mu(E) from E0 to Emax
#
def LSQ_ZeroLine(x, y, k):
xk = np.zeros(shape=(x.size))
ysk = np.zeros(shape=(xk.size))
# B-spline degree. Default is cubic, k=3.
# k = 3
# Calculate number of intervals
nk = round(x[-1]/500.0)+1
if nk <= 3:
nk = 3
print(nk)
# Calculate k axis
xk = (2.0/7.62*x)**0.5
print(xk)
# Calculate knots
tk = np.empty(shape=(nk-1))
tk.fill(0)
for i in range(0, nk-1):
tk[i] = np.sqrt((2.0/7.62*(i+1)*x[-1]/(nk))) #**0.5
print(tk)
# p = np.linspace(1, nk, nk)
# tk = (2.0/7.62*p*x[-1]/(nk))**0.5
# print(tk)
tt = np.r_[(xk[0],)*(k+1), tk, (xk[-1],)*(k+1)]
# print(tt)
# Compute the (coefficients of) an LSQ B-spline.
splk3 = make_lsq_spline(xk, y*xk*xk*xk, tt, k)
# Compute zero line
ysk = splk3(xk)/xk/xk/xk
i = 0
while xk[i] < tk[1]:
i = i + 1
n = i
xk_ = np.empty(shape=(xk.size-n))
xk_.fill(0)
ysk_ = np.empty(shape=(xk.size-n))
ysk_.fill(0)
for j in range(n,xk.size):
ysk_[j-n] = ysk[j]
xk_[j-n] = xk[j]
return ysk_
def extend_zeroline(x, y, x_new):
# Spline order: 1 linear, 2 quadratic, 3 cubic ...
order = 3
# Do extrapolation from E0 to Emin
s = InterpolatedUnivariateSpline(x, y, k=order)
return s(x_new)
##########################################################
# if __name__ == '__main__':
# # Main code
# nf = 999
# x = np.empty(shape=(nf))
# x.fill(0)
# xk = np.empty(shape=(nf))
# xk.fill(0)
# y = np.empty(shape=(nf))
# y.fill(0)
# #ysk2 = np.empty(shape=(nf))
# #ysk2.fill(0)
# #yy = np.empty(shape=(nf))
# #yy.fill(0)
# #
# # Create "Experimental data"
# #
# for i in range (0,nf):
# xk[i] = 30.0*(i+1)/(nf+1) # k space
# x[i] = xk[i]*xk[i]*7.62/2.0 # E space
# y[i] = 6.0*np.sin(2.0*xk[i]*2.0)*np.exp(-2.0*0.01*0.01*xk[i]*xk[i])/(xk[i]*2.0*2.0)
# # y[i] = y[i] + 2*np.sin(0.15*xk[i])
# # y[i] = y[i] + 2*np.sin(0.1*xk[i])
# y[i] = y[i] + 2*np.sin(0.01*xk[i])
# #
# # Calculate zero line for absorption coefficient mu(E)=y(x) from E0 to Emax
# #
# yy = LSQ_ZeroLine(x, y, 3)
# plt.plot(x, y, 'r-', lw=1, ms=5)
# #plt.plot(x, ysk2,'b-', lw=3, label='LSQ spline k^2')
# plt.plot(x, yy,'k-', lw=3, label='LSQ spline k^2')
# plt.legend(loc='best')
# plt.show()