Files
soothe2-re/verify_lut2.py
T

77 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""verify_lut2.py — непараметрическая LUT + свободные tilt/Q/gain. rmse."""
import numpy as np
from scipy.interpolate import PchipInterpolator
from scipy.optimize import least_squares
FS = 44100.0
DEPTH = 0.8639736175537109
QS = [0.1, 0.2, 0.3, 0.5, 0.7, 1.0, 1.5, 2.0, 3.0, 5.0, 10.0]
DUAL = np.array([(10.220, 15.224), (10.219, 13.963), (10.219, 12.947),
(10.219, 11.725), (10.218, 11.119), (10.216, 10.689),
(10.210, 10.412), (10.203, 10.305), (10.182, 10.225),
(10.113, 10.183), (9.822, 10.165)])
FCS = [800.0, 900.0, 950.0, 1000.0, 1050.0, 1100.0, 1200.0]
T1KQ = np.array([7.868, 8.536, 8.726, 8.788, 8.729, 8.575, 8.115])
T1K = np.array([14.548, 15.332, 15.553, 15.626, 15.557, 15.378, 14.840])
L_DUAL = 10 ** (-7.142 / 20)
L_T1KQ = 10 ** (-18.063 / 20)
L_T1K = 1.0
LUTX = np.array([-0.750, -0.500, -0.250, 0.000, 0.250, 0.500, 0.750, 1.000])
LUTY = np.array([0.4453, 0.4551, 0.4784, 0.5041, 0.5331, 0.5715, 0.6574, 0.6636])
lut = PchipInterpolator(LUTX, LUTY)
def res_at(ft, fc, Q, g):
w0 = fc * 2 * np.pi / FS
c, s = np.cos(w0), np.sin(w0)
p = (s * 0.5) / Q
a, a2 = p * g, p / g
A = [a + 1, -2 * c, 1 - a]
B = [a2 + 1, -2 * c, 1 - a2]
w = 2 * np.pi * ft / FS
z = np.exp(-1j * w)
return np.abs(2.0 * (B[0] + B[1] * z + B[2] * z * z) /
(A[0] + A[1] * z + A[2] * z * z))
def model(p):
Q, g, t500, t1000, t2000 = p
out = []
for q in QS:
for f in (500.0, 2000.0):
r = res_at(f, 500, q, g)
tilt = t500 if f < 1000 else t2000
C = DEPTH * tilt * lut(np.log10(L_DUAL / r))
out.append(-20 * np.log10(max(1 - C, 1e-9)))
for i, fc in enumerate(FCS):
r = res_at(1000, fc, 0.9999978, g)
C = DEPTH * t1000 * lut(np.log10(L_T1KQ / r))
out.append(-20 * np.log10(max(1 - C, 1e-9)))
C = DEPTH * t1000 * lut(np.log10(L_T1K / r))
out.append(-20 * np.log10(max(1 - C, 1e-9)))
return np.array(out)
def run():
meas = np.array(list(DUAL.ravel()) + list(T1KQ) + list(T1K))
r = least_squares(lambda p: model(p) - meas, [0.9, 4.13, 1.49, 1.454, 1.795],
bounds=([0.1, 0.5, 0.3, 0.3, 0.3], [5, 12, 5, 5, 5]),
max_nfev=30000, xtol=1e-12, ftol=1e-12)
Q, g, t500, t1000, t2000 = r.x
pred = model(r.x)
rmse = np.sqrt(np.mean((pred - meas) ** 2))
print(f'VERIFY rmse={rmse:.4f} dB Q={Q:.3f} gain={g:.3f}')
print(f'tilt: 500={t500:.3f} 1000={t1000:.3f} 2000={t2000:.3f}')
for i, q in enumerate(QS):
print(f'q={q:5.1f} 500 {DUAL[i,0]:6.3f}/{pred[2*i]:6.3f} '
f'2000 {DUAL[i,1]:6.3f}/{pred[2*i+1]:6.3f}')
for i, fc in enumerate(FCS):
print(f't1kq fc={fc:5.0f} {T1KQ[i]:6.3f}/{pred[22+2*i]:6.3f} | '
f't1k {T1K[i]:6.3f}/{pred[23+2*i]:6.3f}')
if __name__ == '__main__':
run()