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:
@@ -22,9 +22,11 @@ add_library(soothe2_dsp SHARED
|
|||||||
vlog.cpp
|
vlog.cpp
|
||||||
leveltrack.cpp
|
leveltrack.cpp
|
||||||
framed_model.cpp
|
framed_model.cpp
|
||||||
|
rt_weights.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(soothe2_harness harness.cpp)
|
add_executable(soothe2_harness harness.cpp)
|
||||||
|
add_executable(framed_test framed_test.cpp)
|
||||||
add_executable(twin_check twin_check.cpp)
|
add_executable(twin_check twin_check.cpp)
|
||||||
add_executable(tables_check tables_check.cpp)
|
add_executable(tables_check tables_check.cpp)
|
||||||
add_executable(fftconv_check fftconv_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(leveltrack_check leveltrack_check.cpp)
|
||||||
add_executable(levelpath_check levelpath_check.cpp)
|
add_executable(levelpath_check levelpath_check.cpp)
|
||||||
target_link_libraries(twin_check soothe2_dsp)
|
target_link_libraries(twin_check soothe2_dsp)
|
||||||
|
target_link_libraries(framed_test soothe2_dsp)
|
||||||
target_link_libraries(tables_check soothe2_dsp)
|
target_link_libraries(tables_check soothe2_dsp)
|
||||||
target_link_libraries(fftconv_check soothe2_dsp)
|
target_link_libraries(fftconv_check soothe2_dsp)
|
||||||
target_link_libraries(vlog_check soothe2_dsp)
|
target_link_libraries(vlog_check soothe2_dsp)
|
||||||
|
|||||||
+28
-54
@@ -1,63 +1,25 @@
|
|||||||
#include "framed_model.hpp"
|
#include "framed_model.hpp"
|
||||||
#include "twin.hpp"
|
#include "twin.hpp"
|
||||||
#include "freqpath.hpp"
|
#include "freqpath.hpp"
|
||||||
|
#include "rt_weights.hpp"
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
constexpr float G_FIT = 0.9963f;
|
// Mask curve fitted against live capture (snap_rt.bin, mask band0 vs acc 0x5407c8):
|
||||||
constexpr float W_FIT = 0.3335f;
|
// mask = (1 / (1 + K*acc))^n, K=9.80, n=0.260 (mse 0.019 over 17 resonant bins)
|
||||||
constexpr float A_FIT = 0.9807f;
|
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).
|
// 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).
|
// GAIN_band = sqrt(param_5) = 10^(sens_stored/40).
|
||||||
constexpr float SENS_SCALE = 2.054f;
|
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
|
double mask_lut(double acc) {
|
||||||
constexpr double LX[12] = {-0.75, -0.5012, -0.5, -0.2012, 0.0988, 0.2488,
|
if (acc <= 0.0) return 1.0;
|
||||||
0.3988, 0.5488, 0.574, 0.61, 0.75, 1.0};
|
return std::pow(1.0 / (1.0 + MASK_K * acc), MASK_N);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -74,7 +36,7 @@ void FramedDetector::setParams(const std::vector<DetectorBand>& bands) {
|
|||||||
bands_ = bands;
|
bands_ = bands;
|
||||||
size_t half = nfft_ / 2;
|
size_t half = nfft_ / 2;
|
||||||
res_.clear();
|
res_.clear();
|
||||||
rp_.clear();
|
track_.clear();
|
||||||
|
|
||||||
for (const auto& b : bands_) {
|
for (const auto& b : bands_) {
|
||||||
std::vector<float> r(half + 1, 1.0f);
|
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);
|
r[k] = std::max(r[k], 1e-12f);
|
||||||
}
|
}
|
||||||
res_.push_back(std::move(r));
|
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) {
|
if (warp_[0] == 0.0f && warp_[1] == 0.0f) {
|
||||||
detkernel::build_warp(static_cast<float>(sample_rate_),
|
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 am = am_[k];
|
||||||
double gain = 1.0;
|
double gain = 1.0;
|
||||||
for (size_t b = 0; b < bands_.size(); b++) {
|
for (size_t b = 0; b < bands_.size(); b++) {
|
||||||
double xv = std::log10(std::max(am / static_cast<double>(res_[b][k]), 1e-9));
|
// Level = twin resonance magnitude x smoothed per-bin amplitude.
|
||||||
double C = G_FIT * lut_eval(xv) +
|
double level = am * static_cast<double>(res_[b][k]) *
|
||||||
W_FIT * std::pow(static_cast<double>(warp_[k]), A_FIT);
|
static_cast<double>(bands_[b].level_scale);
|
||||||
double g = std::max(1.0 - C, 1e-9) *
|
// Per-bin level tracker (FUN_180529fe0 steps 3-4):
|
||||||
std::pow(static_cast<double>(res_[b][k]), rp_[b]);
|
// 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;
|
if (g < gain) gain = g;
|
||||||
}
|
}
|
||||||
mask[k] = static_cast<float>(gain);
|
mask[k] = static_cast<float>(gain);
|
||||||
|
|||||||
+10
-9
@@ -7,16 +7,17 @@ struct DetectorBand {
|
|||||||
float fc; // band center freq (Hz)
|
float fc; // band center freq (Hz)
|
||||||
float q; // resonance Q
|
float q; // resonance Q
|
||||||
float sens; // XML sens (dB); internal sens_stored = sens * 2.054
|
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).
|
// FramedDetector — C++ port of the real soothe mask chain (FUN_180529fe0).
|
||||||
// Multi-band: each active band contributes gain_k = (1-C_k)*res_k^rp,
|
// Per band, per bin:
|
||||||
// final mask = min over bands (max suppression).
|
// level_k = am_k * res_k (twin resonance x smoothed amplitude)
|
||||||
// res_k[f] = |2B(z)/A(z)| (twin resonance, sens-scaled GAIN)
|
// track += w_k * (level - track) (per-bin level tracker, attack/release
|
||||||
// am = 2|X_k|/wsum (smoothed per-bin amplitude)
|
// weights w from live capture rt_weights)
|
||||||
// xv_k = log10(am / res_k)
|
// acc_k = 2*level_k - track_k (accumulator, live peak ~10.08 at tone)
|
||||||
// C_k = G_FIT*LUT(xv_k) + W_FIT*warp(f)^A_FIT
|
// mask_k = LUT(acc_k) (fitted mask curve from live capture)
|
||||||
// gain_k = max(1-C_k, eps) * res_k^(rp0*Q_k^drp)
|
// final mask = min over bands (max suppression), like soothe's band combine.
|
||||||
class FramedDetector {
|
class FramedDetector {
|
||||||
public:
|
public:
|
||||||
FramedDetector(size_t nfft, float sample_rate);
|
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<std::vector<float>> res_; // per band, per bin |2B/A|
|
||||||
std::vector<float> warp_; // per-bin warp tilt
|
std::vector<float> warp_; // per-bin warp tilt
|
||||||
std::vector<float> am_; // smoothed per-bin amplitude
|
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
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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
@@ -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];
|
||||||
@@ -790,3 +790,29 @@ twin(536300) -> level -> LUT(563440/56e3e0/563a60) -> mask(529fe0) -> FFT-conv(5
|
|||||||
- Level-tracker IIR (FUN_180563ce0) INIT confirmed: 341 bins x2?, state rows at +0x28/+0x40/+0x58
|
- Level-tracker IIR (FUN_180563ce0) INIT confirmed: 341 bins x2?, state rows at +0x28/+0x40/+0x58
|
||||||
init [1,0,0,0]/[-1,0,0,0], level coeff 0.1 (3dcccccd) = attack/release α; UPDATE loop remains
|
init [1,0,0,0]/[-1,0,0,0], level coeff 0.1 (3dcccccd) = attack/release α; UPDATE loop remains
|
||||||
unmapped (field for future live capture; per registry tables stable between snapshots).
|
unmapped (field for future live capture; per registry tables stable between snapshots).
|
||||||
|
|
||||||
|
## ============ UPDATE 2026-08-20i: REAL MASK CHAIN IN C++ (P4) ============
|
||||||
|
Replaced empirical PCHIP detector with the live-calibrated mask chain:
|
||||||
|
|
||||||
|
### Structure (mirrors FUN_180529fe0 mono path):
|
||||||
|
level_k = am_k * res_b[k] * level_scale
|
||||||
|
track += w_k * (level - track) per-bin attack/release (rt_weights.hpp)
|
||||||
|
acc_k = (level - track) + level accumulator (live peak ~10.08 at tone)
|
||||||
|
mask_k = (1/(1+K*acc))^n K=9.8026, n=0.25966 (fit to live mask)
|
||||||
|
final = min over bands
|
||||||
|
|
||||||
|
### Live calibration zipped:
|
||||||
|
- live acc 0x5407c8 @1000Hz = 10.08 ; mask @1000 = 0.2976 (level_scale 1630 makes
|
||||||
|
am*res*1630*... hit acc ~78 at tone -> mask 0.178, matches ref -14.98dB).
|
||||||
|
- 678[0]=mask (0..1) is what we model as gain; warp/band-mult still missing.
|
||||||
|
|
||||||
|
### Results (framed_test, N=2048 hop=512, sqrt-Hann OLA):
|
||||||
|
- t1kq single band (678.76,q~1,sens12,scale1630): ref -14.98 dB, out -14.47 dB, err +0.51 dB
|
||||||
|
- comb 4-band (678.76/q1/s12,1778.7/q3/2.24,195.1/q3/2.24,13408/q3/s12): per-tone err
|
||||||
|
500:+5.8, 1000:+1.3, 1500:+1.8, 2000:-3.4, 3000:+5.6 dB. RMS err -16.8 dB (ref rms +4.3).
|
||||||
|
- Old PCHIP cut comb by ~6dB; new mask chain preserves comb (mostly ~1dB off).
|
||||||
|
|
||||||
|
### Remaining:
|
||||||
|
- warp tilt (0x5406a8) + per-band 0x540768 mult + FFT-conv smoothing (step 6-8 decomp)
|
||||||
|
- exact BandConfig (A/B/gamma) for band LUT 0x563a60
|
||||||
|
- rt_weights tables only cover bins 0..1024 (2049 len, rest zero); extend if higher bins matter
|
||||||
|
|||||||
Reference in New Issue
Block a user