1489 lines
52 KiB
Python
1489 lines
52 KiB
Python
import numpy as np
|
||
from scipy.optimize import least_squares
|
||
|
||
try:
|
||
from .math_utils import ind, polyfit2
|
||
from .geometry_utils import strng2
|
||
from .signal_utils import filter2k3
|
||
except ImportError:
|
||
from math_utils import ind, polyfit2
|
||
from geometry_utils import strng2
|
||
from signal_utils import filter2k3
|
||
|
||
|
||
def _simple_lm(fun, x0, ftol=1e-6, xtol=1e-6, max_nfev=400):
|
||
x = np.asarray(x0, dtype=np.float64).copy()
|
||
n = len(x)
|
||
lambd = 1e-3
|
||
nu = 2.0
|
||
|
||
residuals = fun(x)
|
||
cost = np.sum(residuals ** 2)
|
||
|
||
nfev = 1
|
||
success = False
|
||
|
||
while nfev < max_nfev:
|
||
eps = 1e-8
|
||
J = np.zeros((len(residuals), n))
|
||
for j in range(n):
|
||
dx_eps = np.zeros(n)
|
||
dx_eps[j] = eps * max(1.0, abs(x[j]))
|
||
residuals_plus = fun(x + dx_eps)
|
||
J[:, j] = (residuals_plus - residuals) / dx_eps[j]
|
||
nfev += 1
|
||
if nfev >= max_nfev:
|
||
break
|
||
|
||
if nfev >= max_nfev:
|
||
break
|
||
|
||
JtJ = J.T @ J
|
||
Jtr = J.T @ residuals
|
||
|
||
for _ in range(10):
|
||
diag = np.diag(JtJ) + lambd
|
||
A = JtJ + lambd * np.eye(n)
|
||
try:
|
||
delta = np.linalg.solve(A, Jtr)
|
||
except np.linalg.LinAlgError:
|
||
delta = np.linalg.lstsq(A, Jtr, rcond=None)[0]
|
||
|
||
x_new = x - delta
|
||
residuals_new = fun(x_new)
|
||
cost_new = np.sum(residuals_new ** 2)
|
||
nfev += 1
|
||
|
||
if cost_new < cost:
|
||
rho = (cost - cost_new) / (delta @ (lambd * delta + Jtr) + 1e-10)
|
||
if rho > 0.75:
|
||
lambd = lambd / 3.0
|
||
elif rho < 0.25:
|
||
lambd = lambd * 2.0
|
||
|
||
if np.max(np.abs(delta)) < xtol:
|
||
success = True
|
||
x = x_new
|
||
residuals = residuals_new
|
||
cost = cost_new
|
||
break
|
||
|
||
if abs(cost - cost_new) < ftol:
|
||
success = True
|
||
x = x_new
|
||
residuals = residuals_new
|
||
cost = cost_new
|
||
break
|
||
|
||
x = x_new
|
||
residuals = residuals_new
|
||
cost = cost_new
|
||
break
|
||
else:
|
||
lambd = lambd * nu
|
||
nu = nu * 2.0
|
||
|
||
else:
|
||
break
|
||
|
||
return x, cost, success, nfev
|
||
|
||
|
||
def attcalcx0(x, y, x0, x0r, right):
|
||
att0 = 0.0
|
||
x = np.asarray(x, dtype=np.float64)
|
||
y = np.asarray(y, dtype=np.float64)
|
||
y = np.where(y <= 0, 1e-6, y)
|
||
|
||
N = len(x)
|
||
if N > 4:
|
||
dx = (x[N - 1] - x[0]) / (N - 1)
|
||
|
||
if not right:
|
||
xleft = x0
|
||
xright = x[N - 1]
|
||
else:
|
||
xleft = x[0]
|
||
xright = x0r
|
||
|
||
if (xright - xleft) > dx:
|
||
ll = int(ind(xleft, dx, x[0], N))
|
||
rr = int(ind(xright, dx, x[0], N))
|
||
xt = x[ll - 1:rr]
|
||
yt = y[ll - 1:rr]
|
||
s0 = strng2(xt, x0, x0r)
|
||
min_s0 = 0.01 * (x0r - x0) if (x0r - x0) > 0 else 0.01
|
||
indvek = np.where(s0 > min_s0)[0]
|
||
if len(indvek) > 0:
|
||
att0 = -np.mean(np.log(yt[indvek]) / s0[indvek])
|
||
if np.isnan(att0) or np.isinf(att0):
|
||
att0 = 0.0
|
||
|
||
return att0
|
||
|
||
|
||
def attcalcx1(x, y, x0, x0r, x1, x1r, right):
|
||
att0 = 0.0
|
||
att1 = 0.0
|
||
x = np.asarray(x, dtype=np.float64)
|
||
y = np.asarray(y, dtype=np.float64).copy()
|
||
|
||
y[y <= 0] = 1e-6
|
||
|
||
N = len(x)
|
||
if N > 4:
|
||
dx = (x[N - 1] - x[0]) / (N - 1)
|
||
|
||
if not right:
|
||
xleft = x[0]
|
||
xright = x1
|
||
else:
|
||
xleft = x1r
|
||
xright = x[N - 1]
|
||
|
||
if (xright - xleft) > dx:
|
||
ll = int(ind(xleft, dx, x[0], N))
|
||
rr = int(ind(xright, dx, x[0], N))
|
||
xt = x[ll - 1:rr]
|
||
yt = y[ll - 1:rr]
|
||
s0 = strng2(xt, x0, x0r)
|
||
min_s0 = 0.01 * (x0r - x0) if (x0r - x0) > 0 else 0.01
|
||
indvek = np.where(s0 > min_s0)[0]
|
||
if len(indvek) > 0:
|
||
att0 = -np.mean(np.log(yt[indvek]) / s0[indvek])
|
||
if np.isnan(att0) or np.isinf(att0):
|
||
att0 = 0.0
|
||
|
||
if not right:
|
||
xleft = x1 + dx
|
||
xright = x[N - 1]
|
||
else:
|
||
xleft = x[0]
|
||
xright = x1r - dx
|
||
|
||
if (((xright - xleft) > dx) and (xright > x1) and (xright < x1r) and
|
||
(xleft > x1) and (xleft < x1r) and (att0 > 0)):
|
||
ll = int(ind(xleft, dx, x[0], N))
|
||
rr = int(ind(xright, dx, x[0], N))
|
||
xt = x[ll - 1:rr]
|
||
yt = y[ll - 1:rr]
|
||
s0 = strng2(xt, x0, x0r)
|
||
s1 = strng2(xt, x1, x1r)
|
||
min_s1 = 0.01 * (x1r - x1) if (x1r - x1) > 0 else 0.01
|
||
indvek = np.where(s1 > min_s1)[0]
|
||
if len(indvek) > 0:
|
||
att1 = np.mean((-np.log(yt[indvek]) - att0 * (s0[indvek] - s1[indvek])) / s1[indvek])
|
||
if np.isnan(att1) or np.isinf(att1):
|
||
att1 = 0.0
|
||
|
||
return att0, att1
|
||
|
||
|
||
def resfail(resnorm, lyt, meanyt):
|
||
return np.sqrt(resnorm / lyt) / meanyt
|
||
|
||
|
||
def edge_error2(calcval, exitflag, resnorm, avgval, startalpha0, minxt, maxxt,
|
||
minalpha0, maxalpha0, mincontr, maxcontr, contrsign, resmax,
|
||
diffxt1, diffxt2, lengthxt):
|
||
errorl = np.zeros(2, dtype=np.float64)
|
||
|
||
if exitflag < 0:
|
||
errorl[0] = 51
|
||
else:
|
||
if (calcval['x'] < (minxt + diffxt1)) or (calcval['x'] > (maxxt - diffxt1)):
|
||
errorl[1] = 47
|
||
if (calcval['x'] < (minxt + diffxt2)) or (calcval['x'] > (maxxt - diffxt2)):
|
||
errorl[0] = 47
|
||
if calcval['alpha0'] < minalpha0:
|
||
errorl[0] = 52
|
||
if calcval['alpha0'] > maxalpha0:
|
||
errorl[0] = 53
|
||
if abs(calcval['alpha1'] - calcval['alpha0']) < mincontr:
|
||
errorl[0] = 54
|
||
if (((calcval['alpha1'] - calcval['alpha0']) < 0) and (contrsign == 1)) or \
|
||
(((calcval['alpha1'] - calcval['alpha0']) > 0) and (contrsign == -1)):
|
||
errorl[0] = 56
|
||
|
||
relfail = resfail(resnorm, lengthxt, avgval)
|
||
if relfail > resmax:
|
||
errorl[0] = 57
|
||
if (calcval['x'] < minxt) or (calcval['x'] > maxxt):
|
||
errorl[0] = 46
|
||
|
||
return errorl
|
||
|
||
|
||
def x0fun(para_dyn, para_static, x, yactual=None):
|
||
x = np.asarray(x, dtype=np.float64)
|
||
|
||
x0 = para_dyn[0]
|
||
x0r = para_dyn[1]
|
||
alpha0 = para_dyn[2]
|
||
|
||
x3 = None
|
||
x3r = None
|
||
alpha1 = None
|
||
beta1 = None
|
||
if para_static.get('xlr41det', 0) == 1:
|
||
x3 = para_dyn[3]
|
||
x3r = para_dyn[4]
|
||
elif para_static.get('xlr41det', 0) == 2:
|
||
x3 = para_dyn[3]
|
||
x3r = para_dyn[4]
|
||
alpha1 = para_dyn[5]
|
||
beta1 = para_static.get('beta1', 0)
|
||
|
||
filtactive = para_static.get('filtactive', 0)
|
||
# 读取原始mm值,避免引用传递导致的重复转换
|
||
filtlength = para_static.get('filtlength_mm', para_static.get('filtlength', 0))
|
||
|
||
xout = None
|
||
|
||
if yactual is not None:
|
||
yactual = np.asarray(yactual, dtype=np.float64)
|
||
minx = np.min(x)
|
||
maxx = np.max(x)
|
||
dx = (maxx - minx) / (len(x) - 1)
|
||
if (x0 > minx) and (x0 < maxx):
|
||
stopind = int(ind((x0 - 2 * filtlength), dx, x[0], len(x)))
|
||
I0 = np.mean(yactual[0:stopind])
|
||
elif (x0r > minx) and (x0r < maxx):
|
||
startind = int(ind((x0r + 2 * filtlength), dx, x[0], len(x)))
|
||
I0 = np.mean(yactual[startind - 1:])
|
||
else:
|
||
I0 = 1.0
|
||
|
||
minI0 = 0.93
|
||
maxI0 = 1.07
|
||
if I0 > maxI0:
|
||
I0 = maxI0
|
||
elif I0 < minI0:
|
||
I0 = minI0
|
||
else:
|
||
I0 = para_static.get('I0', 1.0)
|
||
|
||
xlr41det = para_static.get('xlr41det', 0)
|
||
if xlr41det == 0:
|
||
s1 = strng2(x, x0, x0r, 1)
|
||
y = I0 * np.exp(-(alpha0 * s1))
|
||
elif xlr41det == 1:
|
||
s1 = strng2(x, x0, x0r, 1)
|
||
s3 = strng2(x, x3, x3r, 1)
|
||
y = I0 * np.exp(-(alpha0 * (s1 - s3)))
|
||
elif xlr41det == 2:
|
||
s1 = strng2(x, x0, x0r, 1)
|
||
s3 = strng2(x, x3, x3r, 1)
|
||
y = I0 * np.exp(-(alpha0 * (s1 - s3) + beta1 * ((s1 - s3) ** 2) + alpha1 * s3))
|
||
else:
|
||
y = np.zeros_like(x)
|
||
|
||
N2 = 0
|
||
if filtactive:
|
||
if para_static.get('unsharpness', 0) == 1:
|
||
from .filtering_utils import smooth
|
||
filtvek = smooth(para_static.get('a1', 1), para_static.get('a2', 1),
|
||
para_static.get('a3', 1), para_static.get('filtkoff', 1))
|
||
y, N2 = filter2k3(filtvek, y, 1)
|
||
else:
|
||
dx = x[1] - x[0]
|
||
filtlen = int(np.round(filtlength / dx))
|
||
if filtlen % 2 == 0:
|
||
filtlen = filtlen + 1
|
||
y, N2 = filter2k3((1.0 / filtlen) * np.ones(filtlen), y)
|
||
|
||
startind = N2
|
||
stopind = len(x) - N2
|
||
|
||
if para_static.get('lsqn', 0) == 1:
|
||
if yactual is not None:
|
||
y = y[startind:stopind] - yactual[startind:stopind]
|
||
else:
|
||
xout = x[startind:stopind]
|
||
y = y[startind:stopind]
|
||
|
||
return y, I0, xout, N2
|
||
|
||
|
||
def x1fun3setup(para_static, alpha0, alpha1):
|
||
mode = para_static.get('mode', 0)
|
||
|
||
if mode == 0:
|
||
para_dyn = np.array([alpha0, para_static['x1'], para_static['x1r'], alpha1], dtype=np.float64)
|
||
elif mode == 1:
|
||
para_dyn = np.array([alpha0, para_static['x1'], para_static['x1r']], dtype=np.float64)
|
||
elif mode == 2:
|
||
para_dyn = np.array([alpha0, para_static['x1'], alpha1], dtype=np.float64)
|
||
elif mode == 3:
|
||
para_dyn = np.array([alpha0, para_static['x1r'], alpha1], dtype=np.float64)
|
||
elif mode == 4:
|
||
para_dyn = np.array([alpha0, para_static['x1'], para_static['x1r'],
|
||
para_static['x0r'] - para_static['x0']], dtype=np.float64)
|
||
elif mode == 5 or mode == 6:
|
||
para_dyn = np.array([para_static['x1'], para_static['x1r']], dtype=np.float64)
|
||
elif mode == 7:
|
||
para_dyn = np.array([alpha0, para_static['x1'], para_static['x1r'], alpha1,
|
||
para_static.get('x2', 0), para_static.get('x2r', 0), 0.0], dtype=np.float64)
|
||
else:
|
||
para_dyn = np.array([alpha0, para_static['x1'], para_static['x1r'], alpha1], dtype=np.float64)
|
||
|
||
return para_dyn
|
||
|
||
|
||
def x1fun3out(calcpara, para_static):
|
||
mode = para_static.get('mode', 0)
|
||
dataout = {}
|
||
|
||
if mode == 0:
|
||
dataout['alpha0'] = calcpara[0]
|
||
dataout['x1'] = calcpara[1]
|
||
dataout['x1r'] = calcpara[2]
|
||
dataout['alpha1'] = calcpara[3]
|
||
elif mode == 1:
|
||
dataout['alpha0'] = calcpara[0]
|
||
dataout['x1'] = calcpara[1]
|
||
dataout['x1r'] = calcpara[2]
|
||
dataout['alpha1'] = 0.0
|
||
elif mode == 2:
|
||
dataout['alpha0'] = calcpara[0]
|
||
dataout['x1'] = calcpara[1]
|
||
dataout['alpha1'] = calcpara[2]
|
||
dataout['x1r'] = para_static.get('x1r', 0)
|
||
elif mode == 3:
|
||
dataout['alpha0'] = calcpara[0]
|
||
dataout['x1r'] = calcpara[1]
|
||
dataout['alpha1'] = calcpara[2]
|
||
dataout['x1'] = para_static.get('x1', 0)
|
||
elif mode == 4:
|
||
dataout['alpha0'] = calcpara[0]
|
||
dataout['x1'] = calcpara[1]
|
||
dataout['x1r'] = calcpara[2]
|
||
dataout['alpha1'] = 0.0
|
||
elif mode == 5:
|
||
reg_sub = para_static.get('reg_sub', {})
|
||
dataout['alpha0'] = reg_sub.get('alpha0', 0)
|
||
dataout['x1'] = calcpara[0]
|
||
dataout['x1r'] = calcpara[1]
|
||
dataout['alpha1'] = 0.0
|
||
elif mode == 6:
|
||
reg_sub = para_static.get('reg_sub', {})
|
||
dataout['alpha0'] = reg_sub.get('alpha0', 0)
|
||
dataout['x1'] = calcpara[0]
|
||
dataout['x1r'] = calcpara[1]
|
||
dataout['alpha1'] = reg_sub.get('alpha1', 0)
|
||
elif mode == 7:
|
||
dataout['alpha0'] = calcpara[0]
|
||
dataout['x1'] = calcpara[1]
|
||
dataout['x1r'] = calcpara[2]
|
||
dataout['alpha1'] = calcpara[3]
|
||
else:
|
||
dataout['alpha0'] = 0.0
|
||
dataout['x1'] = 0.0
|
||
dataout['x1r'] = 0.0
|
||
dataout['alpha1'] = 0.0
|
||
|
||
return dataout
|
||
|
||
|
||
def x0_x0r_reg_sub(x, IN, klevel=0, Anzpara=0):
|
||
x = np.asarray(x, dtype=np.float64)
|
||
IN = np.asarray(IN, dtype=np.float64)
|
||
|
||
trueind = np.where(IN < 1)[0]
|
||
x = x[trueind]
|
||
IN = IN[trueind]
|
||
x0 = 0.0
|
||
x0r = 0.0
|
||
alpha0 = 0.0
|
||
errorl = np.zeros(2, dtype=np.float64)
|
||
P = np.zeros(3, dtype=np.float64)
|
||
|
||
if len(IN) > 3:
|
||
IN2 = (np.log(IN)) ** 2
|
||
P = polyfit2(x, IN2, 2)
|
||
|
||
A = P[1] / P[0]
|
||
B = (P[1] ** 2 / (4 * P[0] ** 2)) - (P[2] / P[0])
|
||
if (B > 0) and (-P[0] > 0):
|
||
x0 = -0.5 * A - np.sqrt(B)
|
||
alpha0 = 0.5 * np.sqrt(-P[0])
|
||
x0r = -(x0 + A)
|
||
if alpha0 < klevel:
|
||
errorl[0] = 11
|
||
else:
|
||
errorl[0] = 58
|
||
else:
|
||
errorl[0] = 58
|
||
|
||
return x0, x0r, alpha0, errorl, P
|
||
|
||
|
||
def x1fun3(para_dyn, para_static, x, yactual=None):
|
||
x = np.asarray(x, dtype=np.float64)
|
||
|
||
regsubres = {}
|
||
xout = None
|
||
|
||
mode = para_static.get('mode', 0)
|
||
|
||
if mode == 0:
|
||
alpha0 = para_dyn[0]
|
||
x1 = para_dyn[1]
|
||
x1r = para_dyn[2]
|
||
alpha1 = para_dyn[3]
|
||
x0 = para_static['x0']
|
||
x0r = para_static['x0r']
|
||
elif mode == 1:
|
||
alpha0 = para_dyn[0]
|
||
x1 = para_dyn[1]
|
||
x1r = para_dyn[2]
|
||
x0 = para_static['x0']
|
||
x0r = para_static['x0r']
|
||
alpha1 = 0.0
|
||
elif mode == 2:
|
||
alpha0 = para_dyn[0]
|
||
x1 = para_dyn[1]
|
||
alpha1 = para_dyn[2]
|
||
x1r = para_static['x1r']
|
||
x0 = para_static['x0']
|
||
x0r = para_static['x0r']
|
||
elif mode == 3:
|
||
alpha0 = para_dyn[0]
|
||
x1r = para_dyn[1]
|
||
alpha1 = para_dyn[2]
|
||
x1 = para_static['x1']
|
||
x0 = para_static['x0']
|
||
x0r = para_static['x0r']
|
||
elif mode == 4:
|
||
alpha0 = para_dyn[0]
|
||
x1 = para_dyn[1]
|
||
x1r = para_dyn[2]
|
||
d0 = para_dyn[3]
|
||
x0 = 0.5 * (para_static['x0'] + para_static['x0r']) - 0.5 * d0
|
||
x0r = x0 + d0
|
||
alpha1 = 0.0
|
||
elif mode == 5:
|
||
x1 = para_dyn[0]
|
||
x1r = para_dyn[1]
|
||
alpha1 = 0.0
|
||
|
||
if yactual is not None:
|
||
yactual = np.asarray(yactual, dtype=np.float64)
|
||
minx = np.min(x)
|
||
maxx = np.max(x)
|
||
dx = (maxx - minx) / (len(x) - 1)
|
||
xmitt = 0.5 * (para_static['x0'] + para_static['x0r'])
|
||
if (x1 > minx) and (x1 <= maxx):
|
||
startind = 0
|
||
stopind = int(ind((x1 - 0.5 * para_static.get('filtlength', 0)), dx, x[0], len(x)))
|
||
xright = x[stopind - 1] + 2 * (xmitt - x[stopind - 1])
|
||
xreg = np.concatenate([x[startind:stopind], [xright]])
|
||
yreg = np.concatenate([yactual[startind:stopind], [yactual[stopind - 1]]])
|
||
rangeerr = 0
|
||
elif (x1r > minx) and (x1r <= maxx):
|
||
startind = int(ind((x1r + 0.5 * para_static.get('filtlength', 0)), dx, x[0], len(x)))
|
||
stopind = len(x)
|
||
xleft = x[startind - 1] - 2 * (x[startind - 1] - xmitt)
|
||
xreg = np.concatenate([[xleft], x[startind - 1:stopind]])
|
||
yreg = np.concatenate([[yactual[startind - 1]], yactual[startind - 1:stopind]])
|
||
rangeerr = 0
|
||
else:
|
||
rangeerr = 1
|
||
xreg = np.array([])
|
||
yreg = np.array([])
|
||
|
||
if not rangeerr:
|
||
x0, x0r, alpha0, _, _ = x0_x0r_reg_sub(xreg, yreg, 0, 0)
|
||
else:
|
||
x0 = 0.0
|
||
x0r = 0.0
|
||
alpha0 = 0.0
|
||
|
||
if (x0 == 0) or rangeerr:
|
||
x0 = para_static['x0']
|
||
x0r = para_static['x0r']
|
||
alpha0 = 0.0
|
||
|
||
regsubres['x0'] = x0
|
||
regsubres['x0r'] = x0r
|
||
regsubres['alpha0'] = alpha0
|
||
else:
|
||
x0 = para_static.get('reg_sub', {}).get('x0', para_static['x0'])
|
||
x0r = para_static.get('reg_sub', {}).get('x0r', para_static['x0r'])
|
||
alpha0 = para_static.get('reg_sub', {}).get('alpha0', 0.0)
|
||
|
||
elif mode == 6:
|
||
x1 = para_dyn[0]
|
||
x1r = para_dyn[1]
|
||
|
||
if yactual is not None:
|
||
yactual = np.asarray(yactual, dtype=np.float64)
|
||
minx = np.min(x)
|
||
maxx = np.max(x)
|
||
dx = (maxx - minx) / (len(x) - 1)
|
||
xmitt = 0.5 * (para_static['x0'] + para_static['x0r'])
|
||
if (x1 > minx) and (x1 <= maxx):
|
||
startind = 0
|
||
stopind = int(ind((x1 - 0.5 * para_static.get('filtlength', 0)), dx, x[0], len(x)))
|
||
xright = x[stopind - 1] + 2 * (xmitt - x[stopind - 1])
|
||
xreg = np.concatenate([x[startind:stopind], [xright]])
|
||
yreg = np.concatenate([yactual[startind:stopind], [yactual[stopind - 1]]])
|
||
rangeerr = 0
|
||
x1_inRange = 1
|
||
elif (x1r > minx) and (x1r <= maxx):
|
||
startind = int(ind((x1r + 0.5 * para_static.get('filtlength', 0)), dx, x[0], len(x)))
|
||
stopind = len(x)
|
||
xleft = x[startind - 1] - 2 * (x[startind - 1] - xmitt)
|
||
xreg = np.concatenate([[xleft], x[startind - 1:stopind]])
|
||
yreg = np.concatenate([[yactual[startind - 1]], yactual[startind - 1:stopind]])
|
||
rangeerr = 0
|
||
x1_inRange = 0
|
||
else:
|
||
rangeerr = 1
|
||
x1_inRange = 0
|
||
xreg = np.array([])
|
||
yreg = np.array([])
|
||
|
||
if not rangeerr:
|
||
x0, x0r, alpha0, _, _ = x0_x0r_reg_sub(xreg, yreg, 0, 0)
|
||
else:
|
||
x0 = 0.0
|
||
x0r = 0.0
|
||
alpha0 = 0.0
|
||
|
||
alpha1 = 0.0
|
||
if (not rangeerr) and not (x0 == 0):
|
||
if x1_inRange:
|
||
startind = int(ind((x1 + 2 * dx + 0.5 * para_static.get('filtlength', 0)), dx, x[0], len(x)))
|
||
stopx = min([maxx, (x1r - dx)])
|
||
stopind = int(ind(stopx, dx, x[0], len(x)))
|
||
else:
|
||
startx = max([minx, (x1 + dx)])
|
||
startind = int(ind(startx, dx, x[0], len(x)))
|
||
stopind = int(ind((x1r - 2 * dx - 0.5 * para_static.get('filtlength', 0)), dx, x[0], len(x)))
|
||
|
||
xreg2 = x[startind - 1:stopind]
|
||
yreg2 = yactual[startind - 1:stopind]
|
||
if len(xreg2) > 0:
|
||
s1 = strng2(xreg2, x0, x0r, 1)
|
||
s2 = strng2(xreg2, x1, x1r, 1)
|
||
if (x1r > x1):
|
||
alpha1vek = -(np.log(yreg2) + alpha0 * (s1 - s2)) / s2
|
||
valid = ~np.isinf(alpha1vek)
|
||
if np.any(valid):
|
||
alpha1 = np.mean(alpha1vek[valid])
|
||
rangeerr2 = 0
|
||
else:
|
||
alpha1 = 0.0
|
||
rangeerr2 = 1
|
||
else:
|
||
rangeerr2 = 1
|
||
else:
|
||
rangeerr2 = 1
|
||
|
||
if (x0 == 0) or rangeerr:
|
||
x0 = para_static['x0']
|
||
x0r = para_static['x0r']
|
||
alpha0 = 0.0
|
||
alpha1 = 0.0
|
||
elif rangeerr2:
|
||
alpha1 = 0.0
|
||
|
||
regsubres['x0'] = x0
|
||
regsubres['x0r'] = x0r
|
||
regsubres['alpha0'] = alpha0
|
||
regsubres['alpha1'] = alpha1
|
||
else:
|
||
x0 = para_static.get('reg_sub', {}).get('x0', para_static['x0'])
|
||
x0r = para_static.get('reg_sub', {}).get('x0r', para_static['x0r'])
|
||
alpha0 = para_static.get('reg_sub', {}).get('alpha0', 0.0)
|
||
alpha1 = para_static.get('reg_sub', {}).get('alpha1', 0.0)
|
||
|
||
elif mode == 7:
|
||
alpha0 = para_dyn[0]
|
||
x1 = para_dyn[1]
|
||
x1r = para_dyn[2]
|
||
alpha1 = para_dyn[3]
|
||
x2 = para_dyn[4]
|
||
x2r = para_dyn[5]
|
||
alpha2 = para_dyn[6]
|
||
x0 = para_static['x0']
|
||
x0r = para_static['x0r']
|
||
else:
|
||
alpha0 = 0.0
|
||
x1 = 0.0
|
||
x1r = 0.0
|
||
alpha1 = 0.0
|
||
x0 = para_static.get('x0', 0)
|
||
x0r = para_static.get('x0r', 0)
|
||
|
||
s1 = strng2(x, x0, x0r, 1)
|
||
s2 = strng2(x, x1, x1r, 1)
|
||
if mode in [1, 4, 5]:
|
||
y = np.exp(-(alpha0 * (s1 - s2)))
|
||
elif mode == 7:
|
||
s3 = strng2(x, x2, x2r, 1)
|
||
y = np.exp(-(alpha0 * (s1 - s2) + alpha1 * (s2 - s3) + alpha2 * s3))
|
||
else:
|
||
y = np.exp(-(alpha0 * (s1 - s2) + alpha1 * s2))
|
||
|
||
N2 = 0
|
||
if para_static.get('filtactive', 0):
|
||
dx = x[1] - x[0]
|
||
# 从para_static获取原始filtlength(mm),每次都重新转换为像素
|
||
# 匹配MATLAB值传递语义:每次调用filtlength都是原始mm值
|
||
filtlen_orig = para_static.get('filtlength_mm', para_static.get('filtlength', 0))
|
||
filtlen = int(np.round(filtlen_orig / dx))
|
||
if filtlen % 2 == 0:
|
||
filtlen = filtlen + 1
|
||
y, N2 = filter2k3((1.0 / filtlen) * np.ones(filtlen), y)
|
||
|
||
startind = N2
|
||
stopind = len(x) - N2
|
||
|
||
if para_static.get('lsqn', 0) == 1:
|
||
if yactual is not None:
|
||
yactual = np.asarray(yactual, dtype=np.float64)
|
||
y = y[startind:stopind] - yactual[startind:stopind]
|
||
else:
|
||
xout = x[startind:stopind]
|
||
y = y[startind:stopind]
|
||
|
||
return y, regsubres, xout
|
||
|
||
|
||
def x0_x0r_curvefit2(x, y, fun_startpara, fun_para_static, modul_para):
|
||
x = np.asarray(x, dtype=np.float64)
|
||
y = np.asarray(y, dtype=np.float64)
|
||
|
||
# 保存原始filtlength(mm),避免Python引用传递导致的重复转换问题
|
||
if 'filtlength_mm' not in fun_para_static:
|
||
fun_para_static['filtlength_mm'] = fun_para_static.get('filtlength', 0)
|
||
|
||
dataout = {
|
||
'left': {
|
||
'pos': {'result': np.nan, 'start': np.nan,
|
||
'limit': {'left': np.nan, 'right': np.nan}},
|
||
'att': {'outer': np.nan, 'inner': np.nan},
|
||
'error': np.array([np.nan, np.nan])
|
||
},
|
||
'right': {
|
||
'pos': {'result': np.nan, 'start': np.nan,
|
||
'limit': {'left': np.nan, 'right': np.nan}},
|
||
'att': {'outer': np.nan, 'inner': np.nan},
|
||
'error': np.array([np.nan, np.nan])
|
||
}
|
||
}
|
||
|
||
errorlevel = np.zeros((2, 2), dtype=np.float64)
|
||
calcpara = np.zeros(6, dtype=np.float64)
|
||
calcpara1 = np.zeros(6, dtype=np.float64)
|
||
calcpara2 = np.zeros(6, dtype=np.float64)
|
||
|
||
dx = x[1] - x[0]
|
||
diffxt1 = 0.2
|
||
diffxt2 = 0.10
|
||
|
||
tol_fun = modul_para.get('TolFun', 1e-6)
|
||
tol_x = modul_para.get('TolX', 1e-6)
|
||
max_iter = int(modul_para.get('MaxFunEvals', 400))
|
||
|
||
if 'xlr41det' not in fun_para_static:
|
||
fun_para_static['xlr41det'] = 0
|
||
if len(fun_startpara) < 5:
|
||
fun_para_static['xlr41det'] = 0
|
||
|
||
N = len(x)
|
||
|
||
leftcut = fun_startpara[0] - modul_para['range_outer'] - fun_para_static['filtlength']
|
||
if leftcut < (modul_para['x0limit_l'] - fun_para_static['filtlength']):
|
||
leftcut = modul_para['x0limit_l'] - fun_para_static['filtlength']
|
||
|
||
if fun_para_static['xlr41det']:
|
||
rightcut = fun_startpara[3] + modul_para['range_inner'] + fun_para_static['filtlength']
|
||
else:
|
||
rightcut = fun_startpara[0] + modul_para['range_inner'] + fun_para_static['filtlength']
|
||
if rightcut > (modul_para['x0limit_r'] + fun_para_static['filtlength']):
|
||
rightcut = modul_para['x0limit_r'] + fun_para_static['filtlength']
|
||
|
||
ll = int(ind(leftcut, dx, 0, N))
|
||
rr = int(ind(rightcut, dx, 0, N))
|
||
xt = x[ll - 1:rr]
|
||
yt = y[ll - 1:rr]
|
||
|
||
if len(xt) > 10:
|
||
if fun_para_static['xlr41det'] != 2:
|
||
fun_startpara[2] = attcalcx0(xt, yt, fun_startpara[0], fun_startpara[1], 0)
|
||
|
||
fun_para_static['lsqn'] = 1
|
||
|
||
try:
|
||
result = least_squares(lambda p: x0fun(p, fun_para_static, xt, yt)[0],
|
||
fun_startpara, method='lm',
|
||
ftol=tol_fun, xtol=tol_x, max_nfev=max_iter)
|
||
calcpara1 = result.x
|
||
resnorm = np.sum(result.fun ** 2)
|
||
exitflag = 1 if result.success else -1
|
||
except Exception as e:
|
||
import traceback
|
||
traceback.print_exc()
|
||
calcpara1 = fun_startpara
|
||
resnorm = 1e10
|
||
exitflag = -1
|
||
|
||
calcval = {'alpha0': 0, 'alpha1': calcpara1[2], 'x': calcpara1[0]}
|
||
dataout['left']['pos']['result'] = calcval['x']
|
||
dataout['left']['att']['outer'] = calcval['alpha0']
|
||
dataout['left']['att']['inner'] = calcval['alpha1']
|
||
|
||
minxt = np.min(xt)
|
||
maxxt = np.max(xt)
|
||
errorlevel[:, 0] = edge_error2(calcval, exitflag, resnorm, np.mean(yt), 0,
|
||
minxt, maxxt, modul_para['minatt1'], modul_para['maxatt1'],
|
||
modul_para['mincontr'], modul_para['maxcontr'], 1,
|
||
modul_para['resmax'], diffxt1, diffxt2, len(yt))
|
||
else:
|
||
errorlevel[0, 0] = 50
|
||
|
||
if fun_para_static['xlr41det'] == 1:
|
||
leftcut = fun_startpara[4] - modul_para['range_inner'] - fun_para_static['filtlength']
|
||
else:
|
||
leftcut = fun_startpara[1] - modul_para['range_inner'] - fun_para_static['filtlength']
|
||
if leftcut < (modul_para['x0rlimit_l'] - fun_para_static['filtlength']):
|
||
leftcut = modul_para['x0rlimit_l'] - fun_para_static['filtlength']
|
||
|
||
rightcut = fun_startpara[1] + modul_para['range_outer'] + fun_para_static['filtlength']
|
||
if rightcut > (modul_para['x0rlimit_r'] + fun_para_static['filtlength']):
|
||
rightcut = modul_para['x0rlimit_r'] + fun_para_static['filtlength']
|
||
|
||
ll = int(ind(leftcut, dx, 0, N))
|
||
rr = int(ind(rightcut, dx, 0, N))
|
||
xt = x[ll - 1:rr]
|
||
yt = y[ll - 1:rr]
|
||
|
||
|
||
if len(xt) > 10:
|
||
if fun_para_static['xlr41det'] != 2:
|
||
fun_startpara[2] = attcalcx0(xt, yt, fun_startpara[0], fun_startpara[1], 1)
|
||
|
||
fun_para_static['lsqn'] = 1
|
||
try:
|
||
result = least_squares(lambda p: x0fun(p, fun_para_static, xt, yt)[0],
|
||
fun_startpara, method='lm',
|
||
ftol=tol_fun, xtol=tol_x, max_nfev=max_iter)
|
||
calcpara2 = result.x
|
||
resnorm = np.sum(result.fun ** 2)
|
||
exitflag = 1 if result.success else -1
|
||
except Exception as e:
|
||
import traceback
|
||
traceback.print_exc()
|
||
calcpara2 = fun_startpara
|
||
resnorm = 1e10
|
||
exitflag = -1
|
||
|
||
calcval = {'alpha0': 0, 'alpha1': calcpara2[2], 'x': calcpara2[1]}
|
||
dataout['right']['pos']['result'] = calcval['x']
|
||
dataout['right']['att']['outer'] = calcval['alpha0']
|
||
dataout['right']['att']['inner'] = calcval['alpha1']
|
||
|
||
minxt = np.min(xt)
|
||
maxxt = np.max(xt)
|
||
errorlevel[:, 1] = edge_error2(calcval, exitflag, resnorm, np.mean(yt), 0,
|
||
minxt, maxxt, modul_para['minatt1'], modul_para['maxatt1'],
|
||
modul_para['mincontr'], modul_para['maxcontr'], 1,
|
||
modul_para['resmax'], diffxt1, diffxt2, len(yt))
|
||
else:
|
||
errorlevel[0, 1] = 50
|
||
|
||
if (errorlevel[0, 0] == 0) and (errorlevel[0, 1] == 0):
|
||
calcpara[0] = calcpara1[0]
|
||
calcpara[1] = calcpara2[1]
|
||
calcpara[2] = 0.5 * (calcpara1[2] + calcpara2[2])
|
||
elif errorlevel[0, 0] == 0:
|
||
calcpara[0] = calcpara1[0]
|
||
calcpara[2] = calcpara1[2]
|
||
elif errorlevel[0, 1] == 0:
|
||
calcpara[1] = calcpara2[1]
|
||
calcpara[2] = calcpara2[2]
|
||
|
||
if fun_para_static['xlr41det'] == 1:
|
||
calcpara = np.resize(calcpara, 6)
|
||
calcpara[3] = calcpara1[3]
|
||
calcpara[4] = calcpara2[4]
|
||
elif fun_para_static['xlr41det'] == 2:
|
||
calcpara = np.resize(calcpara, 6)
|
||
calcpara[3] = calcpara1[3]
|
||
calcpara[4] = calcpara2[4]
|
||
if len(calcpara1) > 5 and len(calcpara2) > 5:
|
||
calcpara[5] = 0.5 * (calcpara1[5] + calcpara2[5])
|
||
|
||
return calcpara, errorlevel, dataout
|
||
|
||
|
||
def x1_x1r_curvefit3(x, y, fun_para_static, modul_para):
|
||
x = np.asarray(x, dtype=np.float64)
|
||
y = np.asarray(y, dtype=np.float64)
|
||
|
||
# 保存原始filtlength(mm),避免Python引用传递导致的重复转换问题
|
||
if 'filtlength_mm' not in fun_para_static:
|
||
fun_para_static['filtlength_mm'] = fun_para_static.get('filtlength', 0)
|
||
|
||
errorlevel = np.zeros((2, 2), dtype=np.float64)
|
||
calcpara = np.zeros(4, dtype=np.float64)
|
||
dataout = {
|
||
'alpha0': 0, 'x1': 0, 'x1r': 0, 'alpha1': 0,
|
||
'left': {
|
||
'pos': {'result': np.nan, 'start': np.nan,
|
||
'limit': {'left': np.nan, 'right': np.nan}},
|
||
'att': {'outer': np.nan, 'inner': np.nan},
|
||
'error': np.array([np.nan, np.nan])
|
||
},
|
||
'right': {
|
||
'pos': {'result': np.nan, 'start': np.nan,
|
||
'limit': {'left': np.nan, 'right': np.nan}},
|
||
'att': {'outer': np.nan, 'inner': np.nan},
|
||
'error': np.array([np.nan, np.nan])
|
||
}
|
||
}
|
||
|
||
dx = x[1] - x[0]
|
||
N = len(x)
|
||
|
||
tol_fun = modul_para.get('TolFun', 1e-6)
|
||
tol_x = modul_para.get('TolX', 1e-6)
|
||
max_iter = int(modul_para.get('MaxFunEvals', 400))
|
||
contrsign = modul_para.get('contrsign', 0)
|
||
|
||
leftcut = fun_para_static['x1'] - modul_para['range_outer'] - fun_para_static['filtlength']
|
||
if leftcut < (modul_para['x1limit_l'] - fun_para_static['filtlength']):
|
||
leftcut = modul_para['x1limit_l'] - fun_para_static['filtlength']
|
||
rightcut = fun_para_static['x1'] + modul_para['range_inner'] + fun_para_static['filtlength']
|
||
if rightcut > modul_para['x1limit_r'] + fun_para_static['filtlength']:
|
||
rightcut = modul_para['x1limit_r'] + fun_para_static['filtlength']
|
||
|
||
|
||
ll = int(ind(leftcut, dx, 0, N))
|
||
rr = int(ind(rightcut, dx, 0, N))
|
||
xt = x[ll - 1:rr]
|
||
yt = y[ll - 1:rr]
|
||
|
||
dataoutleft = None
|
||
if len(xt) > 10:
|
||
att0, att1 = attcalcx1(xt, yt, fun_para_static['x0'], fun_para_static['x0r'],
|
||
fun_para_static['x1'], fun_para_static['x1r'], 0)
|
||
|
||
fun_startpara = x1fun3setup(fun_para_static, att0, att1)
|
||
|
||
fun_para_static['lsqn'] = 1
|
||
|
||
try:
|
||
result = least_squares(lambda p: x1fun3(p, fun_para_static, xt, yt)[0],
|
||
fun_startpara, method='lm',
|
||
ftol=tol_fun, xtol=tol_x, max_nfev=max_iter)
|
||
calcpara_left = result.x
|
||
resnorm = np.sum(result.fun ** 2)
|
||
exitflag = 1 if result.success else -1
|
||
except Exception:
|
||
calcpara_left = fun_startpara
|
||
resnorm = 1e10
|
||
exitflag = -1
|
||
|
||
_, fun_para_static['reg_sub'], _ = x1fun3(calcpara_left, fun_para_static, xt, yt)
|
||
|
||
dataoutleft = x1fun3out(calcpara_left, fun_para_static)
|
||
|
||
calcval = {'alpha0': dataoutleft['alpha0'], 'alpha1': dataoutleft['alpha1'],
|
||
'x': dataoutleft['x1']}
|
||
dataout['left']['pos']['result'] = calcval['x']
|
||
dataout['left']['att']['inner'] = calcval['alpha1']
|
||
dataout['left']['att']['outer'] = calcval['alpha0']
|
||
|
||
minxt = np.min(xt)
|
||
maxxt = np.max(xt)
|
||
errorlevel[:, 0] = edge_error2(calcval, exitflag, resnorm, np.mean(yt), att0,
|
||
minxt, maxxt, modul_para['minatt1'], modul_para['maxatt1'],
|
||
modul_para['mincontr'], modul_para['maxcontr'], contrsign,
|
||
modul_para['resmax'], 0.1 * (maxxt - minxt),
|
||
0.05 * (maxxt - minxt), len(yt))
|
||
else:
|
||
errorlevel[0, 0] = 50
|
||
|
||
if fun_para_static.get('mode', 0) == 2:
|
||
fun_para_static['mode'] = 3
|
||
|
||
leftcut = fun_para_static['x1r'] - modul_para['range_inner'] - fun_para_static['filtlength']
|
||
if leftcut < (modul_para['x1rlimit_l'] - fun_para_static['filtlength']):
|
||
leftcut = modul_para['x1rlimit_l'] - fun_para_static['filtlength']
|
||
rightcut = fun_para_static['x1r'] + modul_para['range_outer'] + fun_para_static['filtlength']
|
||
if rightcut > modul_para['x1rlimit_r'] + fun_para_static['filtlength']:
|
||
rightcut = modul_para['x1rlimit_r'] + fun_para_static['filtlength']
|
||
|
||
|
||
ll = int(ind(leftcut, dx, 0, N))
|
||
rr = int(ind(rightcut, dx, 0, N))
|
||
xt = x[ll - 1:rr]
|
||
yt = y[ll - 1:rr]
|
||
|
||
dataoutright = None
|
||
if len(xt) > 10:
|
||
att0, att1 = attcalcx1(xt, yt, fun_para_static['x0'], fun_para_static['x0r'],
|
||
fun_para_static['x1'], fun_para_static['x1r'], 1)
|
||
|
||
fun_startpara = x1fun3setup(fun_para_static, att0, att1)
|
||
|
||
fun_para_static['lsqn'] = 1
|
||
try:
|
||
result = least_squares(lambda p: x1fun3(p, fun_para_static, xt, yt)[0],
|
||
fun_startpara, method='lm',
|
||
ftol=tol_fun, xtol=tol_x, max_nfev=max_iter)
|
||
calcpara_right = result.x
|
||
resnorm = np.sum(result.fun ** 2)
|
||
exitflag = 1 if result.success else -1
|
||
except Exception:
|
||
calcpara_right = fun_startpara
|
||
resnorm = 1e10
|
||
exitflag = -1
|
||
|
||
_, fun_para_static['reg_sub'], _ = x1fun3(calcpara_right, fun_para_static, xt, yt)
|
||
|
||
dataoutright = x1fun3out(calcpara_right, fun_para_static)
|
||
|
||
calcval = {'alpha0': dataoutright['alpha0'], 'alpha1': dataoutright['alpha1'],
|
||
'x': dataoutright['x1r']}
|
||
dataout['right']['pos']['result'] = calcval['x']
|
||
dataout['right']['att']['inner'] = calcval['alpha1']
|
||
dataout['right']['att']['outer'] = calcval['alpha0']
|
||
|
||
minxt = np.min(xt)
|
||
maxxt = np.max(xt)
|
||
errorlevel[:, 1] = edge_error2(calcval, exitflag, resnorm, np.mean(yt), att0,
|
||
minxt, maxxt, modul_para['minatt1'], modul_para['maxatt1'],
|
||
modul_para['mincontr'], modul_para['maxcontr'], contrsign,
|
||
modul_para['resmax'], 0.1 * (maxxt - minxt),
|
||
0.05 * (maxxt - minxt), len(yt))
|
||
else:
|
||
errorlevel[0, 1] = 50
|
||
|
||
if (errorlevel[0, 0] == 0) and (errorlevel[0, 1] == 0):
|
||
dataout['alpha0'] = 0.5 * (dataoutleft['alpha0'] + dataoutright['alpha0'])
|
||
dataout['x1'] = dataoutleft['x1']
|
||
dataout['x1r'] = dataoutright['x1r']
|
||
dataout['alpha1'] = 0.5 * (dataoutleft['alpha1'] + dataoutright['alpha1'])
|
||
elif errorlevel[0, 0] == 0:
|
||
dataout['alpha0'] = dataoutleft['alpha0']
|
||
dataout['x1'] = dataoutleft['x1']
|
||
dataout['x1r'] = 0
|
||
dataout['alpha1'] = dataoutleft['alpha1']
|
||
elif errorlevel[0, 1] == 0:
|
||
dataout['alpha0'] = dataoutright['alpha0']
|
||
dataout['x1'] = 0
|
||
dataout['x1r'] = dataoutright['x1r']
|
||
dataout['alpha1'] = dataoutright['alpha1']
|
||
|
||
return dataout, errorlevel
|
||
|
||
|
||
def circlepos(d, strng):
|
||
dist = 0.0
|
||
if strng <= d:
|
||
dist = 0.5 * d + np.sqrt(0.25 * d ** 2 - (strng ** 2 / 4))
|
||
dist = d - dist
|
||
return dist
|
||
|
||
|
||
def linreg(IN, Dl, Dr, dx, alles, xshift=0):
|
||
IN = np.asarray(IN, dtype=np.float64)
|
||
|
||
ll = int(ind(Dl, dx, xshift))
|
||
rr = int(ind(Dr, dx, xshift))
|
||
rechtsx = (ll - 1) * dx + xshift
|
||
N = 2 * int(np.floor((rr - ll + 1) / 2))
|
||
|
||
Indiffv = np.zeros(N, dtype=np.float64)
|
||
x = np.zeros(N, dtype=np.float64)
|
||
Indiffv[0:N] = IN[np.arange(N) + ll - 1]
|
||
x[0:N] = np.arange(N) * dx
|
||
|
||
A = polyfit2(x, Indiffv, 1)
|
||
|
||
reg = np.zeros(N, dtype=np.float64)
|
||
Indiff = np.zeros(N, dtype=np.float64)
|
||
reg[0:N] = A[1] + A[0] * dx * np.arange(N)
|
||
Indiff[0:N] = Indiffv[0:N] - reg[0:N]
|
||
|
||
Ref = Indiffv[int(N / 2)]
|
||
|
||
return Indiff, rechtsx, Ref
|
||
|
||
|
||
def qcheck(Indiff, dx, Ref, klevel, Anzpara):
|
||
Indiff = np.asarray(Indiff, dtype=np.float64)
|
||
|
||
errorlevel = np.zeros(2, dtype=np.float64)
|
||
N = len(Indiff)
|
||
|
||
x = np.arange(N) * dx
|
||
A = polyfit2(x, Indiff, 2)
|
||
|
||
Scheitelx = -A[1] / (2 * A[0])
|
||
if Scheitelx > N * dx:
|
||
errorlevel[1] = 11
|
||
if Scheitelx < 0:
|
||
errorlevel[1] = 11
|
||
|
||
if abs(A[0]) > 0:
|
||
Scheitely = A[2] - ((A[1] ** 2) / (4 * A[0]))
|
||
diff1 = abs(np.polyval(A, 0) - Scheitely)
|
||
diff2 = abs(np.polyval(A, N * dx) - Scheitely)
|
||
diff = (diff1 + diff2) / 2
|
||
Kontrast = diff * 1 / (N * dx)
|
||
if Kontrast < klevel:
|
||
errorlevel[0] = 11
|
||
else:
|
||
errorlevel[0] = 11
|
||
|
||
Maximum = (A[0] < 0)
|
||
|
||
return errorlevel, Maximum
|
||
|
||
|
||
def _lfilter_fifi(b, a, x, zi):
|
||
b = np.asarray(b, dtype=np.float64)
|
||
a = np.asarray(a, dtype=np.float64)
|
||
x = np.asarray(x, dtype=np.float64)
|
||
y = np.zeros_like(x)
|
||
m = len(b)
|
||
n = len(a)
|
||
nfilt = max(m, n)
|
||
z = np.asarray(zi, dtype=np.float64).copy()
|
||
if len(z) < nfilt - 1:
|
||
z = np.pad(z, (0, nfilt - 1 - len(z)))
|
||
|
||
for i in range(len(x)):
|
||
y[i] = b[0] * x[i] + z[0]
|
||
for j in range(1, nfilt):
|
||
bj = b[j] if j < m else 0.0
|
||
aj = a[j] if j < n else 0.0
|
||
if j < nfilt - 1:
|
||
z[j - 1] = bj * x[i] + z[j] - aj * y[i]
|
||
else:
|
||
z[j - 1] = bj * x[i] - aj * y[i]
|
||
|
||
return y
|
||
|
||
|
||
def fifi(b, a, x):
|
||
b = np.asarray(b, dtype=np.float64).ravel()
|
||
a = np.asarray(a, dtype=np.float64).ravel()
|
||
x = np.asarray(x, dtype=np.float64)
|
||
|
||
if len(b) == 0 or len(a) == 0 or len(x) == 0:
|
||
return np.array([])
|
||
|
||
orig_shape = x.shape
|
||
if x.ndim > 1:
|
||
x = x.ravel()
|
||
|
||
len_x = len(x)
|
||
nb = len(b)
|
||
na = len(a)
|
||
nfilt = max(nb, na)
|
||
nfact = 3 * (nfilt - 1)
|
||
|
||
if nb < nfilt:
|
||
b = np.pad(b, (0, nfilt - nb))
|
||
if na < nfilt:
|
||
a = np.pad(a, (0, nfilt - na))
|
||
|
||
if nfilt > 1:
|
||
A_mat = np.eye(nfilt - 1)
|
||
col = -a[1:nfilt]
|
||
B_mat = np.zeros((nfilt - 1, nfilt - 1))
|
||
B_mat[0, :] = col
|
||
if nfilt > 2:
|
||
B_mat[1:, :-1] = np.eye(nfilt - 2)
|
||
zi = np.linalg.solve(A_mat - B_mat, b[1:nfilt] - a[1:nfilt] * b[0])
|
||
else:
|
||
zi = np.array([])
|
||
|
||
y_pad = np.concatenate([
|
||
2 * x[0] - x[nfact:0:-1],
|
||
x,
|
||
2 * x[-1] - x[-2:-nfact - 2:-1]
|
||
])
|
||
|
||
y1 = _lfilter_fifi(b, a, y_pad, zi * y_pad[0])
|
||
y1 = y1[::-1]
|
||
y2 = _lfilter_fifi(b, a, y1, zi * y1[0])
|
||
y2 = y2[::-1]
|
||
|
||
y_out = y2[nfact:len_x + nfact]
|
||
|
||
if len(orig_shape) > 1:
|
||
y_out = y_out.reshape(orig_shape)
|
||
|
||
return y_out
|
||
|
||
|
||
def minmaxall(x):
|
||
x = np.asarray(x, dtype=np.float64).ravel()
|
||
|
||
Nt = len(x)
|
||
inan = np.where(np.isnan(x))[0]
|
||
indx = np.arange(Nt)
|
||
if len(inan) > 0:
|
||
indx = np.delete(indx, inan)
|
||
x = np.delete(x, inan)
|
||
Nt = len(x)
|
||
|
||
dx = np.diff(x)
|
||
b = (dx > 0).astype(int)
|
||
db = np.diff(b)
|
||
Ind_max = np.where(db == -1)[0] + 1
|
||
Ind_min = np.where(db == 1)[0] + 1
|
||
Val_max = x[Ind_max]
|
||
Val_min = x[Ind_min]
|
||
|
||
Ind_max = Ind_max[np.argsort(-Ind_max)]
|
||
Val_max = Val_max[np.argsort(-Ind_max)]
|
||
Ind_min = Ind_min[np.argsort(-Ind_min)]
|
||
Val_min = Val_min[np.argsort(-Ind_min)]
|
||
|
||
return Val_max, Ind_max, Val_min, Ind_min
|
||
|
||
|
||
def errfunc(errorl, errorlevel):
|
||
errorl = np.asarray(errorl, dtype=np.float64).ravel()
|
||
errorlevel = np.asarray(errorlevel, dtype=np.float64).ravel()
|
||
|
||
if (errorlevel[0] == 0) and (errorl[0] > 0):
|
||
errorlevel[0] = errorl[0]
|
||
if len(errorlevel) > 1 and len(errorl) > 1:
|
||
if (errorlevel[1] == 0) and (errorl[1] > 0):
|
||
errorlevel[1] = errorl[1]
|
||
|
||
return errorlevel
|
||
|
||
|
||
def lincorn(IN, conf):
|
||
IN = np.asarray(IN, dtype=np.float64)
|
||
conf = np.asarray(conf, dtype=np.float64)
|
||
|
||
x = 0.0
|
||
errorlevel = np.zeros(2, dtype=np.float64)
|
||
|
||
Dl = conf[0]
|
||
Dr = conf[1]
|
||
Limitl = conf[2]
|
||
Limitr = conf[3]
|
||
fg1 = conf[4]
|
||
sym = conf[5]
|
||
Centerdiff = conf[6]
|
||
Itermax = conf[7]
|
||
dx = conf[8]
|
||
klevel = conf[9]
|
||
alles = conf[10]
|
||
fakfg = conf[11]
|
||
Maximumc = conf[12]
|
||
|
||
right = None
|
||
if len(conf) >= 14:
|
||
right = conf[13]
|
||
|
||
errorl = np.zeros(2, dtype=np.float64)
|
||
stop = 0
|
||
loop = 0
|
||
xalt = Dl + (Dr - Dl) / 2
|
||
|
||
fg = fg1 * fakfg
|
||
fgn = 2 * fg * dx
|
||
try:
|
||
from .filtering_utils import buttmat
|
||
except ImportError:
|
||
from filtering_utils import buttmat
|
||
BFilt1, AFilt1, _ = buttmat(fgn)
|
||
|
||
fgn = 2 * fg1 * dx
|
||
BFilt2, AFilt2, _ = buttmat(fgn)
|
||
|
||
ind_Dl = int(ind(Dl, dx, 0, len(IN)))
|
||
ind_Dr = int(ind(Dr, dx, 0, len(IN)))
|
||
range_ind = ind_Dr - ind_Dl
|
||
if range_ind > 5:
|
||
while stop == 0:
|
||
loop = loop + 1
|
||
|
||
Indiff, rechtsx, Ref = linreg(IN, Dl, Dr, dx, alles)
|
||
|
||
err_q, Maximumq = qcheck(Indiff, dx, Ref, klevel, alles)
|
||
errorlevel[0] = err_q[0]
|
||
errorlevel[1] = err_q[1]
|
||
|
||
if Maximumc == 2:
|
||
Maximum = Maximumq
|
||
else:
|
||
Maximum = Maximumc
|
||
|
||
if loop == 1:
|
||
Maximumloop1 = Maximum
|
||
|
||
if Maximumloop1 != Maximum:
|
||
errorl[0] = 13
|
||
|
||
if loop == 1:
|
||
AFilt = AFilt1
|
||
BFilt = BFilt1
|
||
else:
|
||
AFilt = AFilt2
|
||
BFilt = BFilt2
|
||
|
||
fourindiff = fifi(BFilt, AFilt, Indiff)
|
||
|
||
if right is not None:
|
||
Val_max, Ind_max, Val_min, Ind_min = minmaxall(fourindiff)
|
||
if len(Ind_max) > 0:
|
||
if right:
|
||
x = Ind_max[-1]
|
||
else:
|
||
x = Ind_max[0]
|
||
else:
|
||
try:
|
||
from .extremum_utils import minmax
|
||
except ImportError:
|
||
from extremum_utils import minmax
|
||
mwert, x = minmax(fourindiff, Maximum)
|
||
else:
|
||
try:
|
||
from .extremum_utils import minmax
|
||
except ImportError:
|
||
from extremum_utils import minmax
|
||
mwert, x = minmax(fourindiff, Maximum)
|
||
|
||
x = (x - 1) * dx + rechtsx
|
||
|
||
symnew = sym
|
||
if ((x - Limitl) < symnew):
|
||
symnew = x - Limitl
|
||
if ((Limitr - x) < symnew):
|
||
symnew = Limitr - x
|
||
|
||
Dl = x - symnew
|
||
Dr = x + symnew
|
||
|
||
if ((loop == Itermax) or ((abs(x - xalt) < Centerdiff) and (loop > 1))):
|
||
stop = 1
|
||
if Maximumq != Maximum:
|
||
errorl[0] = 13
|
||
|
||
if (int(ind(Dr, dx, 0, len(IN))) - int(ind(Dl, dx, 0, len(IN)))) < 7:
|
||
stop = 1
|
||
errorl[0] = 12
|
||
|
||
errorlevel = errfunc(errorl, errorlevel)
|
||
xalt = x
|
||
else:
|
||
errorlevel[0] = 12
|
||
|
||
return x, errorlevel
|
||
|
||
|
||
def x1_x1r_curvefit4(x, y, fun_para_static, modul_para):
|
||
x = np.asarray(x, dtype=np.float64)
|
||
y = np.asarray(y, dtype=np.float64)
|
||
|
||
# 保存原始filtlength(mm),避免Python引用传递导致的重复转换问题
|
||
if 'filtlength_mm' not in fun_para_static:
|
||
fun_para_static['filtlength_mm'] = fun_para_static.get('filtlength', 0)
|
||
|
||
errorlevel = np.zeros((2, 2), dtype=np.float64)
|
||
calcpara = np.zeros(4, dtype=np.float64)
|
||
dataout = {
|
||
'alpha0': 0, 'x1': 0, 'x1r': 0, 'alpha1': 0,
|
||
'left': {
|
||
'pos': {'result': np.nan, 'start': np.nan,
|
||
'limit': {'left': np.nan, 'right': np.nan}},
|
||
'att': {'outer': np.nan, 'inner': np.nan},
|
||
'error': np.array([np.nan, np.nan])
|
||
},
|
||
'right': {
|
||
'pos': {'result': np.nan, 'start': np.nan,
|
||
'limit': {'left': np.nan, 'right': np.nan}},
|
||
'att': {'outer': np.nan, 'inner': np.nan},
|
||
'error': np.array([np.nan, np.nan])
|
||
}
|
||
}
|
||
resnorm = np.ones(2, dtype=np.float64)
|
||
|
||
dx = x[1] - x[0]
|
||
N = len(x)
|
||
|
||
tol_fun = modul_para.get('TolFun', 1e-6)
|
||
tol_x = modul_para.get('TolX', 1e-6)
|
||
max_iter = int(modul_para.get('MaxFunEvals', 400))
|
||
contrsign = modul_para.get('contrsign', 0)
|
||
|
||
leftcut = fun_para_static['x1'] - modul_para['range_outer'] - fun_para_static['filtlength']
|
||
if leftcut < (modul_para['x1limit_l'] - fun_para_static['filtlength']):
|
||
leftcut = modul_para['x1limit_l'] - fun_para_static['filtlength']
|
||
rightcut = fun_para_static['x1'] + modul_para['range_inner'] + fun_para_static['filtlength']
|
||
if rightcut > modul_para['x1limit_r'] + fun_para_static['filtlength']:
|
||
rightcut = modul_para['x1limit_r'] + fun_para_static['filtlength']
|
||
|
||
|
||
ll = int(ind(leftcut, dx, 0, N))
|
||
rr = int(ind(rightcut, dx, 0, N))
|
||
xt = x[ll - 1:rr]
|
||
yt = y[ll - 1:rr]
|
||
|
||
dataoutleft = None
|
||
if len(xt) > 10:
|
||
att0, att1 = attcalcx1(xt, yt, fun_para_static['x0'], fun_para_static['x0r'],
|
||
fun_para_static['x1'], fun_para_static['x1r'], 0)
|
||
|
||
fun_startpara = x1fun3setup(fun_para_static, att0, att1)
|
||
|
||
fun_para_static['lsqn'] = 1
|
||
|
||
use_lm = (modul_para.get('LevenbergMarquardt', 0) or
|
||
fun_para_static.get('mode', 0) in (5, 6))
|
||
opt_method = 'lm' if use_lm else 'trf'
|
||
|
||
try:
|
||
result = least_squares(lambda p: x1fun3(p, fun_para_static, xt, yt)[0],
|
||
fun_startpara, method=opt_method,
|
||
ftol=tol_fun, xtol=tol_x, max_nfev=max_iter)
|
||
calcpara_left = result.x
|
||
resnorm[0] = np.sum(result.fun ** 2)
|
||
exitflag = 1 if result.success else -1
|
||
except Exception as e:
|
||
calcpara_left = fun_startpara
|
||
resnorm[0] = 1e10
|
||
exitflag = -1
|
||
|
||
_, fun_para_static['reg_sub'], _ = x1fun3(calcpara_left, fun_para_static, xt, yt)
|
||
|
||
dataoutleft = x1fun3out(calcpara_left, fun_para_static)
|
||
|
||
calcval = {'alpha0': dataoutleft['alpha0'], 'alpha1': dataoutleft['alpha1'],
|
||
'x': dataoutleft['x1']}
|
||
dataout['left']['pos']['result'] = calcval['x']
|
||
dataout['left']['att']['inner'] = calcval['alpha1']
|
||
dataout['left']['att']['outer'] = calcval['alpha0']
|
||
|
||
minxt = np.min(xt)
|
||
maxxt = np.max(xt)
|
||
contrsign = modul_para.get('contrsign', 0)
|
||
errorlevel[:, 0] = edge_error2(calcval, exitflag, resnorm[0], np.mean(yt), att0,
|
||
minxt, maxxt, modul_para['minatt1'], modul_para['maxatt1'],
|
||
modul_para['mincontr'], modul_para['maxcontr'], contrsign,
|
||
modul_para['resmax'], 0.1 * (maxxt - minxt),
|
||
0.05 * (maxxt - minxt), len(yt))
|
||
|
||
# 将左侧拟合得到的x1(即x2左侧)传递给右侧拟合作为固定值
|
||
# 虽然MATLAB原代码未显式更新,但从物理意义和拟合精度考虑需要传递
|
||
if errorlevel[0, 0] == 0 and 'x1' in dataoutleft and np.isfinite(dataoutleft['x1']):
|
||
fun_para_static['x1'] = dataoutleft['x1']
|
||
else:
|
||
errorlevel[0, 0] = 50
|
||
|
||
if fun_para_static.get('mode', 0) == 2:
|
||
fun_para_static['mode'] = 3
|
||
|
||
leftcut = fun_para_static['x1r'] - modul_para['range_inner'] - fun_para_static['filtlength']
|
||
if leftcut < (modul_para['x1rlimit_l'] - fun_para_static['filtlength']):
|
||
leftcut = modul_para['x1rlimit_l'] - fun_para_static['filtlength']
|
||
rightcut = fun_para_static['x1r'] + modul_para['range_outer'] + fun_para_static['filtlength']
|
||
if rightcut > modul_para['x1rlimit_r'] + fun_para_static['filtlength']:
|
||
rightcut = modul_para['x1rlimit_r'] + fun_para_static['filtlength']
|
||
|
||
|
||
ll = int(ind(leftcut, dx, 0, N))
|
||
rr = int(ind(rightcut, dx, 0, N))
|
||
xt = x[ll - 1:rr]
|
||
yt = y[ll - 1:rr]
|
||
|
||
dataoutright = None
|
||
if len(xt) > 10:
|
||
att0, att1 = attcalcx1(xt, yt, fun_para_static['x0'], fun_para_static['x0r'],
|
||
fun_para_static['x1'], fun_para_static['x1r'], 1)
|
||
|
||
fun_startpara = x1fun3setup(fun_para_static, att0, att1)
|
||
|
||
fun_para_static['lsqn'] = 1
|
||
|
||
use_lm_r = (modul_para.get('LevenbergMarquardt', 0) or
|
||
fun_para_static.get('mode', 0) in (5, 6))
|
||
opt_method_r = 'lm' if use_lm_r else 'trf'
|
||
|
||
try:
|
||
result = least_squares(lambda p: x1fun3(p, fun_para_static, xt, yt)[0],
|
||
fun_startpara, method=opt_method_r,
|
||
ftol=tol_fun, xtol=tol_x, max_nfev=max_iter)
|
||
calcpara_right = result.x
|
||
resnorm[1] = np.sum(result.fun ** 2)
|
||
exitflag = 1 if result.success else -1
|
||
except Exception as e:
|
||
calcpara_right = fun_startpara
|
||
resnorm[1] = 1e10
|
||
exitflag = -1
|
||
|
||
_, fun_para_static['reg_sub'], _ = x1fun3(calcpara_right, fun_para_static, xt, yt)
|
||
|
||
dataoutright = x1fun3out(calcpara_right, fun_para_static)
|
||
|
||
calcval = {'alpha0': dataoutright['alpha0'], 'alpha1': dataoutright['alpha1'],
|
||
'x': dataoutright['x1r']}
|
||
dataout['right']['pos']['result'] = calcval['x']
|
||
dataout['right']['att']['inner'] = calcval['alpha1']
|
||
dataout['right']['att']['outer'] = calcval['alpha0']
|
||
|
||
minxt = np.min(xt)
|
||
maxxt = np.max(xt)
|
||
errorlevel[:, 1] = edge_error2(calcval, exitflag, resnorm[1], np.mean(yt), att0,
|
||
minxt, maxxt, modul_para['minatt1'], modul_para['maxatt1'],
|
||
modul_para['mincontr'], modul_para['maxcontr'], contrsign,
|
||
modul_para['resmax'], 0.1 * (maxxt - minxt),
|
||
0.05 * (maxxt - minxt), len(yt))
|
||
else:
|
||
errorlevel[0, 1] = 50
|
||
|
||
if (errorlevel[0, 0] == 0) and (errorlevel[0, 1] == 0):
|
||
dataout['alpha0'] = 0.5 * (dataoutleft['alpha0'] + dataoutright['alpha0'])
|
||
dataout['x1'] = dataoutleft['x1']
|
||
dataout['x1r'] = dataoutright['x1r']
|
||
dataout['alpha1'] = 0.5 * (dataoutleft['alpha1'] + dataoutright['alpha1'])
|
||
elif errorlevel[0, 0] == 0:
|
||
dataout['alpha0'] = dataoutleft['alpha0']
|
||
dataout['x1'] = dataoutleft['x1']
|
||
dataout['x1r'] = 0
|
||
dataout['alpha1'] = dataoutleft['alpha1']
|
||
elif errorlevel[0, 1] == 0:
|
||
dataout['alpha0'] = dataoutright['alpha0']
|
||
dataout['x1'] = 0
|
||
dataout['x1r'] = dataoutright['x1r']
|
||
dataout['alpha1'] = dataoutright['alpha1']
|
||
|
||
return dataout, errorlevel, resnorm
|
||
|
||
|
||
def x1RangeLimiter(x0, x0r, xl41, xr41, NOMOSCL, NOMOSCR, FactorOSC,
|
||
NOMISCL, NOMISCR, FactorINS, CableType):
|
||
try:
|
||
from .math_utils import equal
|
||
except ImportError:
|
||
from math_utils import equal
|
||
|
||
if equal(CableType, 79):
|
||
NOMINSL = 0.5 * ((x0r - x0) - NOMOSCL - NOMOSCR)
|
||
NOMINSR = NOMINSL
|
||
else:
|
||
NOMINSL = (xl41 - x0) - NOMOSCL - NOMISCL
|
||
NOMINSR = (x0r - xr41) - NOMOSCR - NOMISCR
|
||
|
||
x1Limitr = min(x0 + NOMOSCL * FactorOSC, x0 + NOMOSCL + (NOMINSL / FactorINS))
|
||
x1rLimitl = max(x0r - NOMOSCR * FactorOSC, x0r - NOMOSCR - (NOMINSR / FactorINS))
|
||
|
||
return x1Limitr, x1rLimitl
|
||
|
||
|