LCOV - code coverage report
Current view: top level - lib_dec - hf_synth.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 8834b716eb6d7dfb881d5c69dd21cb18e1692722 Lines: 103 420 24.5 %
Date: 2025-07-09 08:36:12 Functions: 7 13 53.8 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "cnst.h"
      44             : #include "prot.h"
      45             : #include "rom_com.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*---------------------------------------------------------------------*
      49             :  * Local function prototypes
      50             :  *---------------------------------------------------------------------*/
      51             : 
      52             : static void hp400_12k8( float signal[], const int16_t lg, float mem[] );
      53             : 
      54             : static void filt_6k_7k( float signal[], const int16_t lg, float mem[] );
      55             : 
      56             : static void hf_synthesis( ZERO_BWE_DEC_HANDLE hBWE_zero, const int32_t core_brate, const int16_t output_frame, const float Aq[], const float exc[], float synth[], float synth16k[] );
      57             : 
      58             : static void hf_synthesis_amr_wb( const int32_t core_brate, const int16_t output_subfr, const float Ap[], float exc16k[], float synth_out[], float *mem_syn_hf, float *delay_syn_hf, float *mem_hp_interp, float p_r, float hf_gain_i, float til, float voice_factors, const float exc[] );
      59             : 
      60             : static void envelope( AMRWB_IO_DEC_HANDLE hAmrwb_IO, const int32_t core_brate, const float Aq[], float Ap[], float *r, float tilt0, float tilt, float voice_factor );
      61             : 
      62             : static void AdaptiveStartBand( int16_t *start_band, const int32_t core_brate, const float *lsf, const float voicing_fac, const int16_t clas, int16_t *voicing_flag, int16_t *start_band_old, float *OptCrit_old );
      63             : 
      64             : 
      65             : /*-------------------------------------------------------------------*
      66             :  * hf_synth_init()
      67             :  *
      68             :  * hf synthesis filters initialization
      69             :  * - initialization of 400 Hz high pass filter
      70             :  * - initialization of band pass 6kHz to 7kHz FIR filter
      71             :  *-------------------------------------------------------------------*/
      72             : 
      73       11598 : void hf_synth_init(
      74             :     ZERO_BWE_DEC_HANDLE hBWE_zero /* o  : zero BWE decoder handle */
      75             : )
      76             : {
      77       11598 :     hBWE_zero->seed2 = RANDOM_INITSEED;
      78             : 
      79       11598 :     set_f( hBWE_zero->mem_hp400, 0, 4 );
      80       11598 :     set_f( hBWE_zero->mem_hf, 0, ( L_FIR - 1 ) );
      81             : 
      82       11598 :     set_f( hBWE_zero->mem_syn_hf, 0.0f, M );
      83       11598 :     set_f( hBWE_zero->mem_hp_interp, 0, INTERP_3_1_MEM_LEN );
      84       11598 :     set_f( hBWE_zero->delay_syn_hf, 0, NS2SA( 16000, DELAY_CLDFB_NS ) );
      85             : 
      86             : 
      87       11598 :     return;
      88             : }
      89             : 
      90             : /*-------------------------------------------------------------------*
      91             :  * hf_synth_amr_wb_init()
      92             :  *
      93             :  * hf synthesis filters initialization
      94             :  * - initialization of 1600 Hz low pass filter
      95             :  * - initialization of band pass 6kHz to 8kHz FIR filter for noise and line resampled signals
      96             :  *-------------------------------------------------------------------*/
      97             : 
      98           9 : void hf_synth_amr_wb_init(
      99             :     AMRWB_IO_DEC_HANDLE hAmrwb_IO /* i/o: AMR-WB IO data handle                            */
     100             : )
     101             : {
     102           9 :     hAmrwb_IO->prev_r = 0.0f;
     103           9 :     hAmrwb_IO->fmerit_w_sm = 0.0f;
     104           9 :     hAmrwb_IO->frame_count = 0;
     105           9 :     hAmrwb_IO->ne_min = -30.0f;
     106           9 :     hAmrwb_IO->fmerit_m_sm = 0.0f;
     107           9 :     hAmrwb_IO->voice_fac_amr_wb_hf = 0.0f;
     108           9 :     hAmrwb_IO->unvoicing = 0.0f;
     109           9 :     hAmrwb_IO->unvoicing_sm = 1.0f;
     110           9 :     hAmrwb_IO->unvoicing_flag = 0;
     111           9 :     hAmrwb_IO->voicing_flag = 0;
     112           9 :     hAmrwb_IO->start_band_old = 160;
     113           9 :     hAmrwb_IO->OptCrit_old = 1.0f;
     114             : 
     115           9 :     return;
     116             : }
     117             : 
     118             : /*-------------------------------------------------------------------*
     119             :  * hf_synth_amr_wb_reset()
     120             :  *
     121             :  * reset of HF synthesis filters
     122             :  * - needed in switching scenarios
     123             :  *-------------------------------------------------------------------*/
     124             : 
     125           0 : void hf_synth_amr_wb_reset(
     126             :     AMRWB_IO_DEC_HANDLE hAmrwb_IO, /* i/o: AMR-WB IO data handle   */
     127             :     ZERO_BWE_DEC_HANDLE hBWE_zero  /* o  : zero BWE decoder handle */
     128             : )
     129             : {
     130             :     int16_t i;
     131             : 
     132           0 :     for ( i = 0; i < L_FRAME16k; i++ )
     133             :     {
     134           0 :         own_random( &( hBWE_zero->seed2 ) );
     135             :     }
     136             : 
     137           0 :     set_f( hBWE_zero->mem_syn_hf, 0.0f, M );
     138           0 :     set_f( hBWE_zero->delay_syn_hf, 0.0, NS2SA( 16000, DELAY_CLDFB_NS ) );
     139           0 :     set_f( hBWE_zero->mem_hp_interp, 0, INTERP_3_1_MEM_LEN );
     140             : 
     141           0 :     hAmrwb_IO->prev_r = 0.0f;
     142           0 :     hAmrwb_IO->fmerit_w_sm = 0.0f;
     143           0 :     hAmrwb_IO->frame_count = 0;
     144           0 :     hAmrwb_IO->ne_min = -30.0f;
     145           0 :     hAmrwb_IO->fmerit_m_sm = 0.0f;
     146           0 :     hAmrwb_IO->voice_fac_amr_wb_hf = 0.0f;
     147           0 :     hAmrwb_IO->unvoicing = 0.0f;
     148           0 :     hAmrwb_IO->unvoicing_sm = 1.0f;
     149           0 :     hAmrwb_IO->unvoicing_flag = 0;
     150           0 :     hAmrwb_IO->voicing_flag = 0;
     151           0 :     hAmrwb_IO->start_band_old = 160;
     152           0 :     hAmrwb_IO->OptCrit_old = 1.0f;
     153             : 
     154           0 :     return;
     155             : }
     156             : 
     157             : /*-------------------------------------------------------------------*
     158             :  * hf_synth_amr_wb()
     159             :  *
     160             :  * HF synthesis in AMR-WB IO
     161             :  *-------------------------------------------------------------------*/
     162             : 
     163           0 : void hf_synth_amr_wb(
     164             :     AMRWB_IO_DEC_HANDLE hAmrwb_IO, /* i/o: AMR-WB IO data handle                             */
     165             :     ZERO_BWE_DEC_HANDLE hBWE_zero, /* o  : zero BWE decoder handle                           */
     166             :     const int32_t core_brate,      /* i  : core bitrate                                      */
     167             :     const int16_t output_frame,    /* i  : output frame length                               */
     168             :     const float *Aq,               /* i  : quantized Az                                      */
     169             :     const float *exc,              /* i  : excitation at 12.8 kHz                            */
     170             :     float *synth,                  /* i/o: synthesis signal at 12.8k                         */
     171             :     int16_t *amr_io_class,         /* i  : signal class (determined by FEC algorithm)        */
     172             :     float *synth_out,              /* i/o: output signal at output Fs                        */
     173             :     float fmerit,                  /* i  : classify parameter from FEC                       */
     174             :     const int16_t *hf_gain,        /* i  : decoded HF gain                                   */
     175             :     const float *voice_factors,    /* i  : voicing factors                                   */
     176             :     const float pitch_buf[],       /* i  : pitch buffer                                      */
     177             :     const float ng_ener_ST,        /* i  : Noise gate - short-term energy                    */
     178             :     const float *lsf_new           /* i  : ISF vector                                        */
     179             : )
     180             : {
     181             :     const float *p_Aq;
     182             :     float *p_Ap;
     183             :     int16_t i, j, i_subfr, output_subfr;
     184             :     float Ap[( M16k + 1 ) * NB_SUBFR];
     185             :     float exc16k[L_FRAME16k], dct_exc[L_FRAME], dct_hb[L_FRAME16k];
     186             :     float ener, tmp, scale;
     187             :     float alpha, beta, sub_gain[NB_SUBFR];
     188           0 :     int16_t core_type = 1;
     189             :     float pitch_var_cur, voice_fac, fmerit_m, fmerit_w;
     190             :     int16_t start_band;
     191             :     float til[NB_SUBFR], til0[NB_SUBFR];
     192             :     float enr1, enr2;
     193             :     float *pt1, *pt2;
     194             :     float e_subfr1[NB_SUBFR], e_subfr2[NB_SUBFR], e_subfr3[NB_SUBFR];
     195             :     float HF_corr_gain[NB_SUBFR];
     196             :     float filt_weight[80];
     197             :     int16_t filt_weight_coeff;
     198             :     float gamma;
     199             :     float hb_ener, g, hb_tonal[80], tonal_ener, hb_amb[80], inv_g;
     200             :     int16_t fb, fn, signum[80];
     201             : 
     202           0 :     pt1 = (float *) synth + 1;
     203           0 :     pt2 = (float *) synth;
     204           0 :     for ( i = 0; i < NB_SUBFR; i++ )
     205             :     {
     206           0 :         enr1 = 0.0f;
     207           0 :         enr2 = 0.0f;
     208           0 :         enr1 = dotp( pt2, pt2, L_SUBFR );
     209           0 :         enr2 = dotp( pt1, pt2, ( L_SUBFR - 1 ) );
     210           0 :         til[i] = enr2 / ( enr1 + 0.1f );
     211           0 :         til0[i] = til[i];
     212           0 :         pt1 += L_SUBFR;
     213           0 :         pt2 += L_SUBFR;
     214             :     }
     215             : 
     216           0 :     output_subfr = output_frame / NB_SUBFR;
     217             : 
     218           0 :     if ( *amr_io_class != 7 )
     219             :     {
     220           0 :         core_type = 0;
     221             :     }
     222             : 
     223             :     /* modify LF parameters for excitation weighting or sub-frame gains calculating */
     224           0 :     pitch_var_cur = (float) ( fabs( pitch_buf[0] - pitch_buf[1] ) + fabs( pitch_buf[1] - pitch_buf[2] ) + fabs( pitch_buf[2] - pitch_buf[3] ) );
     225             : 
     226           0 :     if ( hAmrwb_IO->frame_count > FRAME_COUNT_HF_SYNTH && *amr_io_class == UNVOICED_CLAS )
     227             :     {
     228           0 :         hAmrwb_IO->frame_count = 0;
     229           0 :         hAmrwb_IO->ne_min = -30.0f;
     230             :     }
     231             :     else
     232             :     {
     233           0 :         if ( hAmrwb_IO->frame_count > 2 * FRAME_COUNT_HF_SYNTH )
     234             :         {
     235           0 :             hAmrwb_IO->frame_count = 2 * FRAME_COUNT_HF_SYNTH;
     236             :         }
     237             :         else
     238             :         {
     239           0 :             ( hAmrwb_IO->frame_count )++;
     240             :         }
     241             : 
     242           0 :         if ( ng_ener_ST < hAmrwb_IO->ne_min )
     243             :         {
     244           0 :             hAmrwb_IO->ne_min = ng_ener_ST;
     245             :         }
     246             :     }
     247             : 
     248           0 :     voice_fac = 0.0f;
     249           0 :     for ( i = 0; i < NB_SUBFR; i++ )
     250             :     {
     251           0 :         voice_fac += voice_factors[i];
     252             :     }
     253           0 :     voice_fac *= 0.25f;
     254             : 
     255           0 :     fmerit_w = fmerit > 0.35f ? 0.35f : ( fmerit < 0.15f ? 0.15f : fmerit );
     256           0 :     if ( core_type == 1 )
     257             :     {
     258           0 :         fmerit_w *= 0.5f;
     259             :     }
     260             : 
     261           0 :     fmerit_w *= ( 1.0f + voice_fac );
     262           0 :     hAmrwb_IO->fmerit_w_sm = 0.9f * ( hAmrwb_IO->fmerit_w_sm ) + 0.1f * fmerit_w;
     263           0 :     fmerit_w = hAmrwb_IO->fmerit_w_sm;
     264             : 
     265           0 :     fmerit_m = ( 2.0f - ( fmerit < 0.5f ? 1.0f : fmerit ) );
     266           0 :     hAmrwb_IO->fmerit_m_sm = 0.5f * ( hAmrwb_IO->fmerit_m_sm ) + 0.5f * fmerit_m;
     267           0 :     fmerit_m = hAmrwb_IO->fmerit_m_sm;
     268             : 
     269           0 :     for ( i = 0; i < NB_SUBFR; i++ )
     270             :     {
     271           0 :         if ( pitch_var_cur < 10 && ( til[i] ) < 0 )
     272             :         {
     273           0 :             til[i] = 0.2f;
     274             :         }
     275             : 
     276           0 :         til[i] = ( 1.0f - ( til[i] ) ) < 0.8f ? 0.8f : ( 1.0f - ( til[i] ) );
     277           0 :         til[i] += ( 30.0f + hAmrwb_IO->ne_min ) * 0.007f;
     278           0 :         til[i] *= fmerit_m;
     279             :     }
     280             : 
     281             :     /* predict LPC coefficients and calculate sub-frame gains */
     282           0 :     p_Aq = Aq;
     283           0 :     p_Ap = Ap;
     284           0 :     for ( i = 0; i < NB_SUBFR; i++ )
     285             :     {
     286           0 :         envelope( hAmrwb_IO, core_brate, p_Aq, p_Ap, &sub_gain[i], til0[i], til[i], voice_factors[i] );
     287             : 
     288           0 :         p_Aq += ( M + 1 );
     289           0 :         p_Ap += ( M + 1 );
     290             :     }
     291             : 
     292             :     /* rate dependent adaptive start band */
     293           0 :     AdaptiveStartBand( &start_band, core_brate, lsf_new, voice_fac, *amr_io_class, &hAmrwb_IO->voicing_flag, &hAmrwb_IO->start_band_old, &hAmrwb_IO->OptCrit_old );
     294             : 
     295             :     /* DCT transform of LF excitation */
     296           0 :     edct( exc, dct_exc, L_FRAME, EVS_MONO );
     297             : 
     298           0 :     set_f( dct_hb, 0.0f, L_FRAME16k );
     299             : 
     300             :     /* copy excitation from LF */
     301           0 :     for ( i = 200; i < 240; i++ )
     302             :     {
     303           0 :         dct_hb[i] = dct_exc[i];
     304             :     }
     305             : 
     306           0 :     hb_ener = 0.01f;
     307           0 :     for ( i = 240; i < L_FRAME16k; i++ )
     308             :     {
     309           0 :         dct_hb[i] = dct_exc[i + start_band - 240];
     310           0 :         signum[i - 240] = 1;
     311           0 :         if ( dct_hb[i] < 0 )
     312             :         {
     313           0 :             signum[i - 240] = -1;
     314             :         }
     315           0 :         dct_hb[i] *= signum[i - 240];
     316           0 :         hb_ener += dct_hb[i] * dct_hb[i];
     317             :     }
     318             : 
     319           0 :     fmerit_w *= ( 1.1f - start_band * 0.00625f );
     320           0 :     alpha = (float) sqrt( fmerit_w );
     321           0 :     beta = 1.0f - fmerit_w;
     322           0 :     gamma = max( 0.3f, min( 1.0f, ( 1.05f - alpha * 0.95f ) ) );
     323           0 :     tonal_ener = 0.01f;
     324           0 :     for ( i = 0; i < 8; i++ )
     325             :     {
     326           0 :         fb = 0;
     327           0 :         fn = i + 8;
     328           0 :         tmp = 0;
     329           0 :         for ( j = 0; j < fn; j++ )
     330             :         {
     331           0 :             tmp += dct_hb[j + 240];
     332             :         }
     333           0 :         hb_amb[i] = tmp / fn;
     334           0 :         hb_tonal[i] = dct_hb[i + 240] - hb_amb[i];
     335           0 :         if ( hb_tonal[i] > 0 )
     336             :         {
     337           0 :             tonal_ener += hb_tonal[i] * hb_tonal[i];
     338             :         }
     339             :     }
     340           0 :     for ( ; i < L_SUBFR16k - 8; i++ )
     341             :     {
     342           0 :         fb = i - 7;
     343           0 :         tmp = 0;
     344           0 :         for ( j = fb; j < fb + 15; j++ )
     345             :         {
     346           0 :             tmp += dct_hb[j + 240];
     347             :         }
     348           0 :         hb_amb[i] = tmp / 15;
     349           0 :         hb_tonal[i] = dct_hb[i + 240] - hb_amb[i];
     350           0 :         if ( hb_tonal[i] > 0 )
     351             :         {
     352           0 :             tonal_ener += hb_tonal[i] * hb_tonal[i];
     353             :         }
     354             :     }
     355           0 :     for ( ; i < L_SUBFR16k; i++ )
     356             :     {
     357           0 :         fb = i - 7;
     358           0 :         fn = L_SUBFR16k - i + 7;
     359           0 :         tmp = 0;
     360           0 :         for ( j = fb; j < L_SUBFR16k; j++ )
     361             :         {
     362           0 :             tmp += dct_hb[j + 240];
     363             :         }
     364           0 :         hb_amb[i] = tmp / fn;
     365           0 :         hb_tonal[i] = dct_hb[i + 240] - hb_amb[i];
     366           0 :         if ( hb_tonal[i] > 0 )
     367             :         {
     368           0 :             tonal_ener += hb_tonal[i] * hb_tonal[i];
     369             :         }
     370             :     }
     371           0 :     g = beta * ( hb_ener - tonal_ener ) / ( hb_ener - beta * tonal_ener );
     372           0 :     if ( g < 0.01f && g > -0.01f )
     373             :     {
     374           0 :         inv_g = sign( g ) * 100;
     375             :     }
     376             :     else
     377             :     {
     378           0 :         inv_g = 1 / g;
     379             :     }
     380           0 :     ener = 0.01f;
     381           0 :     for ( i = 0; i < L_SUBFR16k; i++ )
     382             :     {
     383           0 :         if ( hb_tonal[i] > 0 )
     384             :         {
     385           0 :             hb_tonal[i] *= g;
     386             :         }
     387           0 :         hb_amb[i] *= inv_g;
     388           0 :         dct_hb[i + 240] = hb_tonal[i] + hb_amb[i];
     389           0 :         dct_hb[i + 240] *= signum[i];
     390           0 :         ener += dct_hb[i + 240] * dct_hb[i + 240];
     391             :     }
     392           0 :     scale = (float) ( gamma * sqrt( hb_ener / ener ) );
     393             : 
     394           0 :     if ( core_brate == ACELP_6k60 )
     395             :     {
     396           0 :         filt_weight_coeff = 60;
     397             :     }
     398           0 :     else if ( core_brate == ACELP_8k85 )
     399             :     {
     400           0 :         filt_weight_coeff = 40;
     401             :     }
     402             :     else
     403             :     {
     404           0 :         filt_weight_coeff = 20;
     405             :     }
     406             : 
     407           0 :     for ( i = 0; i < filt_weight_coeff; i++ )
     408             :     {
     409           0 :         filt_weight[i] = (float) ( -0.999 / ( filt_weight_coeff - 1 ) ) * i + 1.0f;
     410             :     }
     411             : 
     412           0 :     for ( i = 240; i < L_FRAME16k; i++ )
     413             :     {
     414           0 :         dct_hb[i] *= scale;
     415             : 
     416           0 :         if ( core_brate < ACELP_23k85 && i > 255 )
     417             :         {
     418           0 :             dct_hb[i] *= 0.59525f;
     419             :         }
     420             : 
     421           0 :         if ( i >= L_FRAME16k - filt_weight_coeff )
     422             :         {
     423           0 :             dct_hb[i] *= filt_weight[i - L_FRAME16k + filt_weight_coeff];
     424             :         }
     425             :     }
     426             : 
     427           0 :     for ( i = 200; i < 256; i++ )
     428             :     {
     429           0 :         dct_hb[i] *= filt_hp[i - 200];
     430             : 
     431           0 :         if ( core_brate < ACELP_23k85 )
     432             :         {
     433           0 :             dct_hb[i] *= deem_tab[i - 200];
     434             :         }
     435             :     }
     436             : 
     437           0 :     if ( core_brate == ACELP_23k85 )
     438             :     {
     439           0 :         for ( i = 0; i < NB_SUBFR; i++ )
     440             :         {
     441           0 :             HF_corr_gain[i] = 2 * HP_gain[hf_gain[i]];
     442             :         }
     443             :     }
     444             : 
     445             :     /* inverse DCT transform of HF excitation */
     446           0 :     set_f( exc16k, 0.0f, L_FRAME16k );
     447           0 :     edct( dct_hb, exc16k, L_FRAME16k, EVS_MONO );
     448             : 
     449             :     /* energy weighting in consecutive subframes */
     450           0 :     ener = sum2_f( exc, L_FRAME ) + 0.01f;
     451           0 :     tmp = sum2_f( exc16k, L_FRAME16k ) + 0.01f;
     452             : 
     453           0 :     for ( i = 0; i < NB_SUBFR; i++ )
     454             :     {
     455           0 :         e_subfr1[i] = sum2_f( &exc[i * L_SUBFR], L_SUBFR ) + 0.01f;
     456           0 :         e_subfr2[i] = sum2_f( &exc16k[i * L_SUBFR16k], L_SUBFR16k ) + 0.01f;
     457           0 :         e_subfr3[i] = ( e_subfr1[i] / ener ) * tmp;
     458             : 
     459           0 :         for ( j = i * L_SUBFR16k; j < ( i + 1 ) * L_SUBFR16k; j++ )
     460             :         {
     461           0 :             exc16k[j] *= (float) sqrt( e_subfr3[i] / e_subfr2[i] );
     462             :         }
     463             :     }
     464             : 
     465           0 :     p_Ap = Ap;
     466           0 :     i = 0;
     467           0 :     for ( i_subfr = 0; i_subfr < L_FRAME16k; i_subfr += L_SUBFR16k )
     468             :     {
     469             :         /* synthesis of the HF signal */
     470           0 :         hf_synthesis_amr_wb( core_brate, output_subfr, p_Ap, &exc16k[i_subfr], &synth_out[i * output_subfr], hBWE_zero->mem_syn_hf, hBWE_zero->delay_syn_hf, hBWE_zero->mem_hp_interp, sub_gain[i], HF_corr_gain[i], til0[i], voice_factors[i], &exc[i * L_SUBFR] );
     471             : 
     472           0 :         p_Ap += ( M + 1 );
     473           0 :         i++;
     474             :     }
     475             : 
     476           0 :     return;
     477             : }
     478             : 
     479             : /*-----------------------------------------------------------------------------------*
     480             :  * hf_synthesis_amr_wb()
     481             :  *
     482             :  * HF noise synthesis
     483             :  * - Generate HF noise between 6 and 8 kHz, mix it with signal obtained by linear resampling.
     484             :  * - Set energy of high band
     485             :  *-----------------------------------------------------------------------------------*/
     486             : 
     487           0 : static void hf_synthesis_amr_wb(
     488             :     const int32_t core_brate,   /* i  : core bitrate                    */
     489             :     const int16_t output_subfr, /* i  : output sub-frame length         */
     490             :     const float Ap[],           /* i  : quantized Aq                    */
     491             :     float exc16k[],             /* i  : excitation at 12.8 kHz          */
     492             :     float synth_out[],          /* i/o: synthesis signal at output Fs   */
     493             :     float *mem_syn_hf,          /* i/o: HF synthesis memory             */
     494             :     float *delay_syn_hf,        /* i/o: HF synthesis memory             */
     495             :     float *mem_hp_interp,       /* i/o: interpol. memory                */
     496             :     float p_r,                  /* i  : sub-frame gain                  */
     497             :     float HF_corr_gain,         /* i  : HF gain index                   */
     498             :     float til0,
     499             :     float voice_factors,
     500             :     const float exc[] )
     501             : {
     502             :     int16_t i;
     503             :     float HF_syn[L_SUBFR16k], upsampled_HF_syn[L_FRAME48k / NB_SUBFR];
     504             :     float ener, tmp, scale, exc2385[L_SUBFR16k];
     505             : 
     506           0 :     if ( core_brate == ACELP_23k85 )
     507             :     {
     508           0 :         ener = ( sum2_f( exc, L_SUBFR ) + 0.01f ) / 5;
     509           0 :         tmp = sum2_f( exc16k, L_SUBFR16k ) + 0.01f;
     510           0 :         scale = (float) sqrt( ener / tmp );
     511             : 
     512           0 :         for ( i = 0; i < L_SUBFR16k; i++ )
     513             :         {
     514           0 :             exc2385[i] = exc16k[i] * scale * HF_corr_gain;
     515             :         }
     516             :     }
     517             : 
     518           0 :     for ( i = 0; i < L_SUBFR16k; i++ )
     519             :     {
     520           0 :         exc16k[i] *= p_r;
     521             :     }
     522             : 
     523           0 :     if ( core_brate == ACELP_23k85 )
     524             :     {
     525           0 :         ener = ( sum2_f( exc16k, L_SUBFR16k ) + 0.01f ) * 0.3f;
     526           0 :         tmp = sum2_f( exc2385, L_SUBFR16k ) + 0.01f;
     527           0 :         scale = (float) sqrt( ener / tmp );
     528             : 
     529           0 :         if ( scale > 1.0f || til0 < 0.0f )
     530             :         {
     531           0 :             mvr2r( exc2385, exc16k, L_SUBFR16k );
     532             :         }
     533             :         else
     534             :         {
     535           0 :             for ( i = 0; i < L_SUBFR16k; i++ )
     536             :             {
     537           0 :                 exc16k[i] = exc2385[i] * max( min( 1.0f, ( 1 - til0 ) * ( 1.6f - voice_factors ) ), scale );
     538             :             }
     539             :         }
     540             :     }
     541             : 
     542           0 :     syn_filt( Ap, M, exc16k, HF_syn, L_SUBFR16k, mem_syn_hf, 1 );
     543             : 
     544             :     /*-----------------------------------------------------------------*
     545             :      * Resample to output sampling rate
     546             :      * Synchronize LB and HB components (delay componsation)
     547             :      * Add synthesised high band to speech synthesis
     548             :      *-----------------------------------------------------------------*/
     549             : 
     550             :     /* compensate CLDFB resampling delay */
     551           0 :     delay_signal( HF_syn, L_SUBFR16k, delay_syn_hf, NS2SA( 16000, DELAY_CLDFB_NS ) );
     552             : 
     553             :     /* interpolate the HF synthesis */
     554           0 :     if ( output_subfr == L_FRAME48k / NB_SUBFR ) /* 48kHz sampled output */
     555             :     {
     556           0 :         interpolate_3_over_1_allpass( HF_syn, L_SUBFR16k, upsampled_HF_syn, mem_hp_interp );
     557             :     }
     558           0 :     else if ( output_subfr == L_FRAME32k / NB_SUBFR ) /* 32kHz sampled output */
     559             :     {
     560           0 :         Interpolate_allpass_steep( HF_syn, mem_hp_interp, L_SUBFR16k, upsampled_HF_syn );
     561             :     }
     562             :     else /* 16kHz sampled output */
     563             :     {
     564           0 :         mvr2r( HF_syn, upsampled_HF_syn, L_SUBFR16k );
     565             :     }
     566             : 
     567           0 :     v_add( synth_out, upsampled_HF_syn, synth_out, output_subfr );
     568             : 
     569           0 :     return;
     570             : }
     571             : 
     572             : 
     573             : /*-----------------------------------------------------------------------------------*
     574             :  * EnhanceClass()
     575             :  *
     576             :  *
     577             :  *-----------------------------------------------------------------------------------*/
     578             : 
     579           0 : static int16_t EnhanceClass(
     580             :     const float qq,
     581             :     const float pp,
     582             :     const float tilt0,        /* i  : spectrum tilt                */
     583             :     const float tilt,         /* i  : spectrum tilt                */
     584             :     const float voice_factor, /* i  : voice factor                 */
     585             :     float *voice_fac,         /* i/o: smoothed voiced parameter    */
     586             :     float *unvoicing,         /* i/o: unvoiced parameter           */
     587             :     float *unvoicing_sm,      /* i/o: smoothed unvoiced parameter  */
     588             :     int16_t *unvoicing_flag   /* i/o: unvoiced flag                */
     589             : )
     590             : {
     591             :     float unvoicing_tmp;
     592             : 
     593             :     /* Decide (*unvoicing_flag) to allow BWE enhancement when qq>pp */
     594           0 :     *voice_fac = 0.75f * ( *voice_fac ) + 0.25f * voice_factor;
     595           0 :     unvoicing_tmp = ( ( 1.0f - tilt0 ) / 2.0f ) * ( 1 - *voice_fac ) * min( tilt / 1.5f, 1.0f );
     596           0 :     *unvoicing = 0.5f * ( *unvoicing ) + 0.5f * unvoicing_tmp;
     597             : 
     598           0 :     if ( *unvoicing_sm > *unvoicing )
     599             :     {
     600           0 :         *unvoicing_sm = 0.9f * ( *unvoicing_sm ) + 0.1f * ( *unvoicing );
     601             :     }
     602             :     else
     603             :     {
     604           0 :         *unvoicing_sm = 0.99f * ( *unvoicing_sm ) + 0.01f * ( *unvoicing );
     605             :     }
     606             : 
     607           0 :     if ( *unvoicing - *unvoicing_sm > 0.1f )
     608             :     {
     609           0 :         *unvoicing_flag = 1;
     610             :     }
     611             : 
     612           0 :     if ( *unvoicing - *unvoicing_sm < 0.05f )
     613             :     {
     614           0 :         *unvoicing_flag = 0;
     615             :     }
     616             : 
     617           0 :     return ( *unvoicing_flag && qq > pp );
     618             : }
     619             : 
     620             : /*-----------------------------------------------------------------------------------*
     621             :  * envelope()
     622             :  *
     623             :  *
     624             :  *-----------------------------------------------------------------------------------*/
     625             : 
     626           0 : static void envelope(
     627             :     AMRWB_IO_DEC_HANDLE hAmrwb_IO,
     628             :     const int32_t core_brate, /* i  : core bitrate                    */
     629             :     const float Aq[],         /* i  : de-quant. LPC coefficents       */
     630             :     float Ap[],               /* o  : extended LPC coefficents        */
     631             :     float *sub_gain,          /* o  : sub-frame gain                  */
     632             :     float tilt0,              /* i  : spectrum tilt                   */
     633             :     float tilt,               /* i  : spectrum tilt                   */
     634             :     float voice_factor        /* i  : voice factor                    */
     635             : )
     636             : {
     637             :     float px, py, rx, ry, pp, rr;
     638             :     int16_t i, Unvoicing_flag;
     639             :     float alpha;
     640             :     float est_level1, est_level2, qx, qy, qq, env_level[3];
     641             :     float As[3], k1, k2;
     642             : 
     643             :     /* LPC envelope weighting */
     644           0 :     if ( core_brate == ACELP_6k60 )
     645             :     {
     646           0 :         weight_a( Aq, Ap, 0.9f, M );
     647             :     }
     648             :     else
     649             :     {
     650           0 :         weight_a( Aq, Ap, 0.6f, M );
     651             :     }
     652             : 
     653             :     /* LPC envelope level estimate */
     654           0 :     pp = 0.0f;
     655           0 :     px = 0.0f;
     656           0 :     py = 0.0f;
     657           0 :     rr = 0.0f;
     658           0 :     rx = 0.0f;
     659           0 :     ry = 0.0f;
     660           0 :     for ( i = 0; i < 17; i++ )
     661             :     {
     662           0 :         px += Ap[i] * exp_tab_p[i];
     663           0 :         py += Ap[i] * exp_tab_p[33 - i];
     664           0 :         rx += Aq[i] * exp_tab_q[i];
     665           0 :         ry += Aq[i] * exp_tab_q[33 - i];
     666             :     }
     667             : 
     668           0 :     pp = 1.0f / ( (float) sqrt( px * px + py * py ) );
     669           0 :     rr = 1.0f / ( (float) sqrt( rx * rx + ry * ry ) );
     670             : 
     671           0 :     for ( i = 0; i < 3; i++ )
     672             :     {
     673           0 :         As[i] = Aq[i];
     674             :     }
     675             : 
     676           0 :     if ( As[2] == -1 )
     677             :     {
     678           0 :         k2 = -0.6f;
     679           0 :         k1 = 0.99f;
     680           0 :         if ( As[1] < 0 )
     681             :         {
     682           0 :             k1 = -k1;
     683             :         }
     684             :     }
     685             :     else
     686             :     {
     687           0 :         k1 = As[1] / ( 1 + As[2] );
     688           0 :         k2 = As[2];
     689           0 :         if ( k2 > 0.6f )
     690             :         {
     691           0 :             k2 = 0.6f;
     692             :         }
     693           0 :         if ( k2 < -0.6f )
     694             :         {
     695           0 :             k2 = -0.6f;
     696             :         }
     697           0 :         if ( k1 > 0.99f )
     698             :         {
     699           0 :             k1 = 0.99f;
     700             :         }
     701           0 :         if ( k1 < -0.99f )
     702             :         {
     703           0 :             k1 = -0.99f;
     704             :         }
     705             :     }
     706             : 
     707           0 :     As[1] = ( 1 + k2 ) * k1;
     708           0 :     As[2] = k2;
     709             : 
     710           0 :     qq = 0.0f;
     711           0 :     qx = 0.0f;
     712           0 :     qy = 0.0f;
     713           0 :     for ( i = 0; i < 3; i++ )
     714             :     {
     715           0 :         qx += As[i] * exp_tab_q[i];
     716           0 :         qy += As[i] * exp_tab_q[33 - i];
     717             :     }
     718             : 
     719           0 :     qq = 1.0f / ( (float) sqrt( qx * qx + qy * qy ) );
     720             : 
     721           0 :     Unvoicing_flag = EnhanceClass( rr, pp, tilt0, tilt, voice_factor, &hAmrwb_IO->voice_fac_amr_wb_hf, &hAmrwb_IO->unvoicing, &hAmrwb_IO->unvoicing_sm, &hAmrwb_IO->unvoicing_flag );
     722             : 
     723           0 :     alpha = 0.0f;
     724           0 :     if ( Unvoicing_flag )
     725             :     {
     726           0 :         if ( rr > ( hAmrwb_IO->prev_r ) )
     727             :         {
     728           0 :             rr = 0.5f * rr + 0.5f * ( hAmrwb_IO->prev_r );
     729             :         }
     730             : 
     731           0 :         hAmrwb_IO->prev_r = rr;
     732           0 :         rr *= min( 1.0f, tilt * ( 1.6f - voice_factor ) );
     733           0 :         qq *= max( 1.0f, tilt * ( 1.6f - voice_factor ) );
     734           0 :         rr = min( rr, qq );
     735           0 :         rr = max( rr, pp );
     736             :     }
     737             :     else
     738             :     {
     739           0 :         if ( rr < 1.0f && ( hAmrwb_IO->prev_r ) < 1.0f )
     740             :         {
     741           0 :             alpha = ( 1 - rr * rr );
     742             :         }
     743             : 
     744           0 :         rr = alpha * ( hAmrwb_IO->prev_r ) + ( 1 - alpha ) * rr;
     745           0 :         hAmrwb_IO->prev_r = rr;
     746             : 
     747           0 :         est_level1 = qq * min( 1.0f, tilt * ( 1.6f - voice_factor ) );
     748           0 :         env_level[0] = pp;
     749           0 :         env_level[1] = qq;
     750           0 :         env_level[2] = rr;
     751           0 :         v_sort( env_level, 0, 2 );
     752           0 :         rr = env_level[0];
     753           0 :         est_level2 = rr * ( 1.0f + (float) fabs( tilt - 1 ) * ( 1.6f - voice_factor ) );
     754           0 :         rr = min( est_level1, est_level2 );
     755             :     }
     756             : 
     757           0 :     *sub_gain = min( 5.0f, rr / pp );
     758             : 
     759           0 :     return;
     760             : }
     761             : 
     762             : /*---------------------------------------------------------------------*
     763             :  * AdaptiveStartBand()
     764             :  *
     765             :  * adaptively select the start band of bandwidth extension
     766             :  *---------------------------------------------------------------------*/
     767             : 
     768           0 : static void AdaptiveStartBand(
     769             :     int16_t *start_band,      /* o  : start point of copied band                */
     770             :     const int32_t core_brate, /* i  : core bitrate                              */
     771             :     const float *lsf,         /* i  : lsf frequency                             */
     772             :     const float voicing_fac,  /* i  : voicing factors                           */
     773             :     const int16_t clas,       /* i  : signal class (determined by FEC algorithm)*/
     774             :     int16_t *voicing_flag,
     775             :     int16_t *start_band_old,
     776             :     float *OptCrit_old )
     777             : {
     778           0 :     float lsf_diff[M], Crit, OptCrit = 1.0f, W;
     779             :     int16_t i, pos, M2, voicing_flag_old;
     780             :     int16_t tmp1, tmp2;
     781             : 
     782             :     /*voicing switching flag : to avoid switching start band frequently in VOICED or AUDIO area*/
     783           0 :     voicing_flag_old = *voicing_flag;
     784           0 :     if ( voicing_fac > 0.4f || ( voicing_fac > 0.3f && clas >= VOICED_CLAS ) || clas == AUDIO_CLAS )
     785             :     {
     786           0 :         *voicing_flag = 1;
     787             :     }
     788             : 
     789           0 :     if ( voicing_fac < 0.2f && clas < VOICED_CLAS )
     790             :     {
     791           0 :         *voicing_flag = 0;
     792             :     }
     793             : 
     794             :     /* rate adaptive start band */
     795           0 :     *start_band = 160;
     796           0 :     if ( core_brate < ACELP_23k05 )
     797             :     {
     798           0 :         for ( i = 1; i < ( M - 1 ); i++ )
     799             :         {
     800           0 :             lsf_diff[i] = lsf[i] - lsf[i - 1];
     801             :         }
     802             : 
     803           0 :         W = SQR( 1.0f * core_brate / ACELP_19k85 ) / 6000.0f;
     804             : 
     805           0 :         if ( clas == AUDIO_CLAS )
     806             :         {
     807           0 :             W *= 0.75f;
     808             :         }
     809             : 
     810           0 :         pos = 2;
     811           0 :         M2 = M - 2;
     812           0 :         if ( *voicing_flag == 1 )
     813             :         {
     814           0 :             if ( core_brate <= ACELP_8k85 )
     815             :             {
     816           0 :                 M2 = M - 8;
     817             :             }
     818           0 :             else if ( core_brate <= ACELP_12k65 )
     819             :             {
     820           0 :                 M2 = M - 6;
     821             :             }
     822           0 :             else if ( core_brate <= ACELP_15k85 )
     823             :             {
     824           0 :                 M2 = M - 4;
     825             :             }
     826             :         }
     827             : 
     828           0 :         for ( i = 2; i < M2; i++ )
     829             :         {
     830           0 :             Crit = lsf_diff[i] * max( 1.0f - lsf[i] * W, 0.001f );
     831           0 :             if ( Crit <= OptCrit || i == 2 )
     832             :             {
     833           0 :                 OptCrit = Crit;
     834           0 :                 pos = i;
     835             :             }
     836             :         }
     837             :         /* *start_band = (int16_t)( (0.5f*(lsf[pos]+lsf[pos-1])*40/1000.0f - 40) + 0.5f ); */
     838             :         /* emulate BASOP precision: */
     839           0 :         tmp1 = (int16_t) ( ( 0.02f * lsf[pos] ) + 0.5f );
     840           0 :         tmp2 = (int16_t) ( ( 0.02f * lsf[pos - 1] ) + 0.5f );
     841           0 :         *start_band = tmp1 + tmp2 - 40;
     842           0 :         *start_band = min( max( *start_band, 40 ), 160 );
     843             : 
     844           0 :         if ( voicing_flag_old != *voicing_flag || ( *voicing_flag == 0 && OptCrit < *OptCrit_old ) ||
     845           0 :              ( OptCrit < 0.7f * ( *OptCrit_old ) && *OptCrit_old > 64 ) )
     846             :         {
     847           0 :             *OptCrit_old = OptCrit;
     848           0 :             if ( abs( ( *start_band ) - ( *start_band_old ) ) < 20 && *voicing_flag == 1 && voicing_flag_old == 1 )
     849             :             {
     850           0 :                 *start_band = *start_band_old;
     851             :             }
     852             :         }
     853             :         else
     854             :         {
     855           0 :             if ( OptCrit < ( *OptCrit_old ) && ( *voicing_flag ) == 1 )
     856             :             {
     857           0 :                 *OptCrit_old = OptCrit;
     858             :             }
     859             : 
     860           0 :             *start_band = *start_band_old;
     861             :         }
     862             : 
     863           0 :         if ( clas == AUDIO_CLAS )
     864             :         {
     865           0 :             *start_band = min( *start_band, 120 );
     866             :         }
     867             : 
     868           0 :         if ( *start_band % 2 != 0 )
     869             :         {
     870           0 :             *start_band -= 1;
     871             :         }
     872             :     }
     873             : 
     874           0 :     *start_band_old = *start_band;
     875             : 
     876           0 :     return;
     877             : }
     878             : 
     879             : 
     880             : /*-------------------------------------------------------------------*
     881             :  * hf_synth_reset()
     882             :  *
     883             :  * Reset of HF synthesis filters (needed in switching scenarios)
     884             :  *-------------------------------------------------------------------*/
     885             : 
     886     1189689 : void hf_synth_reset(
     887             :     ZERO_BWE_DEC_HANDLE hBWE_zero /* o  : zero BWE decoder handle */
     888             : )
     889             : {
     890             :     int16_t i;
     891             : 
     892   381890169 :     for ( i = 0; i < L_FRAME16k; i++ )
     893             :     {
     894   380700480 :         own_random( &( hBWE_zero->seed2 ) );
     895             :     }
     896             : 
     897     1189689 :     set_f( hBWE_zero->mem_hf, 0.0f, ( L_FIR - 1 ) );
     898     1189689 :     set_f( hBWE_zero->mem_syn_hf, 0.0f, M );
     899     1189689 :     set_f( hBWE_zero->mem_hp400, 0.0f, 4 );
     900     1189689 :     set_f( hBWE_zero->delay_syn_hf, 0.0f, NS2SA( 16000, DELAY_CLDFB_NS ) );
     901     1189689 :     set_f( hBWE_zero->mem_hp_interp, 0, INTERP_3_1_MEM_LEN );
     902             : 
     903     1189689 :     return;
     904             : }
     905             : 
     906             : 
     907             : /*---------------------------------------------------------------------*
     908             :  * hf_synth()
     909             :  *
     910             :  * High frequency regeneration
     911             :  *---------------------------------------------------------------------*/
     912             : 
     913       46926 : void hf_synth(
     914             :     ZERO_BWE_DEC_HANDLE hBWE_zero, /* o  : zero BWE decoder handle     */
     915             :     const int32_t core_brate,      /* i  : core bitrate                */
     916             :     const int16_t output_frame,    /* i  : output frame length         */
     917             :     const float *Aq,               /* i  : quantized Az                */
     918             :     const float *exc,              /* i  : excitation at 12.8 kHz      */
     919             :     float *synth,                  /* i/o: 12.8kHz synthesis signal    */
     920             :     float *synth16k                /* i/o: 16kHz synthesis signal      */
     921             : )
     922             : {
     923             :     const float *p_Aq;
     924             :     int16_t i_subfr, output_subfr;
     925             : 
     926       46926 :     output_subfr = output_frame / NB_SUBFR;
     927             : 
     928       46926 :     p_Aq = Aq;
     929      234630 :     for ( i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR )
     930             :     {
     931      187704 :         hf_synthesis( hBWE_zero, core_brate, output_subfr, p_Aq, &exc[i_subfr], &synth[i_subfr], &synth16k[i_subfr * output_subfr / L_SUBFR] );
     932      187704 :         p_Aq += ( M + 1 );
     933             :     }
     934             : 
     935       46926 :     return;
     936             : }
     937             : 
     938             : 
     939             : /*-----------------------------------------------------------------------------------*
     940             :  * hf_synthesis()
     941             :  *
     942             :  * HF noise synthesis
     943             :  * - Generate HF noise between 6 and 7 kHz.
     944             :  * - Set energy of noise according to synthesis tilt.
     945             :  *     tilt > 0.8 ==> - 14 dB (voiced)
     946             :  *     tilt   0.5 ==> - 6 dB  (voiced or noise)
     947             :  *     tilt < 0.0 ==>   0 dB  (noise)
     948             :  *-----------------------------------------------------------------------------------*/
     949             : 
     950      187704 : static void hf_synthesis(
     951             :     ZERO_BWE_DEC_HANDLE hBWE_zero, /* o  : zero BWE decoder handle         */
     952             :     const int32_t core_brate,      /* i  : core bitrate                    */
     953             :     const int16_t output_subfr,    /* i  : output sub-frame length         */
     954             :     const float Aq[],              /* i  : quantized Aq                    */
     955             :     const float exc[],             /* i  : excitation at 12.8 kHz          */
     956             :     float synth[],                 /* i/o: 12.8kHz synthesis signal        */
     957             :     float synth16k[]               /* i/o: 16kHz synthesis signal          */
     958             : )
     959             : {
     960             :     int16_t i;
     961             :     float tmp, ener, fac, scale;
     962             :     float HF_exc[L_SUBFR16k], HF_syn[L_SUBFR16k], upsampled_HF_syn[L_FRAME48k / NB_SUBFR];
     963             :     float Ap[M16k + 1];
     964             : 
     965             :     /*-----------------------------------------------------------------*
     966             :      * generate white noise vector
     967             :      *-----------------------------------------------------------------*/
     968             : 
     969    15204024 :     for ( i = 0; i < L_SUBFR16k; i++ )
     970             :     {
     971    15016320 :         HF_exc[i] = (float) own_random( &( hBWE_zero->seed2 ) );
     972             :     }
     973             : 
     974             :     /*-----------------------------------------------------------------*
     975             :      * calculate energy scaling factor so that white noise would have the
     976             :      * same energy as exc12k8
     977             :      * note: the scaling factor should be multiplied by L_SUBFR16k / L_SUBFR for
     978             :      * correctness but it will have only negligible impact so it has been left like this
     979             :      *-----------------------------------------------------------------*/
     980             : 
     981      187704 :     ener = sum2_f( exc, L_SUBFR ) + 0.01f;
     982      187704 :     tmp = sum2_f( HF_exc, L_SUBFR16k ) + 0.01f;
     983      187704 :     scale = (float) ( sqrt( ener / tmp ) );
     984             : 
     985             :     /*-----------------------------------------------------------------*
     986             :      * calculate energy scaling factor to respect tilt of synth12k8
     987             :      * (tilt: 1=voiced, -1=unvoiced)
     988             :      *-----------------------------------------------------------------*/
     989             : 
     990      187704 :     hp400_12k8( synth, L_SUBFR, hBWE_zero->mem_hp400 );
     991             : 
     992      187704 :     ener = 0.001f;
     993      187704 :     tmp = 0.001f;
     994             : 
     995    12013056 :     for ( i = 1; i < L_SUBFR; i++ )
     996             :     {
     997    11825352 :         ener += synth[i] * synth[i];
     998    11825352 :         tmp += synth[i] * synth[i - 1];
     999             :     }
    1000             : 
    1001      187704 :     fac = tmp / ener;
    1002      187704 :     fac = (float) ( 1.0f - fac );
    1003             : 
    1004      187704 :     if ( core_brate == SID_2k40 || core_brate == FRAME_NO_DATA )
    1005             :     {
    1006             :         /* emphasize HF noise in CNG */
    1007       87240 :         fac *= 2.0f;
    1008             :     }
    1009             : 
    1010      187704 :     if ( fac < 0.1f )
    1011             :     {
    1012      107880 :         fac = 0.1f;
    1013             :     }
    1014             : 
    1015      187704 :     if ( fac > 1.0f )
    1016             :     {
    1017       24999 :         fac = 1.0f;
    1018             :     }
    1019             : 
    1020      187704 :     scale *= fac;
    1021             :     /*-----------------------------------------------------------------*
    1022             :      * modify HF excitation according to both calculated scaling factors
    1023             :      *-----------------------------------------------------------------*/
    1024             : 
    1025    15204024 :     for ( i = 0; i < L_SUBFR16k; i++ )
    1026             :     {
    1027    15016320 :         HF_exc[i] *= scale;
    1028             :     }
    1029             : 
    1030             :     /*-----------------------------------------------------------------*
    1031             :      * high pass filtering (0.94ms of delay)
    1032             :      *-----------------------------------------------------------------*/
    1033             : 
    1034      187704 :     filt_6k_7k( HF_exc, L_SUBFR16k, hBWE_zero->mem_hf );
    1035             : 
    1036             :     /*-----------------------------------------------------------------*
    1037             :      * synthesis of noise: 4.8kHz..5.6kHz --> 6kHz..7kHz
    1038             :      *-----------------------------------------------------------------*/
    1039             : 
    1040      187704 :     weight_a( Aq, Ap, 0.6f, M );
    1041      187704 :     syn_filt( Ap, M, HF_exc, HF_syn, L_SUBFR16k, hBWE_zero->mem_syn_hf, 1 );
    1042             : 
    1043             :     /*-----------------------------------------------------------------*
    1044             :      * Add filtered HF noise to speech synthesis
    1045             :      *-----------------------------------------------------------------*/
    1046             : 
    1047             :     /* delay by 5 samples @16kHz to compensate CLDFB resampling delay (20samples) and HP filtering delay (roughly 15 samples) */
    1048      187704 :     delay_signal( HF_syn, L_SUBFR16k, hBWE_zero->delay_syn_hf, NS2SA( 16000, DELAY_CLDFB_NS ) - 15 );
    1049             : 
    1050             :     /* interpolate the HF synthesis */
    1051      187704 :     if ( output_subfr == L_FRAME48k / NB_SUBFR ) /* 48kHz sampled output */
    1052             :     {
    1053      158616 :         interpolate_3_over_1_allpass( HF_syn, L_SUBFR16k, upsampled_HF_syn, hBWE_zero->mem_hp_interp );
    1054             :     }
    1055       29088 :     else if ( output_subfr == L_FRAME32k / NB_SUBFR ) /* 32kHz sampled output */
    1056             :     {
    1057       18984 :         Interpolate_allpass_steep( HF_syn, hBWE_zero->mem_hp_interp, L_SUBFR16k, upsampled_HF_syn );
    1058             :     }
    1059             :     else /* 16kHz sampled output */
    1060             :     {
    1061       10104 :         mvr2r( HF_syn, upsampled_HF_syn, L_SUBFR16k );
    1062             :     }
    1063             : 
    1064      187704 :     v_add( synth16k, upsampled_HF_syn, synth16k, output_subfr );
    1065             : 
    1066      187704 :     return;
    1067             : }
    1068             : 
    1069             : 
    1070             : /*-----------------------------------------------------------------------*
    1071             :  * hp400_12k8()
    1072             :  *
    1073             :  * 2nd order Cheb2 high pass filter with cut off frequency at 400 Hz.
    1074             :  * Optimized for fixed-point to get the following frequency response:
    1075             :  *
    1076             :  *  frequency  :   0Hz   100Hz  200Hz  300Hz  400Hz  630Hz  1.5kHz  3kHz
    1077             :  *  dB loss  :   -infdB  -30dB  -20dB  -10dB  -3dB   +6dB    +1dB    0dB
    1078             :  *
    1079             :  * Algorithm  :
    1080             :  *
    1081             :  *  y[i] = b[0]*x[i] + b[1]*x[i-1] + b[2]*x[i-2]
    1082             :  *                   + a[1]*y[i-1] + a[2]*y[i-2];
    1083             :  *
    1084             :  *  short b[3] = {3660, -7320,  3660};       in Q12
    1085             :  *  short a[3] = {4096,  7320, -3540};       in Q12
    1086             :  *
    1087             :  *  float b[3] = {0.893554687, -1.787109375,  0.893554687};
    1088             :  *  float a[3] = {1.000000000,  1.787109375, -0.864257812};
    1089             :  *-----------------------------------------------------------------------*/
    1090             : 
    1091      187704 : static void hp400_12k8(
    1092             :     float signal[],   /* i/o: signal            */
    1093             :     const int16_t lg, /* i  : length of signal  */
    1094             :     float mem[]       /* i/o: filter memory [4] */
    1095             : )
    1096             : {
    1097             :     int16_t i;
    1098             :     float x0, x1, x2;
    1099             :     float yy0, yy1, y2;
    1100             : 
    1101             : 
    1102      187704 :     yy1 = mem[0];
    1103      187704 :     y2 = mem[1];
    1104      187704 :     x0 = mem[2];
    1105      187704 :     x1 = mem[3];
    1106             : 
    1107    12200760 :     for ( i = 0; i < lg; i++ )
    1108             :     {
    1109    12013056 :         x2 = x1;
    1110    12013056 :         x1 = x0;
    1111    12013056 :         x0 = signal[i];
    1112    12013056 :         yy0 = yy1 * a_hp400[1] + y2 * a_hp400[2] + x0 * b_hp400[0] + x1 * b_hp400[1] + x2 * b_hp400[2];
    1113             : 
    1114    12013056 :         signal[i] = yy0;
    1115    12013056 :         y2 = yy1;
    1116    12013056 :         yy1 = yy0;
    1117             :     }
    1118             : 
    1119      187704 :     mem[0] = yy1;
    1120      187704 :     mem[1] = y2;
    1121      187704 :     mem[2] = x0;
    1122      187704 :     mem[3] = x1;
    1123             : 
    1124      187704 :     return;
    1125             : }
    1126             : 
    1127             : /*-------------------------------------------------------------------*
    1128             :  * filt_6k_7k()
    1129             :  *
    1130             :  * 15th order band pass 6kHz to 7kHz FIR filter
    1131             :  *
    1132             :  * frequency  :4kHz   5kHz  5.5kHz  6kHz  6.5kHz 7kHz  7.5kHz  8kHz
    1133             :  * dB loss  : -60dB  -45dB  -13dB   -3dB   0dB   -3dB  -13dB  -45dB
    1134             :  * (gain = 4.0)
    1135             :  *-------------------------------------------------------------------*/
    1136             : 
    1137      187704 : static void filt_6k_7k(
    1138             :     float signal[],   /* i/o: signal        */
    1139             :     const int16_t lg, /* i  : signal length */
    1140             :     float mem[]       /* i/o: filter memory */
    1141             : )
    1142             : {
    1143             :     int16_t i, j;
    1144             :     float s, x[L_FRAME48k / NB_SUBFR + ( L_FIR - 1 )];
    1145             : 
    1146     5818824 :     for ( i = 0; i < ( L_FIR - 1 ); i++ )
    1147             :     {
    1148     5631120 :         x[i] = mem[i];
    1149             :     }
    1150             : 
    1151    15204024 :     for ( i = 0; i < lg; i++ )
    1152             :     {
    1153    15016320 :         x[i + ( L_FIR - 1 )] = signal[i];
    1154             :     }
    1155             : 
    1156    15204024 :     for ( i = 0; i < lg; i++ )
    1157             :     {
    1158    15016320 :         s = 0.0;
    1159   480522240 :         for ( j = 0; j < L_FIR; j++ )
    1160             :         {
    1161   465505920 :             s += x[i + j] * fir_6k_7k[j];
    1162             :         }
    1163             : 
    1164             :         /* gain of coef = 4.0 */
    1165    15016320 :         signal[i] = (float) ( s * 0.25 );
    1166             :     }
    1167             : 
    1168     5818824 :     for ( i = 0; i < L_FIR - 1; i++ )
    1169             :     {
    1170     5631120 :         mem[i] = x[i + lg];
    1171             :     }
    1172             : 
    1173      187704 :     return;
    1174             : }

Generated by: LCOV version 1.14