1#!/usr/bin/env python3 2# 3# Copyright 2022 Google LLC 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18import numpy as np 19 20LTPF_H12K8 = np.array([ 21 -2.04305583e-05, -4.46345894e-05, -7.16366399e-05, -1.00101113e-04, 22 -1.28372848e-04, -1.54543830e-04, -1.76544567e-04, -1.92256960e-04, 23 -1.99643819e-04, -1.96888686e-04, -1.82538332e-04, -1.55639427e-04, 24 -1.15860365e-04, -6.35893034e-05, 2.81006480e-19, 7.29218021e-05, 25 1.52397076e-04, 2.34920777e-04, 3.16378650e-04, 3.92211738e-04, 26 4.57623849e-04, 5.07824294e-04, 5.38295523e-04, 5.45072918e-04, 27 5.25022155e-04, 4.76098424e-04, 3.97571380e-04, 2.90200217e-04, 28 1.56344667e-04, -5.81880142e-19, -1.73252713e-04, -3.56385965e-04, 29 -5.41155231e-04, -7.18414023e-04, -8.78505232e-04, -1.01171451e-03, 30 -1.10876706e-03, -1.16134522e-03, -1.16260169e-03, -1.10764097e-03, 31 -9.93941563e-04, -8.21692190e-04, -5.94017766e-04, -3.17074654e-04, 32 9.74695082e-19, 3.45293760e-04, 7.04480871e-04, 1.06133447e-03, 33 1.39837473e-03, 1.69763080e-03, 1.94148675e-03, 2.11357591e-03, 34 2.19968245e-03, 2.18860625e-03, 2.07294546e-03, 1.84975249e-03, 35 1.52102188e-03, 1.09397426e-03, 5.81108062e-04, -1.42248266e-18, 36 -6.27153730e-04, -1.27425140e-03, -1.91223839e-03, -2.51026925e-03, 37 -3.03703830e-03, -3.46222687e-03, -3.75800672e-03, -3.90053247e-03, 38 -3.87135231e-03, -3.65866558e-03, -3.25835851e-03, -2.67475555e-03, 39 -1.92103305e-03, -1.01925433e-03, 1.86962369e-18, 1.09841545e-03, 40 2.23113197e-03, 3.34830927e-03, 4.39702277e-03, 5.32342672e-03, 41 6.07510531e-03, 6.60352025e-03, 6.86645399e-03, 6.83034270e-03, 42 6.47239234e-03, 5.78237521e-03, 4.76401273e-03, 3.43586351e-03, 43 1.83165284e-03, -2.25189837e-18, -1.99647619e-03, -4.08266886e-03, 44 -6.17308037e-03, -8.17444895e-03, -9.98882386e-03, -1.15169871e-02, 45 -1.26621006e-02, -1.33334458e-02, -1.34501120e-02, -1.29444881e-02, 46 -1.17654154e-02, -9.88086732e-03, -7.28003640e-03, -3.97473021e-03, 47 2.50961778e-18, 4.58604422e-03, 9.70324900e-03, 1.52512477e-02, 48 2.11120585e-02, 2.71533724e-02, 3.32324245e-02, 3.92003203e-02, 49 4.49066644e-02, 5.02043309e-02, 5.49542017e-02, 5.90297032e-02, 50 6.23209727e-02, 6.47385023e-02, 6.62161245e-02, 6.67132287e-02, 51 6.62161245e-02, 6.47385023e-02, 6.23209727e-02, 5.90297032e-02, 52 5.49542017e-02, 5.02043309e-02, 4.49066644e-02, 3.92003203e-02, 53 3.32324245e-02, 2.71533724e-02, 2.11120585e-02, 1.52512477e-02, 54 9.70324900e-03, 4.58604422e-03, 2.50961778e-18, -3.97473021e-03, 55 -7.28003640e-03, -9.88086732e-03, -1.17654154e-02, -1.29444881e-02, 56 -1.34501120e-02, -1.33334458e-02, -1.26621006e-02, -1.15169871e-02, 57 -9.98882386e-03, -8.17444895e-03, -6.17308037e-03, -4.08266886e-03, 58 -1.99647619e-03, -2.25189837e-18, 1.83165284e-03, 3.43586351e-03, 59 4.76401273e-03, 5.78237521e-03, 6.47239234e-03, 6.83034270e-03, 60 6.86645399e-03, 6.60352025e-03, 6.07510531e-03, 5.32342672e-03, 61 4.39702277e-03, 3.34830927e-03, 2.23113197e-03, 1.09841545e-03, 62 1.86962369e-18, -1.01925433e-03, -1.92103305e-03, -2.67475555e-03, 63 -3.25835851e-03, -3.65866558e-03, -3.87135231e-03, -3.90053247e-03, 64 -3.75800672e-03, -3.46222687e-03, -3.03703830e-03, -2.51026925e-03, 65 -1.91223839e-03, -1.27425140e-03, -6.27153730e-04, -1.42248266e-18, 66 5.81108062e-04, 1.09397426e-03, 1.52102188e-03, 1.84975249e-03, 67 2.07294546e-03, 2.18860625e-03, 2.19968245e-03, 2.11357591e-03, 68 1.94148675e-03, 1.69763080e-03, 1.39837473e-03, 1.06133447e-03, 69 7.04480871e-04, 3.45293760e-04, 9.74695082e-19, -3.17074654e-04, 70 -5.94017766e-04, -8.21692190e-04, -9.93941563e-04, -1.10764097e-03, 71 -1.16260169e-03, -1.16134522e-03, -1.10876706e-03, -1.01171451e-03, 72 -8.78505232e-04, -7.18414023e-04, -5.41155231e-04, -3.56385965e-04, 73 -1.73252713e-04, -5.81880142e-19, 1.56344667e-04, 2.90200217e-04, 74 3.97571380e-04, 4.76098424e-04, 5.25022155e-04, 5.45072918e-04, 75 5.38295523e-04, 5.07824294e-04, 4.57623849e-04, 3.92211738e-04, 76 3.16378650e-04, 2.34920777e-04, 1.52397076e-04, 7.29218021e-05, 77 2.81006480e-19, -6.35893034e-05, -1.15860365e-04, -1.55639427e-04, 78 -1.82538332e-04, -1.96888686e-04, -1.99643819e-04, -1.92256960e-04, 79 -1.76544567e-04, -1.54543830e-04, -1.28372848e-04, -1.00101113e-04, 80 -7.16366399e-05, -4.46345894e-05, -2.04305583e-05 81]) 82 83LTPF_HI = np.array([ 84 6.69885837e-03, 3.96711478e-02, 1.06999186e-01, 2.09880463e-01, 85 3.35690625e-01, 4.59220930e-01, 5.50075002e-01, 5.83527575e-01, 86 5.50075002e-01, 4.59220930e-01, 3.35690625e-01, 2.09880463e-01, 87 1.06999186e-01, 3.96711478e-02, 6.69885837e-03 88]) 89 90def print_table(t, m=4): 91 92 for (i, v) in enumerate(t): 93 print('{:14.8e},'.format(v), end = '\n' if i%m == m-1 else ' ') 94 95 if len(t) % 4: 96 print('') 97 98 99def mdct_fft_twiddles(): 100 101 for n in (10, 20, 30, 40, 60, 80, 90, 120, 160, 180, 240): 102 103 print('\n--- fft bf2 twiddles {:3d} ---'.format(n)) 104 105 kv = -2 * np.pi * np.arange(n // 2) / n 106 for (i, k) in enumerate(kv): 107 print('{{ {:14.7e}, {:14.7e} }},'.format(np.cos(k), np.sin(k)), 108 end = '\n' if i%2 == 1 else ' ') 109 110 for n in (15, 45): 111 112 print('\n--- fft bf3 twiddles {:3d} ---'.format(n)) 113 114 kv = -2 * np.pi * np.arange(n) / n 115 for k in kv: 116 print(('{{ {{ {:14.7e}, {:14.7e} }},' + 117 ' {{ {:14.7e}, {:14.7e} }} }},').format( 118 np.cos(k), np.sin(k), np.cos(2*k), np.sin(2*k))) 119 120 121def mdct_rot_twiddles(): 122 123 for n in (120, 160, 240, 320, 360, 480, 640, 720, 960): 124 125 print('\n--- mdct rot twiddles {:3d} ---'.format(n)) 126 127 kv = 2 * np.pi * (np.arange(n // 4) + 1/8) / n 128 scale = np.sqrt( np.sqrt( 4 / n ) ) 129 for (i, k) in enumerate(kv): 130 print('{{ {:14.7e}, {:14.7e} }},'.format( 131 np.cos(k) * scale, np.sin(k) * scale), 132 end = '\n' if i%2 == 1 else ' ') 133 134 135def mdct_scaling(): 136 137 print('\n--- mdct scaling ---') 138 ns = np.array([ [ 60, 120, 180, 240, 360], [ 80, 160, 240, 320, 480] ]) 139 print_table(np.sqrt(2 / ns[0])) 140 print_table(np.sqrt(2 / ns[1])) 141 142 143def tns_lag_window(): 144 145 print('\n--- tns lag window ---') 146 print_table(np.exp(-0.5 * (0.02 * np.pi * np.arange(9)) ** 2)) 147 148 149def tns_quantization_table(): 150 151 print('\n--- tns quantization table ---') 152 print_table(np.sin((np.arange(8) + 0.5) * (np.pi / 17))) 153 print_table(np.sin((np.arange(8)) * (np.pi / 17))) 154 155 156def quant_iq_table(): 157 158 print('\n--- quantization iq table ---') 159 print_table(10 ** (np.arange(65) / 28)) 160 161 162def sns_ge_table(): 163 164 g_tilt_table = [ 14, 18, 22, 26, 30 ] 165 166 for (sr, g_tilt) in enumerate(g_tilt_table): 167 print('\n--- sns ge table, sr:{} ---'.format(sr)) 168 print_table(10 ** ((np.arange(64) * g_tilt) / 630)) 169 170 171def inv_table(): 172 173 print('\n--- inv table ---') 174 print_table(np.append(np.zeros(1), 1 / np.arange(1, 28))) 175 176def ltpf_resampler_table(): 177 178 for sr in [ 8, 16, 32, 24, 48 ]: 179 180 r = 192 // sr 181 k = 64 if r & (r-1) else 192 182 183 p = (192 // k) * (k // sr) 184 q = p * (0.5 if sr == 8 else 1) 185 186 print('\n--- LTPF resampler {:d} KHz to 12.8 KHz ---'.format(sr)) 187 188 h = np.rint(np.append(LTPF_H12K8, 0.) * q * 2**15).astype(int) 189 h = h.reshape((len(h) // p, p)).T 190 h = np.flip(h, axis=0) 191 print('... Gain:', np.max(np.sum(np.abs(h), axis=1)) / 32768.) 192 193 for i in range(0, len(h), 192 // k): 194 for j in range(0, len(h[i]), 10): 195 print('{:5d}, {:5d}, {:5d}, {:5d}, {:5d}, ' 196 '{:5d}, {:5d}, {:5d}, {:5d}, {:5d},'.format( 197 h[i][j+0], h[i][j+1], h[i][j+2], h[i][j+3], h[i][j+4], 198 h[i][j+5], h[i][j+6], h[i][j+7], h[i][j+8], h[i][j+9])) 199 200 201def ltpf_interpolate_table(): 202 203 print('\n--- LTPF interpolation ---') 204 205 h = np.rint(np.append(LTPF_HI, 0.) * 2**15).astype(int) 206 207 h = h.reshape(len(h) // 4, 4).T 208 h = np.flip(h, axis=0) 209 print('... Gain:', np.max(np.sum(np.abs(h), axis=1)) / 32768.) 210 211 for i in range(len(h)): 212 print('{:5d}, {:5d}, {:5d}, {:5d}'.format( 213 h[i][0], h[i][1], h[i][2], h[i][3])) 214 215 216if __name__ == '__main__': 217 218 mdct_fft_twiddles() 219 mdct_rot_twiddles() 220 mdct_scaling() 221 222 inv_table() 223 sns_ge_table() 224 tns_lag_window() 225 tns_quantization_table() 226 quant_iq_table() 227 228 ltpf_resampler_table() 229 ltpf_interpolate_table() 230 231 print('') 232