xref: /btstack/3rd-party/lc3-google/test/bwdet.py (revision 4c4eb519208b4224604d94b3ed1931841ddd93bb)
14930cef6SMatthias Ringwald#
24930cef6SMatthias Ringwald# Copyright 2022 Google LLC
34930cef6SMatthias Ringwald#
44930cef6SMatthias Ringwald# Licensed under the Apache License, Version 2.0 (the "License");
54930cef6SMatthias Ringwald# you may not use this file except in compliance with the License.
64930cef6SMatthias Ringwald# You may obtain a copy of the License at
74930cef6SMatthias Ringwald#
84930cef6SMatthias Ringwald#     http://www.apache.org/licenses/LICENSE-2.0
94930cef6SMatthias Ringwald#
104930cef6SMatthias Ringwald# Unless required by applicable law or agreed to in writing, software
114930cef6SMatthias Ringwald# distributed under the License is distributed on an "AS IS" BASIS,
124930cef6SMatthias Ringwald# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134930cef6SMatthias Ringwald# See the License for the specific language governing permissions and
144930cef6SMatthias Ringwald# limitations under the License.
154930cef6SMatthias Ringwald#
164930cef6SMatthias Ringwald
174930cef6SMatthias Ringwaldimport numpy as np
184930cef6SMatthias Ringwald
19*4c4eb519SMatthias Ringwaldimport lc3
204930cef6SMatthias Ringwaldimport tables as T, appendix_c as C
214930cef6SMatthias Ringwald
224930cef6SMatthias Ringwald
234930cef6SMatthias RingwaldBW_START = [
244930cef6SMatthias Ringwald    [ [], [ 51 ], [ 45, 58 ], [ 42, 53, 60 ], [ 40, 51, 57, 61 ] ],
254930cef6SMatthias Ringwald    [ [], [ 53 ], [ 47, 59 ], [ 44, 54, 60 ], [ 41, 51, 57, 61 ] ]
264930cef6SMatthias Ringwald]
274930cef6SMatthias Ringwald
284930cef6SMatthias RingwaldBW_STOP = [
294930cef6SMatthias Ringwald    [ [], [ 63 ], [ 55, 63 ], [ 51, 58, 63 ], [ 48, 55, 60, 63 ] ],
304930cef6SMatthias Ringwald    [ [], [ 63 ], [ 56, 63 ], [ 52, 59, 63 ], [ 49, 55, 60, 63 ] ]
314930cef6SMatthias Ringwald]
324930cef6SMatthias Ringwald
334930cef6SMatthias RingwaldTQ = [ 20, 10, 10, 10 ]
344930cef6SMatthias Ringwald
354930cef6SMatthias RingwaldTC = [ 15, 23, 20, 20 ]
364930cef6SMatthias RingwaldL  = [ [ 4, 4, 3, 2 ], [ 4, 4, 3, 1 ] ]
374930cef6SMatthias Ringwald
384930cef6SMatthias Ringwald
394930cef6SMatthias Ringwald### ------------------------------------------------------------------------ ###
404930cef6SMatthias Ringwald
414930cef6SMatthias Ringwaldclass BandwidthDetector:
424930cef6SMatthias Ringwald
434930cef6SMatthias Ringwald    def __init__(self, dt, sr):
444930cef6SMatthias Ringwald
454930cef6SMatthias Ringwald        self.dt = dt
464930cef6SMatthias Ringwald        self.sr = sr
474930cef6SMatthias Ringwald
484930cef6SMatthias Ringwald    def run(self, e):
494930cef6SMatthias Ringwald
504930cef6SMatthias Ringwald        dt = self.dt
514930cef6SMatthias Ringwald        sr = self.sr
524930cef6SMatthias Ringwald
534930cef6SMatthias Ringwald        ### Stage 1, determine bw0 candidate
544930cef6SMatthias Ringwald
554930cef6SMatthias Ringwald        bw0 = 0
564930cef6SMatthias Ringwald
574930cef6SMatthias Ringwald        for bw in range(sr):
584930cef6SMatthias Ringwald            i0 = BW_START[dt][sr][bw]
594930cef6SMatthias Ringwald            i1 = BW_STOP[dt][sr][bw]
604930cef6SMatthias Ringwald            if np.mean(e[i0:i1+1]) >= TQ[bw]:
614930cef6SMatthias Ringwald                bw0 = bw + 1
624930cef6SMatthias Ringwald
634930cef6SMatthias Ringwald        ### Stage 2, Cut-off random coefficients at each steps
644930cef6SMatthias Ringwald
654930cef6SMatthias Ringwald        bw = bw0
664930cef6SMatthias Ringwald
674930cef6SMatthias Ringwald        if bw0 < sr:
684930cef6SMatthias Ringwald            l  = L[dt][bw0]
694930cef6SMatthias Ringwald            i0 = BW_START[dt][sr][bw0] - l
704930cef6SMatthias Ringwald            i1 = BW_START[dt][sr][bw0]
714930cef6SMatthias Ringwald
724930cef6SMatthias Ringwald            c = 10 * np.log10(1e-31 + e[i0-l+1:i1-l+2] / e[i0+1:i1+2])
734930cef6SMatthias Ringwald            if np.amax(c) <= TC[bw0]:
744930cef6SMatthias Ringwald                bw = sr
754930cef6SMatthias Ringwald
764930cef6SMatthias Ringwald        self.bw = bw
774930cef6SMatthias Ringwald        return self.bw
784930cef6SMatthias Ringwald
794930cef6SMatthias Ringwald    def get_nbits(self):
804930cef6SMatthias Ringwald
814930cef6SMatthias Ringwald        return 0 if self.sr == 0 else \
824930cef6SMatthias Ringwald               1 + np.log2(self.sr).astype(int)
834930cef6SMatthias Ringwald
84*4c4eb519SMatthias Ringwald    def store(self, b):
854930cef6SMatthias Ringwald
864930cef6SMatthias Ringwald        b.write_uint(self.bw, self.get_nbits())
874930cef6SMatthias Ringwald
88*4c4eb519SMatthias Ringwald    def get(self, b):
894930cef6SMatthias Ringwald
904930cef6SMatthias Ringwald        return b.read_uint(self.get_nbits())
914930cef6SMatthias Ringwald
924930cef6SMatthias Ringwald### ------------------------------------------------------------------------ ###
934930cef6SMatthias Ringwald
944930cef6SMatthias Ringwalddef check_unit(rng, dt, sr):
954930cef6SMatthias Ringwald
964930cef6SMatthias Ringwald    ok = True
974930cef6SMatthias Ringwald
984930cef6SMatthias Ringwald    bwdet = BandwidthDetector(dt, sr)
994930cef6SMatthias Ringwald
1004930cef6SMatthias Ringwald    for bw0 in range(sr+1):
1014930cef6SMatthias Ringwald        for drop in range(10):
1024930cef6SMatthias Ringwald
1034930cef6SMatthias Ringwald            ### Generate random 'high' energy and
1044930cef6SMatthias Ringwald            ### scale relevant bands to select 'bw0'
1054930cef6SMatthias Ringwald
1064930cef6SMatthias Ringwald            e = 20 + 100 * rng.random(64)
1074930cef6SMatthias Ringwald
1084930cef6SMatthias Ringwald            for i in range(sr):
1094930cef6SMatthias Ringwald                if i+1 != bw0:
1104930cef6SMatthias Ringwald                    i0 = BW_START[dt][sr][i]
1114930cef6SMatthias Ringwald                    i1 = BW_STOP[dt][sr][i]
1124930cef6SMatthias Ringwald                    e[i0:i1+1] /= (np.mean(e[i0:i1+1]) / TQ[i] + 1e-3)
1134930cef6SMatthias Ringwald
1144930cef6SMatthias Ringwald            ### Stage 2 Condition,
1154930cef6SMatthias Ringwald            ### cut-off random coefficients at each steps
1164930cef6SMatthias Ringwald
1174930cef6SMatthias Ringwald            if bw0 < sr:
1184930cef6SMatthias Ringwald                l  = L[dt][bw0]
1194930cef6SMatthias Ringwald                i0 = BW_START[dt][sr][bw0] - l
1204930cef6SMatthias Ringwald                i1 = BW_START[dt][sr][bw0]
1214930cef6SMatthias Ringwald
1224930cef6SMatthias Ringwald                e[i0-l+1:i1+2] /= np.power(10, np.arange(2*l+1) / (1 + drop))
1234930cef6SMatthias Ringwald
1244930cef6SMatthias Ringwald            ### Check with implementation
1254930cef6SMatthias Ringwald
1264930cef6SMatthias Ringwald            bw_c = lc3.bwdet_run(dt, sr, e)
1274930cef6SMatthias Ringwald
1284930cef6SMatthias Ringwald            ok = ok and bw_c == bwdet.run(e)
1294930cef6SMatthias Ringwald
1304930cef6SMatthias Ringwald    return ok
1314930cef6SMatthias Ringwald
1324930cef6SMatthias Ringwalddef check_appendix_c(dt):
1334930cef6SMatthias Ringwald
1344930cef6SMatthias Ringwald    sr = T.SRATE_16K
1354930cef6SMatthias Ringwald    ok = True
1364930cef6SMatthias Ringwald
1374930cef6SMatthias Ringwald    E_B  = C.E_B[dt]
1384930cef6SMatthias Ringwald    P_BW = C.P_BW[dt]
1394930cef6SMatthias Ringwald
1404930cef6SMatthias Ringwald    bw = lc3.bwdet_run(dt, sr, E_B[0])
1414930cef6SMatthias Ringwald    ok = ok and bw == P_BW[0]
1424930cef6SMatthias Ringwald
1434930cef6SMatthias Ringwald    bw = lc3.bwdet_run(dt, sr, E_B[1])
1444930cef6SMatthias Ringwald    ok = ok and bw == P_BW[1]
1454930cef6SMatthias Ringwald
1464930cef6SMatthias Ringwald    return ok
1474930cef6SMatthias Ringwald
1484930cef6SMatthias Ringwalddef check():
1494930cef6SMatthias Ringwald
1504930cef6SMatthias Ringwald    rng = np.random.default_rng(1234)
1514930cef6SMatthias Ringwald
1524930cef6SMatthias Ringwald    ok = True
1534930cef6SMatthias Ringwald    for dt in range(T.NUM_DT):
1544930cef6SMatthias Ringwald        for sr in range(T.NUM_SRATE):
1554930cef6SMatthias Ringwald            ok = ok and check_unit(rng, dt, sr)
1564930cef6SMatthias Ringwald
1574930cef6SMatthias Ringwald    for dt in range(T.NUM_DT):
1584930cef6SMatthias Ringwald        ok = ok and check_appendix_c(dt)
1594930cef6SMatthias Ringwald
1604930cef6SMatthias Ringwald    return ok
1614930cef6SMatthias Ringwald
1624930cef6SMatthias Ringwald### ------------------------------------------------------------------------ ###
163