mirror of
https://git.vladimir.cc/vladimir/wdsp.git
synced 2026-08-25 17:27:33 +00:00
emnr: linear-time aepf, drop idiv from ring walks, halve getKey's logs
Three separate costs, found with a sampling profile of the RX chain with
emnr forced on:
aepf() averaged mask[] over a window of N = 2*psi + 1 = 41 bins by walking
the window for every one of the 2049 bins, i.e. O(msize*N). Its three spans
are all symmetric windows clipped at the array ends, so take each from a
prefix sum instead: one subtraction per output, O(msize).
xemnr() advanced four ring indices with a '% size' per step. iasize is 4096
and oasize 1024 here, and neither is known to the compiler, so each step was
a real integer division -- ~8700 of them per frame. The indices step by one
and, since iasize >= fsize and oasize >= incr always hold, wrap at most once
per loop, so walk contiguous runs and wrap between them.
calc_gain() called getKey() twice per bin with the same gamma, so the gamma
row index and its log10 were computed twice. Split getKey into keyIndex() +
keyLerp() and locate gamma once. The remaining logs go through wdsp_log10()
(new fastmath.h), accurate to 2e-13 against libm and ~2.4x its throughput;
gamma and xi are bracketed against the table limits first, so the argument
is always positive and normal. Also clamp the row index so the second
bilinear corner cannot address the next row of the 241x241 table.
Measured in situ on an Apple M1 Pro, 512-sample buffers, cost of turning
emnr on, best of 5:
baseline 73661 ns
+ aepf, ring walks 48927 ns 1.51x
+ getKey 37852 ns 1.95x
Output is not bit-identical, as the prefix sum and the reassociated logs
round differently. Over 300 buffers with emnr alone the worst deviation is
4.0e-09, an SNR of 196 dB; perturbing a single input sample of the unmodified
code by one ulp diverges it from itself by 1.4e-08 (186 dB), so this change
disturbs the chain less than the last bit of the input does.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
3faad14fc1
commit
fd2ba84e7d
@@ -557,6 +557,7 @@ void calc_emnr(EMNR a)
|
||||
a->ae.psi = 20.0;
|
||||
a->ae.t2 = 0.20;
|
||||
a->ae.nmask = (double *)malloc0(a->ae.msize * sizeof(double));
|
||||
a->ae.csum = (double *)malloc0((a->ae.msize + 1) * sizeof(double));
|
||||
//
|
||||
// post2
|
||||
a->post2.run = 0;
|
||||
@@ -580,6 +581,7 @@ void decalc_emnr(EMNR a)
|
||||
_aligned_free(a->post2.noise_frame);
|
||||
_aligned_free(a->post2.w);
|
||||
// ae
|
||||
_aligned_free(a->ae.csum);
|
||||
_aligned_free(a->ae.nmask);
|
||||
// npl
|
||||
_aligned_free(a->npl.D);
|
||||
@@ -868,26 +870,26 @@ void aepf(EMNR a)
|
||||
else
|
||||
N = 1 + 2 * (int)(0.5 + a->ae.psi * (1.0 - zetaT / a->ae.zetaThresh));
|
||||
n = N / 2;
|
||||
for (k = 0; k < n; k++)
|
||||
/* Each of the three spans below averages mask[] over a window that is
|
||||
symmetric about k and clipped at the array ends. Taking them straight
|
||||
from a prefix sum makes each output one subtraction rather than a walk of
|
||||
up to N = 2*psi + 1 taps, so the pass is O(msize) instead of O(msize*N). */
|
||||
{
|
||||
a->ae.nmask[k] = 0.0;
|
||||
for (m = 0; m <= 2 * k; m++)
|
||||
a->ae.nmask[k] += a->mask[m];
|
||||
a->ae.nmask[k] /= (double)(2 * k + 1);
|
||||
}
|
||||
for (k = n; k < (a->ae.msize - n); k++)
|
||||
{
|
||||
a->ae.nmask[k] = 0.0;
|
||||
for (m = k - n; m <= (k + n); m++)
|
||||
a->ae.nmask[k] += a->mask[m];
|
||||
a->ae.nmask[k] /= (double)N;
|
||||
}
|
||||
for (k = a->ae.msize - n; k < a->ae.msize; k++)
|
||||
{
|
||||
a->ae.nmask[k] = 0.0;
|
||||
for (m = (a->ae.msize - 1); m >= (-a->ae.msize + 2 * k + 1); m--)
|
||||
a->ae.nmask[k] += a->mask[m];
|
||||
a->ae.nmask[k] /= (double)(2 * (a->ae.msize - k) - 1);
|
||||
const int msize = a->ae.msize;
|
||||
const double* WDSP_RESTRICT mask = a->mask;
|
||||
double* WDSP_RESTRICT nmask = a->ae.nmask;
|
||||
double* WDSP_RESTRICT csum = a->ae.csum;
|
||||
|
||||
csum[0] = 0.0;
|
||||
for (k = 0; k < msize; k++)
|
||||
csum[k + 1] = csum[k] + mask[k];
|
||||
|
||||
for (k = 0; k < n; k++) // window [0, 2k]
|
||||
nmask[k] = (csum[2 * k + 1] - csum[0]) / (double)(2 * k + 1);
|
||||
for (k = n; k < (msize - n); k++) // window [k-n, k+n]
|
||||
nmask[k] = (csum[k + n + 1] - csum[k - n]) / (double)N;
|
||||
for (k = msize - n; k < msize; k++) // window [2k+1-msize, msize-1]
|
||||
nmask[k] = (csum[msize] - csum[2 * k + 1 - msize]) / (double)(2 * (msize - k) - 1);
|
||||
}
|
||||
memcpy (a->mask, a->ae.nmask, a->ae.msize * sizeof (double));
|
||||
if (a->g.gain_method == 3 && zetaT < a->ae.t2)
|
||||
@@ -998,52 +1000,54 @@ void SetRXAEMNRpost2Rate(int channel, double tc)
|
||||
* End Post-Processing Functions *
|
||||
********************************************************************************************************/
|
||||
|
||||
double getKey(double* type, double gamma, double xi)
|
||||
/* Locate v on the table's 0.25 dB grid: n1/n2 bracket it, d is the fraction.
|
||||
|
||||
v is compared against the table's dB limits first, so on the interpolating
|
||||
path the log argument is positive and normal and wdsp_log10 applies. */
|
||||
static inline void keyIndex (double v, int* n1, int* n2, double* d)
|
||||
{
|
||||
int ngamma1, ngamma2, nxi1, nxi2;
|
||||
double tg, tx, dg, dx;
|
||||
const double dmin = 0.001;
|
||||
const double dmax = 1000.0;
|
||||
if (gamma <= dmin)
|
||||
if (v <= dmin)
|
||||
{
|
||||
ngamma1 = ngamma2 = 0;
|
||||
tg = 0.0;
|
||||
*n1 = *n2 = 0;
|
||||
*d = 0.0;
|
||||
}
|
||||
else if (gamma >= dmax)
|
||||
else if (v >= dmax)
|
||||
{
|
||||
ngamma1 = ngamma2 = 240;
|
||||
tg = 60.0;
|
||||
*n1 = *n2 = 240;
|
||||
*d = 0.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
tg = 10.0 * log10(gamma / dmin);
|
||||
ngamma1 = (int)(4.0 * tg);
|
||||
ngamma2 = ngamma1 + 1;
|
||||
double f = 40.0 * wdsp_log10 (v / dmin); // 4 * (10 * log10)
|
||||
int i = (int)f;
|
||||
/* clamp so n2 cannot address the next row of the 241x241 table */
|
||||
if (i > 239) i = 239;
|
||||
*n1 = i;
|
||||
*n2 = i + 1;
|
||||
*d = f - (double)i;
|
||||
}
|
||||
if (xi <= dmin)
|
||||
{
|
||||
nxi1 = nxi2 = 0;
|
||||
tx = 0.0;
|
||||
}
|
||||
else if (xi >= dmax)
|
||||
{
|
||||
nxi1 = nxi2 = 240;
|
||||
tx = 60.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
tx = 10.0 * log10(xi / dmin);
|
||||
nxi1 = (int)(4.0 * tx);
|
||||
nxi2 = nxi1 + 1;
|
||||
}
|
||||
dg = (tg - 0.25 * ngamma1) / 0.25;
|
||||
dx = (tx - 0.25 * nxi1) / 0.25;
|
||||
}
|
||||
|
||||
static inline double keyLerp (const double* type, int ngamma1, int ngamma2, double dg,
|
||||
int nxi1, int nxi2, double dx)
|
||||
{
|
||||
return (1.0 - dg) * (1.0 - dx) * type[241 * nxi1 + ngamma1]
|
||||
+ (1.0 - dg) * dx * type[241 * nxi2 + ngamma1]
|
||||
+ dg * (1.0 - dx) * type[241 * nxi1 + ngamma2]
|
||||
+ dg * dx * type[241 * nxi2 + ngamma2];
|
||||
}
|
||||
|
||||
double getKey(double* type, double gamma, double xi)
|
||||
{
|
||||
int ngamma1, ngamma2, nxi1, nxi2;
|
||||
double dg, dx;
|
||||
keyIndex (gamma, &ngamma1, &ngamma2, &dg);
|
||||
keyIndex (xi, &nxi1, &nxi2, &dx);
|
||||
return keyLerp (type, ngamma1, ngamma2, dg, nxi1, nxi2, dx);
|
||||
}
|
||||
|
||||
int getZeta( EMNR a, double gamma, double eps, double* zeta)
|
||||
{
|
||||
int index, i_gamma, i_xi;
|
||||
@@ -1133,13 +1137,20 @@ void calc_gain (EMNR a)
|
||||
case 2:
|
||||
{
|
||||
double gamma, eps_hat, eps_p;
|
||||
int ngamma1, ngamma2, nxi1, nxi2, npi1, npi2;
|
||||
double dg, dx, dp;
|
||||
for (k = 0; k < a->g.msize; k++)
|
||||
{
|
||||
gamma = min(a->g.lambda_y[k] / a->g.lambda_d[k], a->g.gamma_max);
|
||||
eps_hat = a->g.alpha * a->g.prev_mask[k] * a->g.prev_mask[k] * a->g.prev_gamma[k]
|
||||
+ (1.0 - a->g.alpha) * max(gamma - 1.0, a->g.eps_floor);
|
||||
eps_p = eps_hat / (1.0 - a->g.q);
|
||||
a->g.mask[k] = getKey(a->g.GG, gamma, eps_hat) * getKey(a->g.GGS, gamma, eps_p);
|
||||
/* both lookups share gamma, so locate it once */
|
||||
keyIndex (gamma, &ngamma1, &ngamma2, &dg);
|
||||
keyIndex (eps_hat, &nxi1, &nxi2, &dx);
|
||||
keyIndex (eps_p, &npi1, &npi2, &dp);
|
||||
a->g.mask[k] = keyLerp (a->g.GG, ngamma1, ngamma2, dg, nxi1, nxi2, dx)
|
||||
* keyLerp (a->g.GGS, ngamma1, ngamma2, dg, npi1, npi2, dp);
|
||||
a->g.prev_gamma[k] = gamma;
|
||||
a->g.prev_mask[k] = a->g.mask[k];
|
||||
}
|
||||
@@ -1203,18 +1214,34 @@ void xemnr (EMNR a, int pos)
|
||||
{
|
||||
int i, j, k, sbuff, sbegin;
|
||||
double g1;
|
||||
for (i = 0; i < 2 * a->bsize; i += 2)
|
||||
/* The ring indices below advance by one per iteration and wrap at most
|
||||
once per loop, so a '% size' each step is an integer division for
|
||||
nothing (iasize = 3584 here, not a power of two). Walk contiguous runs
|
||||
and wrap between them instead. */
|
||||
const int iasize = a->iasize;
|
||||
const int oasize = a->oasize;
|
||||
const int fsize = a->fsize;
|
||||
const int incr = a->incr;
|
||||
const int bsize = a->bsize;
|
||||
const int ovrlp = a->ovrlp;
|
||||
|
||||
for (i = 0, j = a->iainidx; i < 2 * bsize; i += 2)
|
||||
{
|
||||
a->inaccum[a->iainidx] = a->in[i];
|
||||
a->iainidx = (a->iainidx + 1) % a->iasize;
|
||||
a->inaccum[j] = a->in[i];
|
||||
if (++j == iasize) j = 0;
|
||||
}
|
||||
a->nsamps += a->bsize;
|
||||
while (a->nsamps >= a->fsize)
|
||||
a->iainidx = j;
|
||||
a->nsamps += bsize;
|
||||
while (a->nsamps >= fsize)
|
||||
{
|
||||
for (i = 0, j = a->iaoutidx; i < a->fsize; i++, j = (j + 1) % a->iasize)
|
||||
a->forfftin[i] = a->window[i] * a->inaccum[j];
|
||||
a->iaoutidx = (a->iaoutidx + a->incr) % a->iasize;
|
||||
a->nsamps -= a->incr;
|
||||
int n1 = iasize - a->iaoutidx;
|
||||
if (n1 > fsize) n1 = fsize;
|
||||
for (i = 0; i < n1; i++)
|
||||
a->forfftin[i] = a->window[i] * a->inaccum[a->iaoutidx + i];
|
||||
for (; i < fsize; i++)
|
||||
a->forfftin[i] = a->window[i] * a->inaccum[i - n1];
|
||||
if ((a->iaoutidx += incr) >= iasize) a->iaoutidx -= iasize;
|
||||
a->nsamps -= incr;
|
||||
fftw_execute (a->Rfor);
|
||||
calc_gain(a);
|
||||
for (i = 0; i < a->msize; i++)
|
||||
@@ -1225,29 +1252,40 @@ void xemnr (EMNR a, int pos)
|
||||
}
|
||||
post2(a);
|
||||
fftw_execute (a->Rrev);
|
||||
for (i = 0; i < a->fsize; i++)
|
||||
for (i = 0; i < fsize; i++)
|
||||
a->save[a->saveidx][i] = a->window[i] * a->revfftout[i];
|
||||
for (i = a->ovrlp; i > 0; i--)
|
||||
for (i = ovrlp; i > 0; i--)
|
||||
{
|
||||
sbuff = (a->saveidx + i) % a->ovrlp;
|
||||
sbegin = a->incr * (a->ovrlp - i);
|
||||
for (j = sbegin, k = a->oainidx; j < a->incr + sbegin; j++, k = (k + 1) % a->oasize)
|
||||
const double* WDSP_RESTRICT sv;
|
||||
double* WDSP_RESTRICT oa = a->outaccum;
|
||||
int m1;
|
||||
sbuff = (a->saveidx + i) % ovrlp;
|
||||
sbegin = incr * (ovrlp - i);
|
||||
sv = a->save[sbuff] + sbegin;
|
||||
m1 = oasize - a->oainidx;
|
||||
if (m1 > incr) m1 = incr;
|
||||
k = a->oainidx;
|
||||
if (i == ovrlp)
|
||||
{
|
||||
if ( i == a->ovrlp)
|
||||
a->outaccum[k] = a->save[sbuff][j];
|
||||
else
|
||||
a->outaccum[k] += a->save[sbuff][j];
|
||||
for (j = 0; j < m1; j++) oa[k + j] = sv[j];
|
||||
for (; j < incr; j++) oa[j - m1] = sv[j];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (j = 0; j < m1; j++) oa[k + j] += sv[j];
|
||||
for (; j < incr; j++) oa[j - m1] += sv[j];
|
||||
}
|
||||
}
|
||||
a->saveidx = (a->saveidx + 1) % a->ovrlp;
|
||||
a->oainidx = (a->oainidx + a->incr) % a->oasize;
|
||||
if (++a->saveidx == ovrlp) a->saveidx = 0;
|
||||
if ((a->oainidx += incr) >= oasize) a->oainidx -= oasize;
|
||||
}
|
||||
for (i = 0; i < a->bsize; i++)
|
||||
for (i = 0, k = a->oaoutidx; i < bsize; i++)
|
||||
{
|
||||
a->out[2 * i + 0] = a->outaccum[a->oaoutidx];
|
||||
a->out[2 * i + 0] = a->outaccum[k];
|
||||
a->out[2 * i + 1] = 0.0;
|
||||
a->oaoutidx = (a->oaoutidx + 1) % a->oasize;
|
||||
if (++k == oasize) k = 0;
|
||||
}
|
||||
a->oaoutidx = k;
|
||||
}
|
||||
else if (a->out != a->in)
|
||||
memcpy (a->out, a->in, a->bsize * sizeof (complex));
|
||||
|
||||
Reference in New Issue
Block a user