wip: MDCT, SNS in fixed point

This commit is contained in:
Antoine Soulier
2023-04-14 14:47:31 -07:00
parent 2ce884d8ef
commit 82db8e05ae
16 changed files with 2526 additions and 2187 deletions

View File

@@ -97,7 +97,7 @@ def check_forward_unit(rng, dt, sr):
nd = T.ND[dt][sr]
ok = True
x = (2 * rng.random(ns)) - 1
x = ( (2 * rng.random(ns)) - 1 ) * 2**15
y = [ None ] * 2
y_c = [ None ] * 2
@@ -109,8 +109,8 @@ def check_forward_unit(rng, dt, sr):
(y_c[0], d_c) = lc3.mdct_forward(dt, sr, x, np.zeros(nd))
y_c[1] = lc3.mdct_forward(dt, sr, x, d_c)[0]
ok = ok and np.amax(np.abs(y[0] - y_c[0])) < 1e-5
ok = ok and np.amax(np.abs(y[1] - y_c[1])) < 1e-5
ok = ok and np.amax(np.abs(y[0] - y_c[0])) < 1e-1
ok = ok and np.amax(np.abs(y[1] - y_c[1])) < 1e-1
return ok
@@ -137,7 +137,7 @@ def check_inverse_unit(rng, dt, sr):
nd = [ (23 * ns) // 30, (5 * ns) // 8 ][dt]
ok = True
x = (2 * rng.random(ns)) - 1
x = ( (2 * rng.random(ns)) - 1 ) * 2**15
y = [ None ] * 2
y_c = [ None ] * 2
@@ -149,8 +149,8 @@ def check_inverse_unit(rng, dt, sr):
(y_c[0], d_c) = lc3.mdct_inverse(dt, sr, x, np.zeros(nd))
y_c[1] = lc3.mdct_inverse(dt, sr, x, d_c)[0]
ok = ok and np.amax(np.abs(y[0] - y_c[0])) < 1e-5
ok = ok and np.amax(np.abs(y[1] - y_c[1])) < 1e-5
ok = ok and np.amax(np.abs(y[0] - y_c[0])) < 1e-1
ok = ok and np.amax(np.abs(y[1] - y_c[1])) < 1e-1
return ok

View File

@@ -28,7 +28,7 @@ static PyObject *mdct_forward_py(PyObject *m, PyObject *args)
PyObject *x_obj, *xd_obj, *y_obj, *d_obj;
enum lc3_dt dt;
enum lc3_srate sr;
float *x, *xd, *y, *d;
lc3_intfloat_t *x, *xd, *y, *d;
if (!PyArg_ParseTuple(args, "iiOO", &dt, &sr, &x_obj, &xd_obj))
return NULL;
@@ -45,8 +45,18 @@ static PyObject *mdct_forward_py(PyObject *m, PyObject *args)
memcpy(d, xd, nd * sizeof(float));
#ifdef CONFIG_FIXED_POINT
for (int i = 0; i < ns; i++) x[i] = ldexpf(((float *)x)[i], 8);
for (int i = 0; i < nd; i++) d[i] = ldexpf(((float *)d)[i], 8);
#endif
lc3_mdct_forward(dt, sr, sr, x, d, y);
#ifdef CONFIG_FIXED_POINT
for (int i = 0; i < nd; i++) ((float *)d)[i] = ldexpf(d[i], -8);
for (int i = 0; i < ns; i++) ((float *)y)[i] = ldexpf(y[i], -8);
#endif
return Py_BuildValue("NN", y_obj, d_obj);
}
@@ -55,7 +65,7 @@ static PyObject *mdct_inverse_py(PyObject *m, PyObject *args)
PyObject *x_obj, *xd_obj, *d_obj, *y_obj;
enum lc3_dt dt;
enum lc3_srate sr;
float *x, *xd, *d, *y;
lc3_intfloat_t *x, *xd, *d, *y;
if (!PyArg_ParseTuple(args, "iiOO", &dt, &sr, &x_obj, &xd_obj))
return NULL;
@@ -72,8 +82,18 @@ static PyObject *mdct_inverse_py(PyObject *m, PyObject *args)
memcpy(d, xd, nd * sizeof(float));
#ifdef CONFIG_FIXED_POINT
for (int i = 0; i < ns; i++) x[i] = ldexpf(((float *)x)[i], 8);
for (int i = 0; i < nd; i++) d[i] = ldexpf(((float *)d)[i], 8);
#endif
lc3_mdct_inverse(dt, sr, sr, x, d, y);
#ifdef CONFIG_FIXED_POINT
for (int i = 0; i < nd; i++) ((float *)d)[i] = ldexpf(d[i], -8);
for (int i = 0; i < ns; i++) ((float *)y)[i] = ldexpf(y[i], -8);
#endif
return Py_BuildValue("NN", y_obj, d_obj);
}

View File

@@ -535,7 +535,7 @@ def check_analysis_appendix_c(dt):
ok = ok and np.amax(np.abs(scf_q - C.SCF_Q[dt][i])) < 1e-5
x = lc3.sns_spectral_shaping(dt, sr, C.SCF_Q[dt][i], False, C.X[dt][i])
ok = ok and np.amax(np.abs(1 - x/C.X_S[dt][i])) < 1e-5
ok = ok and np.amax(np.abs(x - C.X_S[dt][i])) < 1e-1
(x, data) = lc3.sns_analyze(dt, sr, C.E_B[dt][i], False, C.X[dt][i])
ok = ok and data['lfcb'] == C.IND_LF[dt][i]
@@ -549,7 +549,7 @@ def check_analysis_appendix_c(dt):
data['idx_b'] == C.IDX_B[dt][i])
ok = ok and (C.LS_IND_B[dt][i] is None or
data['ls_b'] == C.LS_IND_B[dt][i])
ok = ok and np.amax(np.abs(1 - x/C.X_S[dt][i])) < 1e-5
ok = ok and np.amax(np.abs(x - C.X_S[dt][i])) < 1e-1
return ok

View File

@@ -92,7 +92,7 @@ static PyObject *unquantize_py(PyObject *m, PyObject *args)
PyObject *y_obj, *scf_obj;
int lfcb_idx, hfcb_idx;
int shape, gain;
float *y, *scf;
lc3_intfloat_t *y, *scf;
if (!PyArg_ParseTuple(args, "iiOii",
&lfcb_idx, &hfcb_idx, &y_obj, &shape, &gain))
@@ -107,8 +107,16 @@ static PyObject *unquantize_py(PyObject *m, PyObject *args)
scf_obj = new_1d_ptr(NPY_FLOAT, 16, &scf);
#ifdef CONFIG_FIXED_POINT
for (int i = 0; i < 16; i++) y[i] = ldexpf(((float *)y)[i], 24);
#endif
unquantize(lfcb_idx, hfcb_idx, y, shape, gain, scf);
#ifdef CONFIG_FIXED_POINT
for (int i = 0; i < 16; i++) ((float *)scf)[i] = ldexpf(scf[i], -24);
#endif
return Py_BuildValue("N", scf_obj);
}
@@ -116,7 +124,7 @@ static PyObject *spectral_shaping_py(PyObject *m, PyObject *args)
{
PyObject *scf_q_obj, *x_obj;
unsigned dt, sr;
float *scf_q, *x;
lc3_intfloat_t *scf_q, *x;
int inv;
if (!PyArg_ParseTuple(args, "IIOpO", &dt, &sr, &scf_q_obj, &inv, &x_obj))
@@ -130,8 +138,17 @@ static PyObject *spectral_shaping_py(PyObject *m, PyObject *args)
CTYPES_CHECK("scf_q", to_1d_ptr(scf_q_obj, NPY_FLOAT, 16, &scf_q));
CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_FLOAT, ne, &x));
#ifdef CONFIG_FIXED_POINT
for (int i = 0; i < 16; i++) scf_q[i] = ldexpf(((float *)scf_q)[i], 24);
for (int i = 0; i < ne; i++) x[i] = ldexpf(((float *)x)[i], 8);
#endif
spectral_shaping(dt, sr, scf_q, inv, x, x);
#ifdef CONFIG_FIXED_POINT
for (int i = 0; i < ne; i++) ((float *)x)[i] = ldexpf(x[i], -8);
#endif
return Py_BuildValue("O", x_obj);
}
@@ -165,7 +182,7 @@ static PyObject *synthesize_py(PyObject *m, PyObject *args)
PyObject *data_obj, *x_obj;
struct lc3_sns_data data;
unsigned dt, sr;
float *x;
lc3_intfloat_t *x;
if (!PyArg_ParseTuple(args, "IIOO", &dt, &sr, &data_obj, &x_obj))
return NULL;
@@ -178,8 +195,16 @@ static PyObject *synthesize_py(PyObject *m, PyObject *args)
CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_FLOAT, ne, &x));
#ifdef CONFIG_FIXED_POINT
for (int i = 0; i < ne; i++) x[i] = ldexpf(((float *)x)[i], 8);
#endif
lc3_sns_synthesize(dt, sr, &data, x, x);
#ifdef CONFIG_FIXED_POINT
for (int i = 0; i < ne; i++) ((float *)x)[i] = ldexpf(x[i], -8);
#endif
return Py_BuildValue("O", x_obj);
}