From d9973b605bc7d203b83bfff4cbdc8147b6672a25 Mon Sep 17 00:00:00 2001 From: Antoine Soulier Date: Tue, 23 Jan 2024 14:09:16 -0800 Subject: [PATCH] tns: Quantize on 16 bits the RC coeffs, required for LC3 Plus HR --- src/tns.c | 15 ++++++++------- tables/mktables.py | 14 ++++++++++++-- test/tns.py | 2 ++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/tns.c b/src/tns.c index e3fc3b7..b2b9305 100644 --- a/src/tns.c +++ b/src/tns.c @@ -241,11 +241,12 @@ LC3_HOT static void lpc_reflection( */ static void quantize_rc(const float *rc, int maxorder, int *order, int *rc_q) { - /* Quantization table, sin(delta * (i + 0.5)), delta = Pi / 17 */ + /* Quantization table, sin(delta * (i + 0.5)), delta = Pi / 17, + * rounded to fixed point Q15 value (LC3-Plus HR requirements). */ static float q_thr[] = { - 9.22683595e-02, 2.73662990e-01, 4.45738356e-01, 6.02634636e-01, - 7.39008917e-01, 8.50217136e-01, 9.32472229e-01, 9.82973100e-01 + 0x0bcfp-15, 0x2307p-15, 0x390ep-15, 0x4d23p-15, + 0x5e98p-15, 0x6cd4p-15, 0x775bp-15, 0x7dd2p-15, }; *order = maxorder; @@ -270,12 +271,12 @@ static void quantize_rc(const float *rc, int maxorder, int *order, int *rc_q) */ static void unquantize_rc(const int *rc_q, int order, float rc[8]) { - /* Quantization table, sin(delta * i), delta = Pi / 17 */ + /* Quantization table, sin(delta * i), delta = Pi / 17, + * rounded to fixed point Q15 value (LC3-Plus HR requirements). */ static float q_inv[] = { - 0.00000000e+00, 1.83749517e-01, 3.61241664e-01, 5.26432173e-01, - 6.73695641e-01, 7.98017215e-01, 8.95163302e-01, 9.61825645e-01, - 9.95734176e-01 + 0x0000p-15, 0x1785p-15, 0x2e3dp-15, 0x4362p-15, + 0x563cp-15, 0x6625p-15, 0x7295p-15, 0x7b1dp-15, 0x7f74p-15, }; int i; diff --git a/tables/mktables.py b/tables/mktables.py index c2b8297..3b53ea8 100755 --- a/tables/mktables.py +++ b/tables/mktables.py @@ -141,9 +141,19 @@ def tns_lag_window(): def tns_quantization_table(): print('\n--- tns quantization table ---') - print_table(np.sin((np.arange(8) + 0.5) * (np.pi / 17))) - print_table(np.sin((np.arange(8)) * (np.pi / 17))) + xe = np.sin((np.arange(8) + 0.5) * (np.pi / 17)) + xe = np.rint(xe * 2**15).astype(np.int16) + + xd = np.sin(np.arange(9) * (np.pi / 17)) + xd = np.rint(xd * 2**15).astype(np.int16) + + for x in (xe, xd): + print() + for (i, xi) in enumerate(x): + print('0x{:04x}p-15,'.format(xi), end = '\n' if i%4 == 4-1 else ' ') + if len(x) % 4: + print() def quant_iq_table(): diff --git a/test/tns.py b/test/tns.py index 7a28320..4bb7b99 100644 --- a/test/tns.py +++ b/test/tns.py @@ -203,6 +203,7 @@ class TnsAnalysis(Tns): delta = np.pi / 17 rc_i = np.rint(np.arcsin(rc) / delta).astype(int) + 8 rc_q = np.sin(delta * (rc_i - 8)) + rc_q = np.rint(rc_q * 2**15) / 2**15 rc_order = len(rc_i) - np.argmin(rc_i[::-1] == 8) @@ -337,6 +338,7 @@ class TnsSynthesis(Tns): rc_order = self.rc_order[f] rc = np.sin((np.pi / 17) * (self.rc[f] - 8)) + rc = np.rint(rc * 2**15) / 2**15 if rc_order > 0: i0 = Tns.FREQ_LIM[self.dt][bw][f]