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 194c4eb519SMatthias Ringwaldimport lc3 204930cef6SMatthias Ringwaldimport tables as T, appendix_c as C 214930cef6SMatthias Ringwald 224930cef6SMatthias Ringwald### ------------------------------------------------------------------------ ### 234930cef6SMatthias Ringwald 244930cef6SMatthias Ringwaldclass EnergyBand: 254930cef6SMatthias Ringwald 264930cef6SMatthias Ringwald def __init__(self, dt, sr): 274930cef6SMatthias Ringwald 284930cef6SMatthias Ringwald self.dt = dt 294930cef6SMatthias Ringwald self.I = T.I[dt][sr] 304930cef6SMatthias Ringwald 314930cef6SMatthias Ringwald def compute(self, x): 324930cef6SMatthias Ringwald 334930cef6SMatthias Ringwald e = [ np.mean(np.square(x[self.I[i]:self.I[i+1]])) 344930cef6SMatthias Ringwald for i in range(len(self.I)-1) ] 354930cef6SMatthias Ringwald 36*6897da5cSDirk Helbig e_lo = np.sum(e[:len(e) - [2, 3, 4, 2][self.dt]]) 37*6897da5cSDirk Helbig e_hi = np.sum(e[len(e) - [2, 3, 4, 2][self.dt]:]) 384930cef6SMatthias Ringwald 39*6897da5cSDirk Helbig return e, (e_hi > 30*e_lo) 404930cef6SMatthias Ringwald 414930cef6SMatthias Ringwald### ------------------------------------------------------------------------ ### 424930cef6SMatthias Ringwald 434930cef6SMatthias Ringwalddef check_unit(rng, dt, sr): 444930cef6SMatthias Ringwald 454930cef6SMatthias Ringwald ns = T.NS[dt][sr] 464930cef6SMatthias Ringwald ok = True 474930cef6SMatthias Ringwald 484930cef6SMatthias Ringwald nrg = EnergyBand(dt, sr) 494930cef6SMatthias Ringwald 504930cef6SMatthias Ringwald x = (2 * rng.random(T.NS[dt][sr])) - 1 514930cef6SMatthias Ringwald 524930cef6SMatthias Ringwald (e , nn ) = nrg.compute(x) 534930cef6SMatthias Ringwald (e_c, nn_c) = lc3.energy_compute(dt, sr, x) 544930cef6SMatthias Ringwald ok = ok and np.amax(np.abs(e_c - e)) < 1e-5 and nn_c == nn 554930cef6SMatthias Ringwald 564930cef6SMatthias Ringwald x[15*ns//16:] *= 1e2; 574930cef6SMatthias Ringwald 584930cef6SMatthias Ringwald (e , nn ) = nrg.compute(x) 594930cef6SMatthias Ringwald (e_c, nn_c) = lc3.energy_compute(dt, sr, x) 604930cef6SMatthias Ringwald ok = ok and np.amax(np.abs(e_c - e)) < 1e-3 and nn_c == nn 614930cef6SMatthias Ringwald 624930cef6SMatthias Ringwald return ok 634930cef6SMatthias Ringwald 644930cef6SMatthias Ringwalddef check_appendix_c(dt): 654930cef6SMatthias Ringwald 66*6897da5cSDirk Helbig i0 = dt - T.DT_7M5 674930cef6SMatthias Ringwald sr = T.SRATE_16K 68*6897da5cSDirk Helbig 694930cef6SMatthias Ringwald ok = True 704930cef6SMatthias Ringwald 71*6897da5cSDirk Helbig e = lc3.energy_compute(dt, sr, C.X[i0][0])[0] 72*6897da5cSDirk Helbig ok = ok and np.amax(np.abs(1 - e/C.E_B[i0][0])) < 1e-6 734930cef6SMatthias Ringwald 74*6897da5cSDirk Helbig e = lc3.energy_compute(dt, sr, C.X[i0][1])[0] 75*6897da5cSDirk Helbig ok = ok and np.amax(np.abs(1 - e/C.E_B[i0][1])) < 1e-6 764930cef6SMatthias Ringwald 774930cef6SMatthias Ringwald return ok 784930cef6SMatthias Ringwald 794930cef6SMatthias Ringwalddef check(): 804930cef6SMatthias Ringwald 814930cef6SMatthias Ringwald rng = np.random.default_rng(1234) 824930cef6SMatthias Ringwald 834930cef6SMatthias Ringwald ok = True 844930cef6SMatthias Ringwald 854930cef6SMatthias Ringwald for dt in range(T.NUM_DT): 86*6897da5cSDirk Helbig for sr in range(T.SRATE_8K, T.SRATE_48K + 1): 874930cef6SMatthias Ringwald ok = ok and check_unit(rng, dt, sr) 884930cef6SMatthias Ringwald 89*6897da5cSDirk Helbig for dt in ( T.DT_2M5, T.DT_5M, T.DT_10M ): 90*6897da5cSDirk Helbig for sr in ( T.SRATE_48K_HR, T.SRATE_96K_HR ): 91*6897da5cSDirk Helbig ok = ok and check_unit(rng, dt, sr) 92*6897da5cSDirk Helbig 93*6897da5cSDirk Helbig for dt in ( T.DT_7M5, T.DT_10M ): 944930cef6SMatthias Ringwald ok = ok and check_appendix_c(dt) 954930cef6SMatthias Ringwald 964930cef6SMatthias Ringwald return ok 974930cef6SMatthias Ringwald 984930cef6SMatthias Ringwald### ------------------------------------------------------------------------ ### 99