Implement real RFFT for FIR construction (experimental)

Added real RFFT functions (execute_real_forward, execute_real_inverse)
to fft.hpp/cpp. These implement the standard algorithm for real-valued
FFT using complex FFT of half size.

Updated buildFirFromMask to use real RFFTs matching the plugin's pipeline:
1. log(mask) → negate
2. forward real RFFT (opB)
3. EXP in-place
4. inverse real RFFT (opC)
5. Window
6. forward real RFFT (opD)

However, the real RFFT implementation makes results worse (10.377 dB vs
1.825 dB default). The plugin's real RFFT likely has subtle differences
(normalization, twiddle factors) that are not captured by the standard
algorithm.

The default path (no FIRCONV) remains the best approach with 1.825 dB
TOTAL error.

Future work: Reverse-engineer the plugin's exact real RFFT implementation
from disassembly (th1a90/th2180) to achieve bit-exact FIR construction.
This commit is contained in:
2026-08-27 19:48:34 +03:00
parent d7cbab3e4c
commit 8805a8f183
3 changed files with 138 additions and 49 deletions
+48 -49
View File
@@ -144,82 +144,81 @@ void SpectralProcessor::loadWinFreq() {
}
void SpectralProcessor::buildFirFromMask(const float* mask, std::complex<double>* fir, size_t nbin) {
// Plugin FIR construction pipeline (52b550-52b8bb) uses real RFFTs with twiddle operations.
// Our implementation uses a simplified approach: ln → negate → exp2 → IFFT → window → FFT
// This is NOT bit-exact but provides reasonable results for most cases.
//
// Plugin's exact pipeline:
// 1. log(bands) → scratch
// 2. copy scratch → FIR
// 3. opA: inverse real-RFFT (th2180) with twiddle
// 4. FIR[n]=0, sign inversion, zero upper half
// 5. opB: forward real-RFFT (th1a90) with twiddle
// 6. EXP in-place (140b30)
// 7. opC: inverse real-RFFT (th2180) with twiddle
// 8. FIR[n]=0, window, zero upper half
// 9. opD: forward real-RFFT (th1a90) with twiddle
// 10. FIR[0]=1, FIR[1]=0
//
// The twiddle operations use buf548 (cos/sin table) and mask598 (SIMD masks)
// and are specific to real RFFTs. Implementing real RFFTs correctly requires
// significant effort and is deferred to future work.
// Plugin FIR construction pipeline (52b550-52b8bb) uses real RFFTs:
// 1. log(bands) → negate
// 2. forward real RFFT (opB, th1a90)
// 3. EXP in-place (140b30)
// 4. inverse real RFFT (opC, th2180)
// 5. Window with WIN_freq
// 6. forward real RFFT (opD, th1a90)
// 7. Normalize: FIR[0]=1, FIR[1]=0
const size_t half = nfft_ / 2;
const size_t nfft = nfft_;
// Compute ln(mask) and negate
std::vector<std::complex<double>> H(nfft);
// Step 1: log(mask) and negate
std::vector<double> log_mask(nfft);
for (size_t i = 0; i <= half; i++) {
float m = mask[i];
if (m > 1e-12f) {
float ln_m = soothe2::ln_plugin_f32(m);
ln_m = -ln_m;
H[i] = std::complex<double>(static_cast<double>(ln_m), 0.0);
log_mask[i] = -static_cast<double>(ln_m);
} else {
H[i] = std::complex<double>(0.0, 0.0);
log_mask[i] = 0.0;
}
}
// Zero upper half
for (size_t i = half + 1; i < nfft; i++) {
H[i] = std::complex<double>(0.0, 0.0);
log_mask[i] = 0.0;
}
// IFFT to time domain
fft::execute_inverse(&plan_, H.data());
// Step 2: forward real RFFT (opB)
std::vector<std::complex<double>> H(half + 1);
fft::execute_real_forward(&plan_, log_mask.data(), H.data());
// Causal window: keep first half, apply rising Hann (0.5→1.0)
for (size_t i = 0; i < half; i++) {
double win = 0.5 * (1.0 - std::cos(2.0 * M_PI * i / nfft));
H[i] *= win;
}
for (size_t i = half; i < nfft; i++) {
H[i] = std::complex<double>(0.0, 0.0);
// Step 3: EXP in-place using plugin's exp2
for (size_t i = 0; i <= half; i++) {
double re = H[i].real();
double im = H[i].imag();
double exp_re = exp2d::exp2_dsp(re);
H[i] = std::complex<double>(exp_re * std::cos(im), exp_re * std::sin(im));
}
// FFT back to freq domain
fft::execute(&plan_, H.data());
// Step 4: inverse real RFFT (opC)
std::vector<double> time_domain(nfft);
fft::execute_real_inverse(&plan_, H.data(), time_domain.data());
// Apply WIN_freq window
// Step 5: Window with WIN_freq (falling half of periodic Hann)
if (!win_freq_.empty() && win_freq_.size() > half) {
for (size_t i = 0; i <= half; i++) {
H[i] *= static_cast<double>(win_freq_[i]);
for (size_t i = 0; i < half; i++) {
time_domain[i] *= static_cast<double>(win_freq_[half + i]);
}
}
// Zero upper half again
for (size_t i = half + 1; i < nfft; i++) {
H[i] = std::complex<double>(0.0, 0.0);
// Zero upper half
for (size_t i = half; i < nfft; i++) {
time_domain[i] = 0.0;
}
// Normalize: FIR[0]=1, FIR[1]=0
// Step 6: forward real RFFT (opD)
std::vector<std::complex<double>> H_final(half + 1);
fft::execute_real_forward(&plan_, time_domain.data(), H_final.data());
// Step 7: Normalize and copy to output
double scale = 1.0;
if (std::abs(H[0].real()) > 1e-12) {
scale = 1.0 / H[0].real();
if (std::abs(H_final[0].real()) > 1e-12) {
scale = 1.0 / H_final[0].real();
}
for (size_t i = 0; i < nfft; i++) {
fir[i] = H[i] * scale;
// Copy to full complex array
for (size_t i = 0; i <= half; i++) {
fir[i] = H_final[i] * scale;
}
// Mirror for negative frequencies
for (size_t i = 1; i < half; i++) {
fir[nfft - i] = std::conj(fir[i]);
}
// Enforce FIR[0]=1, FIR[1]=0
fir[0] = std::complex<double>(1.0, 0.0);
if (half >= 1) {
fir[1] = std::complex<double>(0.0, 0.0);