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
+3
View File
@@ -22,9 +22,11 @@ add_library(soothe2_dsp SHARED
vlog.cpp
leveltrack.cpp
framed_model.cpp
rt_weights.cpp
)
add_executable(soothe2_harness harness.cpp)
add_executable(framed_test framed_test.cpp)
add_executable(twin_check twin_check.cpp)
add_executable(tables_check tables_check.cpp)
add_executable(fftconv_check fftconv_check.cpp)
@@ -32,6 +34,7 @@ add_executable(vlog_check vlog_check.cpp)
add_executable(leveltrack_check leveltrack_check.cpp)
add_executable(levelpath_check levelpath_check.cpp)
target_link_libraries(twin_check soothe2_dsp)
target_link_libraries(framed_test soothe2_dsp)
target_link_libraries(tables_check soothe2_dsp)
target_link_libraries(fftconv_check soothe2_dsp)
target_link_libraries(vlog_check soothe2_dsp)
+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);
+10 -9
View File
@@ -7,16 +7,17 @@ struct DetectorBand {
float fc; // band center freq (Hz)
float q; // resonance Q
float sens; // XML sens (dB); internal sens_stored = sens * 2.054
float level_scale = 1.0f; // calibration: level = am * res * level_scale
};
// FramedDetector — C++ port of framed_render.py (Phase 5 pilot, mean=0.175 dB).
// Multi-band: each active band contributes gain_k = (1-C_k)*res_k^rp,
// final mask = min over bands (max suppression).
// res_k[f] = |2B(z)/A(z)| (twin resonance, sens-scaled GAIN)
// am = 2|X_k|/wsum (smoothed per-bin amplitude)
// xv_k = log10(am / res_k)
// C_k = G_FIT*LUT(xv_k) + W_FIT*warp(f)^A_FIT
// gain_k = max(1-C_k, eps) * res_k^(rp0*Q_k^drp)
// FramedDetector — C++ port of the real soothe mask chain (FUN_180529fe0).
// Per band, per bin:
// level_k = am_k * res_k (twin resonance x smoothed amplitude)
// track += w_k * (level - track) (per-bin level tracker, attack/release
// weights w from live capture rt_weights)
// acc_k = 2*level_k - track_k (accumulator, live peak ~10.08 at tone)
// mask_k = LUT(acc_k) (fitted mask curve from live capture)
// final mask = min over bands (max suppression), like soothe's band combine.
class FramedDetector {
public:
FramedDetector(size_t nfft, float sample_rate);
@@ -35,5 +36,5 @@ private:
std::vector<std::vector<float>> res_; // per band, per bin |2B/A|
std::vector<float> warp_; // per-bin warp tilt
std::vector<float> am_; // smoothed per-bin amplitude
std::vector<float> rp_; // per-band res power
std::vector<std::vector<float>> track_; // per band, per bin level tracker
};
+101
View File
@@ -0,0 +1,101 @@
// framed_test.cpp — рендер входа через SpectralProcessor (real mask chain).
// Usage: framed_test <input.wav> <output.wav> [fc[,q[,sens]] ...]
#include "spectral.hpp"
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <cstring>
static bool load_wav(const char* path, std::vector<float>& out, int& sr) {
FILE* f = fopen(path, "rb");
if (!f) return false;
char hdr[44];
if (fread(hdr, 1, 44, f) != 44) return false;
sr = *(int*)(hdr + 24);
int ch = *(short*)(hdr + 22);
int bits = *(short*)(hdr + 34);
int data = *(int*)(hdr + 40);
int n = data / (ch * (bits / 8));
std::vector<short> raw(n * ch);
fread(raw.data(), 2, n * ch, f);
fclose(f);
out.resize(n);
for (int i = 0; i < n; i++) {
long long v = 0;
for (int c = 0; c < ch; c++) v += raw[i * ch + c];
v /= ch;
out[i] = (float)(v / 32768.0);
}
return true;
}
static bool save_wav(const char* path, const std::vector<float>& x, int sr) {
FILE* f = fopen(path, "wb");
if (!f) return false;
int data = (int)(x.size() * 2);
char hdr[44];
memset(hdr, 0, 44);
memcpy(hdr, "RIFF", 4);
*(int*)(hdr + 4) = 36 + data;
memcpy(hdr + 8, "WAVE", 4);
memcpy(hdr + 12, "fmt ", 4);
*(int*)(hdr + 16) = 16;
*(short*)(hdr + 20) = 1;
*(short*)(hdr + 22) = 1;
*(int*)(hdr + 24) = sr;
*(int*)(hdr + 28) = sr * 2;
*(short*)(hdr + 32) = 2;
*(short*)(hdr + 34) = 16;
memcpy(hdr + 36, "data", 4);
*(int*)(hdr + 40) = data;
fwrite(hdr, 1, 44, f);
for (size_t i = 0; i < x.size(); i++) {
short v = (short)(std::max(-1.0f, std::min(1.0f, x[i])) * 32767.0f);
fwrite(&v, 2, 1, f);
}
fclose(f);
return true;
}
int main(int argc, char** argv) {
if (argc < 3) { fprintf(stderr, "usage: %s in.wav out.wav [fc,q,sens] ...\n", argv[0]); return 1; }
std::vector<float> x;
int sr;
if (!load_wav(argv[1], x, sr)) { fprintf(stderr, "cannot load %s\n", argv[1]); return 1; }
std::vector<DetectorBand> bands;
if (argc >= 4 && strchr(argv[3], ',')) {
// comma form: "fc,q,sens[,scale]"
float fc, q, sens, scl = 1.0f;
// parse first 3
int n = sscanf(argv[3], "%f,%f,%f,%f", &fc, &q, &sens, &scl);
DetectorBand b; b.fc = fc; b.q = q; b.sens = sens; b.level_scale = scl;
bands.push_back(b);
} else {
for (int i = 3; i + 2 < argc; i += 3) {
DetectorBand b;
b.fc = (float)atof(argv[i]);
b.q = (float)atof(argv[i + 1]);
b.sens = (float)atof(argv[i + 2]);
if (i + 3 < argc) b.level_scale = (float)atof(argv[i + 3]);
bands.push_back(b);
}
}
if (bands.empty()) bands.push_back({1000.0f, 1.0f, 12.0f});
SpectralProcessor sp(2048, 512);
sp.setDetectorParams(bands);
std::vector<float> y(x.size());
const size_t BLK = 1 << 16;
std::vector<float> inb(BLK), outb(BLK);
for (size_t s = 0; s < x.size(); s += BLK) {
size_t n = std::min(BLK, x.size() - s);
memcpy(inb.data(), x.data() + s, n * sizeof(float));
for (size_t i = n; i < BLK; i++) inb[i] = 0.0f;
sp.processBlock(inb.data(), outb.data(), BLK, 1);
memcpy(y.data() + s, outb.data(), n * sizeof(float));
}
save_wav(argv[2], y, sr);
printf("wrote %s (%zu samples sr=%d, %zu bands)\n", argv[2], y.size(), sr, bands.size());
return 0;
}
File diff suppressed because one or more lines are too long
+5
View File
@@ -0,0 +1,5 @@
// Auto-generated from live RT capture /tmp/snap_rt.bin (0x5406c8/0x5406e8).
// per-bin attack (att) and release (rel) coefficients for the level tracker.
#pragma once
extern const float kRTAtt[2049];
extern const float kRTRel[2049];