xref: /btstack/3rd-party/lc3-google/src/energy.c (revision 6897da5c53aac5b1f90f41b5b15d0bd43d61dfff)
19a19cd78SMatthias Ringwald /******************************************************************************
29a19cd78SMatthias Ringwald  *
34930cef6SMatthias Ringwald  *  Copyright 2022 Google LLC
49a19cd78SMatthias Ringwald  *
59a19cd78SMatthias Ringwald  *  Licensed under the Apache License, Version 2.0 (the "License");
69a19cd78SMatthias Ringwald  *  you may not use this file except in compliance with the License.
79a19cd78SMatthias Ringwald  *  You may obtain a copy of the License at:
89a19cd78SMatthias Ringwald  *
99a19cd78SMatthias Ringwald  *  http://www.apache.org/licenses/LICENSE-2.0
109a19cd78SMatthias Ringwald  *
119a19cd78SMatthias Ringwald  *  Unless required by applicable law or agreed to in writing, software
129a19cd78SMatthias Ringwald  *  distributed under the License is distributed on an "AS IS" BASIS,
139a19cd78SMatthias Ringwald  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149a19cd78SMatthias Ringwald  *  See the License for the specific language governing permissions and
159a19cd78SMatthias Ringwald  *  limitations under the License.
169a19cd78SMatthias Ringwald  *
179a19cd78SMatthias Ringwald  ******************************************************************************/
189a19cd78SMatthias Ringwald 
199a19cd78SMatthias Ringwald #include "energy.h"
209a19cd78SMatthias Ringwald #include "tables.h"
219a19cd78SMatthias Ringwald 
229a19cd78SMatthias Ringwald 
239a19cd78SMatthias Ringwald /**
249a19cd78SMatthias Ringwald  * Energy estimation per band
259a19cd78SMatthias Ringwald  */
lc3_energy_compute(enum lc3_dt dt,enum lc3_srate sr,const float * x,float * e)269a19cd78SMatthias Ringwald bool lc3_energy_compute(
279a19cd78SMatthias Ringwald     enum lc3_dt dt, enum lc3_srate sr, const float *x, float *e)
289a19cd78SMatthias Ringwald {
29*6897da5cSDirk Helbig     int nb = lc3_num_bands[dt][sr];
309a19cd78SMatthias Ringwald     const int *lim = lc3_band_lim[dt][sr];
319a19cd78SMatthias Ringwald 
32*6897da5cSDirk Helbig     /* Mean the square of coefficients within each band */
33*6897da5cSDirk Helbig 
34*6897da5cSDirk Helbig     float e_sum[2] = { 0, 0 };
35*6897da5cSDirk Helbig     int iband_h = nb - (const int []){
36*6897da5cSDirk Helbig         [LC3_DT_2M5] = 2, [LC3_DT_5M ] = 3,
37*6897da5cSDirk Helbig         [LC3_DT_7M5] = 4, [LC3_DT_10M] = 2 }[dt];
38*6897da5cSDirk Helbig 
39*6897da5cSDirk Helbig     for (int iband = 0, i = lim[iband]; iband < nb; iband++) {
409a19cd78SMatthias Ringwald         int ie = lim[iband+1];
419a19cd78SMatthias Ringwald         int n = ie - i;
429a19cd78SMatthias Ringwald 
439a19cd78SMatthias Ringwald         float sx2 = x[i] * x[i];
449a19cd78SMatthias Ringwald         for (i++; i < ie; i++)
459a19cd78SMatthias Ringwald             sx2 += x[i] * x[i];
469a19cd78SMatthias Ringwald 
479a19cd78SMatthias Ringwald         *e = sx2 / n;
489a19cd78SMatthias Ringwald         e_sum[iband >= iband_h] += *(e++);
499a19cd78SMatthias Ringwald     }
509a19cd78SMatthias Ringwald 
519a19cd78SMatthias Ringwald     /* Return the near nyquist flag */
529a19cd78SMatthias Ringwald 
539a19cd78SMatthias Ringwald     return e_sum[1] > 30 * e_sum[0];
549a19cd78SMatthias Ringwald }
55