xref: /btstack/3rd-party/lc3-google/tables/mktables.py (revision 6897da5c53aac5b1f90f41b5b15d0bd43d61dfff)
14930cef6SMatthias Ringwald#!/usr/bin/env python3
24930cef6SMatthias Ringwald#
34930cef6SMatthias Ringwald# Copyright 2022 Google LLC
44930cef6SMatthias Ringwald#
54930cef6SMatthias Ringwald# Licensed under the Apache License, Version 2.0 (the "License");
64930cef6SMatthias Ringwald# you may not use this file except in compliance with the License.
74930cef6SMatthias Ringwald# You may obtain a copy of the License at
84930cef6SMatthias Ringwald#
94930cef6SMatthias Ringwald#     http://www.apache.org/licenses/LICENSE-2.0
104930cef6SMatthias Ringwald#
114930cef6SMatthias Ringwald# Unless required by applicable law or agreed to in writing, software
124930cef6SMatthias Ringwald# distributed under the License is distributed on an "AS IS" BASIS,
134930cef6SMatthias Ringwald# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144930cef6SMatthias Ringwald# See the License for the specific language governing permissions and
154930cef6SMatthias Ringwald# limitations under the License.
164930cef6SMatthias Ringwald#
174930cef6SMatthias Ringwald
184930cef6SMatthias Ringwaldimport numpy as np
194930cef6SMatthias Ringwald
204930cef6SMatthias RingwaldLTPF_H12K8 = np.array([
214930cef6SMatthias Ringwald    -2.04305583e-05, -4.46345894e-05, -7.16366399e-05, -1.00101113e-04,
224930cef6SMatthias Ringwald    -1.28372848e-04, -1.54543830e-04, -1.76544567e-04, -1.92256960e-04,
234930cef6SMatthias Ringwald    -1.99643819e-04, -1.96888686e-04, -1.82538332e-04, -1.55639427e-04,
244930cef6SMatthias Ringwald    -1.15860365e-04, -6.35893034e-05,  2.81006480e-19,  7.29218021e-05,
254930cef6SMatthias Ringwald     1.52397076e-04,  2.34920777e-04,  3.16378650e-04,  3.92211738e-04,
264930cef6SMatthias Ringwald     4.57623849e-04,  5.07824294e-04,  5.38295523e-04,  5.45072918e-04,
274930cef6SMatthias Ringwald     5.25022155e-04,  4.76098424e-04,  3.97571380e-04,  2.90200217e-04,
284930cef6SMatthias Ringwald     1.56344667e-04, -5.81880142e-19, -1.73252713e-04, -3.56385965e-04,
294930cef6SMatthias Ringwald    -5.41155231e-04, -7.18414023e-04, -8.78505232e-04, -1.01171451e-03,
304930cef6SMatthias Ringwald    -1.10876706e-03, -1.16134522e-03, -1.16260169e-03, -1.10764097e-03,
314930cef6SMatthias Ringwald    -9.93941563e-04, -8.21692190e-04, -5.94017766e-04, -3.17074654e-04,
324930cef6SMatthias Ringwald     9.74695082e-19,  3.45293760e-04,  7.04480871e-04,  1.06133447e-03,
334930cef6SMatthias Ringwald     1.39837473e-03,  1.69763080e-03,  1.94148675e-03,  2.11357591e-03,
344930cef6SMatthias Ringwald     2.19968245e-03,  2.18860625e-03,  2.07294546e-03,  1.84975249e-03,
354930cef6SMatthias Ringwald     1.52102188e-03,  1.09397426e-03,  5.81108062e-04, -1.42248266e-18,
364930cef6SMatthias Ringwald    -6.27153730e-04, -1.27425140e-03, -1.91223839e-03, -2.51026925e-03,
374930cef6SMatthias Ringwald    -3.03703830e-03, -3.46222687e-03, -3.75800672e-03, -3.90053247e-03,
384930cef6SMatthias Ringwald    -3.87135231e-03, -3.65866558e-03, -3.25835851e-03, -2.67475555e-03,
394930cef6SMatthias Ringwald    -1.92103305e-03, -1.01925433e-03,  1.86962369e-18,  1.09841545e-03,
404930cef6SMatthias Ringwald     2.23113197e-03,  3.34830927e-03,  4.39702277e-03,  5.32342672e-03,
414930cef6SMatthias Ringwald     6.07510531e-03,  6.60352025e-03,  6.86645399e-03,  6.83034270e-03,
424930cef6SMatthias Ringwald     6.47239234e-03,  5.78237521e-03,  4.76401273e-03,  3.43586351e-03,
434930cef6SMatthias Ringwald     1.83165284e-03, -2.25189837e-18, -1.99647619e-03, -4.08266886e-03,
444930cef6SMatthias Ringwald    -6.17308037e-03, -8.17444895e-03, -9.98882386e-03, -1.15169871e-02,
454930cef6SMatthias Ringwald    -1.26621006e-02, -1.33334458e-02, -1.34501120e-02, -1.29444881e-02,
464930cef6SMatthias Ringwald    -1.17654154e-02, -9.88086732e-03, -7.28003640e-03, -3.97473021e-03,
474930cef6SMatthias Ringwald     2.50961778e-18,  4.58604422e-03,  9.70324900e-03,  1.52512477e-02,
484930cef6SMatthias Ringwald     2.11120585e-02,  2.71533724e-02,  3.32324245e-02,  3.92003203e-02,
494930cef6SMatthias Ringwald     4.49066644e-02,  5.02043309e-02,  5.49542017e-02,  5.90297032e-02,
504930cef6SMatthias Ringwald     6.23209727e-02,  6.47385023e-02,  6.62161245e-02,  6.67132287e-02,
514930cef6SMatthias Ringwald     6.62161245e-02,  6.47385023e-02,  6.23209727e-02,  5.90297032e-02,
524930cef6SMatthias Ringwald     5.49542017e-02,  5.02043309e-02,  4.49066644e-02,  3.92003203e-02,
534930cef6SMatthias Ringwald     3.32324245e-02,  2.71533724e-02,  2.11120585e-02,  1.52512477e-02,
544930cef6SMatthias Ringwald     9.70324900e-03,  4.58604422e-03,  2.50961778e-18, -3.97473021e-03,
554930cef6SMatthias Ringwald    -7.28003640e-03, -9.88086732e-03, -1.17654154e-02, -1.29444881e-02,
564930cef6SMatthias Ringwald    -1.34501120e-02, -1.33334458e-02, -1.26621006e-02, -1.15169871e-02,
574930cef6SMatthias Ringwald    -9.98882386e-03, -8.17444895e-03, -6.17308037e-03, -4.08266886e-03,
584930cef6SMatthias Ringwald    -1.99647619e-03, -2.25189837e-18,  1.83165284e-03,  3.43586351e-03,
594930cef6SMatthias Ringwald     4.76401273e-03,  5.78237521e-03,  6.47239234e-03,  6.83034270e-03,
604930cef6SMatthias Ringwald     6.86645399e-03,  6.60352025e-03,  6.07510531e-03,  5.32342672e-03,
614930cef6SMatthias Ringwald     4.39702277e-03,  3.34830927e-03,  2.23113197e-03,  1.09841545e-03,
624930cef6SMatthias Ringwald     1.86962369e-18, -1.01925433e-03, -1.92103305e-03, -2.67475555e-03,
634930cef6SMatthias Ringwald    -3.25835851e-03, -3.65866558e-03, -3.87135231e-03, -3.90053247e-03,
644930cef6SMatthias Ringwald    -3.75800672e-03, -3.46222687e-03, -3.03703830e-03, -2.51026925e-03,
654930cef6SMatthias Ringwald    -1.91223839e-03, -1.27425140e-03, -6.27153730e-04, -1.42248266e-18,
664930cef6SMatthias Ringwald     5.81108062e-04,  1.09397426e-03,  1.52102188e-03,  1.84975249e-03,
674930cef6SMatthias Ringwald     2.07294546e-03,  2.18860625e-03,  2.19968245e-03,  2.11357591e-03,
684930cef6SMatthias Ringwald     1.94148675e-03,  1.69763080e-03,  1.39837473e-03,  1.06133447e-03,
694930cef6SMatthias Ringwald     7.04480871e-04,  3.45293760e-04,  9.74695082e-19, -3.17074654e-04,
704930cef6SMatthias Ringwald    -5.94017766e-04, -8.21692190e-04, -9.93941563e-04, -1.10764097e-03,
714930cef6SMatthias Ringwald    -1.16260169e-03, -1.16134522e-03, -1.10876706e-03, -1.01171451e-03,
724930cef6SMatthias Ringwald    -8.78505232e-04, -7.18414023e-04, -5.41155231e-04, -3.56385965e-04,
734930cef6SMatthias Ringwald    -1.73252713e-04, -5.81880142e-19,  1.56344667e-04,  2.90200217e-04,
744930cef6SMatthias Ringwald     3.97571380e-04,  4.76098424e-04,  5.25022155e-04,  5.45072918e-04,
754930cef6SMatthias Ringwald     5.38295523e-04,  5.07824294e-04,  4.57623849e-04,  3.92211738e-04,
764930cef6SMatthias Ringwald     3.16378650e-04,  2.34920777e-04,  1.52397076e-04,  7.29218021e-05,
774930cef6SMatthias Ringwald     2.81006480e-19, -6.35893034e-05, -1.15860365e-04, -1.55639427e-04,
784930cef6SMatthias Ringwald    -1.82538332e-04, -1.96888686e-04, -1.99643819e-04, -1.92256960e-04,
794930cef6SMatthias Ringwald    -1.76544567e-04, -1.54543830e-04, -1.28372848e-04, -1.00101113e-04,
804930cef6SMatthias Ringwald    -7.16366399e-05, -4.46345894e-05, -2.04305583e-05
814930cef6SMatthias Ringwald])
824930cef6SMatthias Ringwald
834930cef6SMatthias RingwaldLTPF_HI = np.array([
844930cef6SMatthias Ringwald     6.69885837e-03,  3.96711478e-02,  1.06999186e-01,  2.09880463e-01,
854930cef6SMatthias Ringwald     3.35690625e-01,  4.59220930e-01,  5.50075002e-01,  5.83527575e-01,
864930cef6SMatthias Ringwald     5.50075002e-01,  4.59220930e-01,  3.35690625e-01,  2.09880463e-01,
874930cef6SMatthias Ringwald     1.06999186e-01,  3.96711478e-02,  6.69885837e-03
884930cef6SMatthias Ringwald])
894930cef6SMatthias Ringwald
904930cef6SMatthias Ringwalddef print_table(t, m=4):
914930cef6SMatthias Ringwald
924930cef6SMatthias Ringwald    for (i, v) in enumerate(t):
934930cef6SMatthias Ringwald        print('{:14.8e},'.format(v), end = '\n' if i%m == m-1 else ' ')
944930cef6SMatthias Ringwald
954930cef6SMatthias Ringwald    if len(t) % 4:
964930cef6SMatthias Ringwald        print('')
974930cef6SMatthias Ringwald
984930cef6SMatthias Ringwald
994930cef6SMatthias Ringwalddef mdct_fft_twiddles():
1004930cef6SMatthias Ringwald
101*6897da5cSDirk Helbig    for n in (10, 20, 30, 40, 60, 80, 90, 120, 160, 180, 240, 480):
1024930cef6SMatthias Ringwald
1034930cef6SMatthias Ringwald        print('\n--- fft bf2 twiddles {:3d} ---'.format(n))
1044930cef6SMatthias Ringwald
1054930cef6SMatthias Ringwald        kv = -2 * np.pi * np.arange(n // 2) / n
1064930cef6SMatthias Ringwald        for (i, k) in enumerate(kv):
1074930cef6SMatthias Ringwald            print('{{ {:14.7e}, {:14.7e} }},'.format(np.cos(k), np.sin(k)),
1084930cef6SMatthias Ringwald                  end = '\n' if i%2 == 1 else ' ')
1094930cef6SMatthias Ringwald
1104930cef6SMatthias Ringwald    for n in (15, 45):
1114930cef6SMatthias Ringwald
1124930cef6SMatthias Ringwald        print('\n--- fft bf3 twiddles {:3d} ---'.format(n))
1134930cef6SMatthias Ringwald
1144930cef6SMatthias Ringwald        kv = -2 * np.pi * np.arange(n) / n
1154930cef6SMatthias Ringwald        for k in kv:
1164930cef6SMatthias Ringwald            print(('{{ {{ {:14.7e}, {:14.7e} }},' +
1174930cef6SMatthias Ringwald                     ' {{ {:14.7e}, {:14.7e} }} }},').format(
1184930cef6SMatthias Ringwald                np.cos(k), np.sin(k), np.cos(2*k), np.sin(2*k)))
1194930cef6SMatthias Ringwald
1204930cef6SMatthias Ringwald
1214930cef6SMatthias Ringwalddef mdct_rot_twiddles():
1224930cef6SMatthias Ringwald
123*6897da5cSDirk Helbig    for n in (40, 80, 120, 160, 240, 320, 360, 480, 640, 720, 960, 1920):
1244930cef6SMatthias Ringwald
1254930cef6SMatthias Ringwald        print('\n--- mdct rot twiddles {:3d} ---'.format(n))
1264930cef6SMatthias Ringwald
1274930cef6SMatthias Ringwald        kv = 2 * np.pi * (np.arange(n // 4) + 1/8) / n
128*6897da5cSDirk Helbig        scale = np.sqrt( np.sqrt( 4 / n ) )
1294930cef6SMatthias Ringwald        for (i, k) in enumerate(kv):
130*6897da5cSDirk Helbig            print('{{ {:14.7e}, {:14.7e} }},'.format(
131*6897da5cSDirk Helbig                np.cos(k) * scale, np.sin(k) * scale),
1324930cef6SMatthias Ringwald                  end = '\n' if i%2 == 1 else ' ')
1334930cef6SMatthias Ringwald
1344930cef6SMatthias Ringwald
1354930cef6SMatthias Ringwalddef tns_lag_window():
1364930cef6SMatthias Ringwald
1374930cef6SMatthias Ringwald    print('\n--- tns lag window ---')
1384930cef6SMatthias Ringwald    print_table(np.exp(-0.5 * (0.02 * np.pi * np.arange(9)) ** 2))
1394930cef6SMatthias Ringwald
1404930cef6SMatthias Ringwald
1414930cef6SMatthias Ringwalddef tns_quantization_table():
1424930cef6SMatthias Ringwald
1434930cef6SMatthias Ringwald    print('\n--- tns quantization table ---')
1444930cef6SMatthias Ringwald
145*6897da5cSDirk Helbig    xe = np.sin((np.arange(8) + 0.5) * (np.pi / 17))
146*6897da5cSDirk Helbig    xe = np.rint(xe * 2**15).astype(np.int16)
147*6897da5cSDirk Helbig
148*6897da5cSDirk Helbig    xd = np.sin(np.arange(9) * (np.pi / 17))
149*6897da5cSDirk Helbig    xd = np.rint(xd * 2**15).astype(np.int16)
150*6897da5cSDirk Helbig
151*6897da5cSDirk Helbig    for x in (xe, xd):
152*6897da5cSDirk Helbig        print()
153*6897da5cSDirk Helbig        for (i, xi) in enumerate(x):
154*6897da5cSDirk Helbig            print('0x{:04x}p-15,'.format(xi), end = '\n' if i%4 == 4-1 else ' ')
155*6897da5cSDirk Helbig        if len(x) % 4:
156*6897da5cSDirk Helbig            print()
1574930cef6SMatthias Ringwald
1584930cef6SMatthias Ringwalddef quant_iq_table():
1594930cef6SMatthias Ringwald
1604930cef6SMatthias Ringwald    print('\n--- quantization iq table ---')
1614930cef6SMatthias Ringwald    print_table(10 ** (np.arange(65) / 28))
1624930cef6SMatthias Ringwald
1634930cef6SMatthias Ringwald
1644930cef6SMatthias Ringwalddef sns_ge_table():
1654930cef6SMatthias Ringwald
166*6897da5cSDirk Helbig    g_tilt_table = [ 14, 18, 22, 26, 30, 34 ]
1674930cef6SMatthias Ringwald
1684930cef6SMatthias Ringwald    for (sr, g_tilt) in enumerate(g_tilt_table):
1694930cef6SMatthias Ringwald        print('\n--- sns ge table, sr:{} ---'.format(sr))
1704930cef6SMatthias Ringwald        print_table(10 ** ((np.arange(64) * g_tilt) / 630))
1714930cef6SMatthias Ringwald
1724930cef6SMatthias Ringwald
1734930cef6SMatthias Ringwalddef inv_table():
1744930cef6SMatthias Ringwald
1754930cef6SMatthias Ringwald    print('\n--- inv table ---')
1764930cef6SMatthias Ringwald    print_table(np.append(np.zeros(1), 1 / np.arange(1, 28)))
1774930cef6SMatthias Ringwald
1784930cef6SMatthias Ringwalddef ltpf_resampler_table():
1794930cef6SMatthias Ringwald
180*6897da5cSDirk Helbig    for sr in [ 8, 16, 32, 24, 48, 96 ]:
1814930cef6SMatthias Ringwald
1824930cef6SMatthias Ringwald        r = 192 // sr
1834930cef6SMatthias Ringwald        k = 64 if r & (r-1) else 192
1844930cef6SMatthias Ringwald
1854930cef6SMatthias Ringwald        p = (192 // k) * (k // sr)
1864930cef6SMatthias Ringwald        q = p * (0.5 if sr == 8 else 1)
1874930cef6SMatthias Ringwald
1884930cef6SMatthias Ringwald        print('\n--- LTPF resampler {:d} KHz to 12.8 KHz ---'.format(sr))
1894930cef6SMatthias Ringwald
1904930cef6SMatthias Ringwald        h = np.rint(np.append(LTPF_H12K8, 0.) * q * 2**15).astype(int)
1914930cef6SMatthias Ringwald        h = h.reshape((len(h) // p, p)).T
1924930cef6SMatthias Ringwald        h = np.flip(h, axis=0)
1934930cef6SMatthias Ringwald        print('... Gain:', np.max(np.sum(np.abs(h), axis=1)) / 32768.)
1944930cef6SMatthias Ringwald
1954930cef6SMatthias Ringwald        for i in range(0, len(h), 192 // k):
1964930cef6SMatthias Ringwald            for j in range(0, len(h[i]), 10):
1974930cef6SMatthias Ringwald                print('{:5d}, {:5d}, {:5d}, {:5d}, {:5d}, '
1984930cef6SMatthias Ringwald                      '{:5d}, {:5d}, {:5d}, {:5d}, {:5d},'.format(
1994930cef6SMatthias Ringwald                    h[i][j+0], h[i][j+1], h[i][j+2], h[i][j+3], h[i][j+4],
2004930cef6SMatthias Ringwald                    h[i][j+5], h[i][j+6], h[i][j+7], h[i][j+8], h[i][j+9]))
2014930cef6SMatthias Ringwald
2024930cef6SMatthias Ringwald
2034930cef6SMatthias Ringwalddef ltpf_interpolate_table():
2044930cef6SMatthias Ringwald
2054930cef6SMatthias Ringwald    print('\n--- LTPF interpolation ---')
2064930cef6SMatthias Ringwald
2074930cef6SMatthias Ringwald    h = np.rint(np.append(LTPF_HI, 0.) * 2**15).astype(int)
2084930cef6SMatthias Ringwald
2094930cef6SMatthias Ringwald    h = h.reshape(len(h) // 4, 4).T
2104930cef6SMatthias Ringwald    h = np.flip(h, axis=0)
2114930cef6SMatthias Ringwald    print('... Gain:', np.max(np.sum(np.abs(h), axis=1)) / 32768.)
2124930cef6SMatthias Ringwald
2134930cef6SMatthias Ringwald    for i in range(len(h)):
2144930cef6SMatthias Ringwald        print('{:5d}, {:5d}, {:5d}, {:5d}'.format(
2154930cef6SMatthias Ringwald            h[i][0], h[i][1], h[i][2], h[i][3]))
2164930cef6SMatthias Ringwald
2174930cef6SMatthias Ringwald
2184930cef6SMatthias Ringwaldif __name__ == '__main__':
2194930cef6SMatthias Ringwald
2204930cef6SMatthias Ringwald    mdct_fft_twiddles()
2214930cef6SMatthias Ringwald    mdct_rot_twiddles()
2224930cef6SMatthias Ringwald
2234930cef6SMatthias Ringwald    inv_table()
2244930cef6SMatthias Ringwald    sns_ge_table()
2254930cef6SMatthias Ringwald    tns_lag_window()
2264930cef6SMatthias Ringwald    tns_quantization_table()
2274930cef6SMatthias Ringwald    quant_iq_table()
2284930cef6SMatthias Ringwald
2294930cef6SMatthias Ringwald    ltpf_resampler_table()
2304930cef6SMatthias Ringwald    ltpf_interpolate_table()
2314930cef6SMatthias Ringwald
2324930cef6SMatthias Ringwald    print('')
233