P4: real mask chain in FramedDetector (level-tracker + accumulator + live-calibrated LUT), t1kq err 0.5dB

Replace empirical PCHIP detector with live-calibrated mask chain from FUN_180529fe0:
  level = am*res*scale; track += w*(level-track) (per-bin attack/release weights
  extracted from RT snapshot, rt_weights.hpp); acc = (level-track)+level; mask =
  (1/(1+K*acc))^n (K=9.8026 n=0.25966 fitted to live mask band0). min-combine.

Results (N=2048 hop=512): t1kq single-band err +0.51 dB (-14.47 vs -14.98 dB ref);
comb 4-band per-tone -3.4..+5.8 dB (old PCHIP over-cut comb ~6 dB). Adds framed_test
harness for C++ FramedDetector eval on tone1kq/comb.
This commit is contained in:
2026-08-20 13:11:27 +03:00
parent 5c939583f5
commit fc48a9fee4
7 changed files with 180 additions and 63 deletions
+28 -54
View File
@@ -1,63 +1,25 @@
#include "framed_model.hpp"
#include "twin.hpp"
#include "freqpath.hpp"
#include "rt_weights.hpp"
#include <cmath>
#include <cstring>
#include <algorithm>
namespace {
constexpr float G_FIT = 0.9963f;
constexpr float W_FIT = 0.3335f;
constexpr float A_FIT = 0.9807f;
// Mask curve fitted against live capture (snap_rt.bin, mask band0 vs acc 0x5407c8):
// mask = (1 / (1 + K*acc))^n, K=9.80, n=0.260 (mse 0.019 over 17 resonant bins)
constexpr double MASK_K = 9.8026;
constexpr double MASK_N = 0.25966;
// sens XML -> internal sens_stored = sens * 2.054 (NOTES_TWIN:74: XML 12 -> 24.65 dB).
// GAIN_band = sqrt(param_5) = 10^(sens_stored/40).
constexpr float SENS_SCALE = 2.054f;
constexpr int RT_WEIGHTS_N = 2049; // captured attack/release tables size
// framed_render.py LUT nodes (Pchip): C = G_FIT*LUT(xv) + W_FIT*warp^A_FIT
constexpr double LX[12] = {-0.75, -0.5012, -0.5, -0.2012, 0.0988, 0.2488,
0.3988, 0.5488, 0.574, 0.61, 0.75, 1.0};
constexpr double LY[12] = {0.4402, 0.366, 0.4552, 0.459, 0.541, 0.576,
0.608, 0.636, 0.5645, 0.6471, 0.6562, 0.6670};
constexpr int LN = 12;
// PchipInterpolator (monotone cubic Hermite, scipy semantics) eval at one x.
double pchip_eval(double x) {
if (x <= LX[0]) return LY[0];
if (x >= LX[LN - 1]) return LY[LN - 1];
// slopes
double m[LN];
for (int i = 0; i < LN - 1; i++) m[i] = (LY[i + 1] - LY[i]) / (LX[i + 1] - LX[i]);
double d[LN];
d[0] = m[0];
for (int i = 1; i < LN - 1; i++) {
if (m[i - 1] * m[i] <= 0.0) d[i] = 0.0;
else {
double w1 = 2 * LX[i + 1] - LX[i] - LX[i - 1];
double w2 = LX[i + 1] - LX[i - 1];
d[i] = (w1 + w2) / ((w1 / m[i - 1]) + (w2 / m[i]));
}
}
d[LN - 1] = m[LN - 2];
int i = 0;
while (i < LN - 2 && x > LX[i + 1]) i++;
double h = LX[i + 1] - LX[i];
double t = (x - LX[i]) / h;
double y0 = LY[i], y1 = LY[i + 1], d0 = d[i], d1 = d[i + 1];
double h00 = 2 * t * t * t - 3 * t * t + 1;
double h10 = t * t * t - 2 * t * t + t;
double h01 = -2 * t * t * t + 3 * t * t;
double h11 = t * t * t - t * t;
double v = h00 * y0 + h10 * h * d0 + h01 * y1 + h11 * h * d1;
return std::max(LY[LN - 2] < LY[LN - 1] ? 0.0 : -1e9,
std::min(v, *std::max_element(LY, LY + LN) * 1.0));
}
double lut_eval(double x) {
double v = pchip_eval(x);
double lo = *std::min_element(LY, LY + LN);
double hi = *std::max_element(LY, LY + LN);
return std::max(lo, std::min(hi, v));
double mask_lut(double acc) {
if (acc <= 0.0) return 1.0;
return std::pow(1.0 / (1.0 + MASK_K * acc), MASK_N);
}
} // namespace
@@ -74,7 +36,7 @@ void FramedDetector::setParams(const std::vector<DetectorBand>& bands) {
bands_ = bands;
size_t half = nfft_ / 2;
res_.clear();
rp_.clear();
track_.clear();
for (const auto& b : bands_) {
std::vector<float> r(half + 1, 1.0f);
@@ -95,8 +57,8 @@ void FramedDetector::setParams(const std::vector<DetectorBand>& bands) {
r[k] = std::max(r[k], 1e-12f);
}
res_.push_back(std::move(r));
rp_.push_back(static_cast<float>(0.0275 * std::pow(static_cast<double>(b.q), 0.2159)));
}
track_.assign(bands_.size(), std::vector<float>(half + 1, 0.0f));
if (warp_[0] == 0.0f && warp_[1] == 0.0f) {
detkernel::build_warp(static_cast<float>(sample_rate_),
@@ -130,11 +92,23 @@ void FramedDetector::processFrame(const std::complex<double>* spectrum, float* m
double am = am_[k];
double gain = 1.0;
for (size_t b = 0; b < bands_.size(); b++) {
double xv = std::log10(std::max(am / static_cast<double>(res_[b][k]), 1e-9));
double C = G_FIT * lut_eval(xv) +
W_FIT * std::pow(static_cast<double>(warp_[k]), A_FIT);
double g = std::max(1.0 - C, 1e-9) *
std::pow(static_cast<double>(res_[b][k]), rp_[b]);
// Level = twin resonance magnitude x smoothed per-bin amplitude.
double level = am * static_cast<double>(res_[b][k]) *
static_cast<double>(bands_[b].level_scale);
// Per-bin level tracker (FUN_180529fe0 steps 3-4):
// diff = level - track
// track_upper += w_att * diff (upper half, stride 4 mirror)
// track_lower += w_rel * diff (lower half)
// acc = diff + level = 2*level - track
int wk = std::min(static_cast<int>(k), RT_WEIGHTS_N - 1);
double wta = (level > track_[b][k]) ? static_cast<double>(kRTAtt[wk])
: static_cast<double>(kRTRel[wk]);
double diff = level - track_[b][k];
double track = track_[b][k] + wta * diff;
track_[b][k] = static_cast<float>(track);
// Accumulator (FUN_180529fe0 step 5: 0x5407c8 = diff; += level).
double acc = diff + level;
double g = mask_lut(acc);
if (g < gain) gain = g;
}
mask[k] = static_cast<float>(gain);