www.pudn.com > SMV_Code.rar > lib_cla.c


/*=========================================================================*/ 
/* Each of the companies; Ericsson, Lucent, Mindspeed, Motorola, Nokia,    */ 
/* Nortel Networks, and Qualcomm (hereinafter referred to individually as  */ 
/* “Source” or collectively as “Sources”) do hereby state:                 */ 
/*                                                                         */ 
/* To the extent to which the Source(s) may legally and freely do so,      */ 
/* the Source(s), upon submission of a Contribution, grant(s) a free,      */ 
/* irrevocable, non-exclusive, license to the Third Generation Partnership */ 
/* Project 2 (3GPP2) and its Organizational Partners: ARIB, CCSA, TIA,     */ 
/* TTA, and TTC, under the Source’s copyright or copyright license rights  */ 
/* in the Contribution, to, in whole or in part, copy, make derivative     */ 
/* works, perform, display and distribute the Contribution and derivative  */ 
/* works thereof consistent with 3GPP2’s and each Organizational Partner’s */ 
/* policies and procedures, with the right to (i) sublicense the foregoing */ 
/* rights consistent with 3GPP2’s and each Organizational Partner’s        */ 
/* policies and procedures and (ii) copyright and sell, if applicable) in  */ 
/* 3GPP2's name or each Organizational Partner’s name any 3GPP2 or         */ 
/* transposed Publication even though this Publication may contain the     */ 
/* Contribution or a derivative work thereof.  The Contribution shall      */ 
/* disclose any known limitations on the Source’s rights to license as     */ 
/* herein provided.                                                        */ 
/*                                                                         */ 
/* When a Contribution is submitted by the Source(s) to assist the         */ 
/* formulating groups of 3GPP2 or any of its Organizational Partners,      */ 
/* it is proposed to the Committee as a basis for discussion and is not    */ 
/* to be construed as a binding proposal on the Source(s).  The Source(s)  */ 
/* specifically reserve(s) the right to amend or modify the material       */ 
/* contained in the Contribution. Nothing contained in the Contribution    */ 
/* shall, except as herein expressly provided, be construed as conferring  */ 
/* by implication, estoppel or otherwise, any license or right under       */ 
/* (i) any existing or later issuing patent, whether or not the use of     */ 
/* information in the document necessarily employs an invention of any     */ 
/* existing or later issued patent, (ii) any copyright, (iii) any          */ 
/* trademark, or (iv) any other intellectual property right.               */ 
/*                                                                         */ 
/* With respect to the Software necessary for the practice of any or all   */ 
/* Normative portions of the Selectable Mode Vocoder (SMV) as it exists on */ 
/* the date of submittal of this form, should the SMV be approved as a     */ 
/* Specification or Report by 3GPP2, or as a transposed Standard by any of */ 
/* the 3GPP2’s Organizational Partners, the Source(s) state(s) that a      */ 
/* worldwide license to reproduce, use and distribute the Software, the    */ 
/* license rights to which are held by the Source(s), will be made         */ 
/* available to applicants under terms and conditions that are reasonable  */ 
/* and non-discriminatory, which may include monetary compensation,        */ 
/* and only to the extent necessary for the practice of any or all of the  */ 
/* Normative portions of the SMV or the field of use of practice of the    */ 
/* SMV Specification, Report, or Standard.  The statement contained above  */ 
/* is irrevocable and shall be binding upon the Source(s).  In the event   */ 
/* the rights of the Source(s) in and to copyright or copyright license    */ 
/* rights subject to such commitment are assigned or transferred,          */ 
/* the Source(s) shall notify the assignee or transferee of the existence  */ 
/* of such commitments.                                                    */ 
/*=========================================================================*/ 
/*                                                                   */ 
/*-------------------------------------------------------------------*/ 
/*===================================================================*/ 
/* LIBRARY: lib_cla.c                                                */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE : Library of input signal classifiers.                    */ 
/*===================================================================*/ 
 
/*----------------------------------------------------------------------------*/ 
/*-------------------------------- INCLUDE -----------------------------------*/ 
/*----------------------------------------------------------------------------*/ 
 
#include "typedef.h" 
 
#include "main.h" 
#include "const.h" 
#include "gputil.h" 
#include "mcutil.h" 
 
#include "ext_var.h" 
 
#include "lib_cla.h" 
#include "lib_lpc.h" 
#include "lib_ppp.h" 
 
#ifdef WMOPS 
 
#include "lib_wmp.h" 
 
#endif 
 
/*----------------------------------------------------------------------------*/ 
/*-------------------------------- FUNCTIONS ---------------------------------*/ 
/*----------------------------------------------------------------------------*/ 
 
 
/*===================================================================*/ 
/* FUNCTION       :  CLA_init_lib ().                                */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE        :  This function initialise the global variables   */ 
/*                   of the CLA library.                             */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*                    _ None.                                        */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*                    _ None.                                        */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*                    _ None.                                        */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*                    _ None.                                        */ 
/*===================================================================*/ 
 
void CLA_init_lib (void) 
	{ 
	 /*-------------------------------------------------------------------*/ 
#ifdef ENC_CMP 
 
	 VUV = 0; 
	 frame_class = SILENCE;  
	 frame_onset=0; 
 
	 energy_m = 0.0; 
	 energy	  = 0.0; 
 
	 /*-------------------------------------------------------------------*/ 
	  
	 frame_class_mem = SILENCE; 
	 onstplsv_mem    = 0; 
	 ini_svector (voiced_mem, 0, 1, (INT16)0); 
 
	 ini_dvector (sig_ppr_mem, 0, L_FRM+MEM_CLASS+L_LPCLHD-1, 0.0); 
	 ini_dvector (Lp_buffer, 0, N_LP-1, (FLOAT64)MIN_LAG); 
     
	 AR_avg_wRp = 0.0; 
 
	 AR_min_wtilt = 0.0; 
 
	 ini_svector (cond_mem, 0, 2, 0); 
 
	 /*---------------------------------------------------------*/ 
	 /*                Generate Hamming window                  */ 
	 /*---------------------------------------------------------*/     
 
	 E_noi = 1.0; 
	 T_noi = 0.0; 
	 M_noi = 0.0; 
	 R_noi = 0.0; 
 
	 ini_dvector (buffer_wtilt, 0, MAX_N_SF-1, 0.0); 
	 ini_dvector (buffer_wmax,  0, MAX_N_SF-1, 0.0); 
#endif 
	 /*-------------------------------------------------------------------*/ 
 
#ifdef DEC_CMP 
 
	 SVS_Count_dec = 0; 
	 Vad_0_Count_dec = 0; 
	 FlatSp_Flag_dec = 0; 
	  
	 Av_value_dec = 0.0; 
#endif 
 
#ifdef ENC_CMP 
	 SVS_Count_enc = 0; 
	 Vad_0_Count_enc = 0; 
	 FlatSp_Flag_enc = 0; 
	  
	 Av_value_enc = 0.0; 
 
	 /*-------------------------------------------------------------------*/ 
  
	 Rp_m_cla = 0.0; 
 
	 /*-------------------------------------------------------------------*/ 
	  
	 OnSet = 0;  
	 frame_class_mm = NON_STATIONARY_VOICED; 
            
	 /*-------------------------------------------------------------------*/ 
#endif 
 
#ifdef DEC_CMP 
 
	 SVS_deci_mem = 0; 
#endif 
	 /*-------------------------------------------------------------------*/ 
	 
	 return; 
	 
	 /*-------------------------------------------------------------------*/ 
	} 
 
/*----------------------------------------------------------------------------*/ 
#ifdef ENC_CMP 
 
/*===================================================================*/ 
/* FUNCTION       :  CLA_signal_classifier ().                       */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE        :  This function classify teh input signal frame:  */ 
/*                   file.                                           */ 
/*  frame_class 0: silence                                           */ 
/*  frame_class 1: noise-like   (CURRENTLY, NOT ACTIVATED)           */ 
/*  frame_class 2: unvoiced                                          */ 
/*  frame_class 3: onset                                             */ 
/*  frame_class 4: plosive      (CURRENTLY, NOT ACTIVATED)           */ 
/*  frame_class 5: non-stationary voiced                             */ 
/*  frame_class 6: stationary voiced (CURRENTLY, NOT DISTINGUISHED   */ 
/*                                       FROM CLASS 5)               */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*            _ (FLOAT64 []) signal: input frame.                    */ 
/*            _ (FLOAT64 )   Rp    : Pitch correlation.              */ 
/*            _ (FLOAT64 )   Lp    : Pitch lags.                     */ 
/*            _ (INT16  )    Vad   : VAD of current frame.           */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*                    _ None.                                        */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*                    _ None.                                        */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*            _ (INT16  ) frame_class: current fame frame_class.     */ 
/*===================================================================*/ 
 
INT16	CLA_signal_classifier (FLOAT64 signal[], FLOAT64 Rp, FLOAT64 Lp,  
			       INT16 Vad) 
	{ 
	 /*-------------------------------------------------------------------*/ 
 
	 INT16 frame_class, i_sf, k, idx; 
	 FLOAT64 *buffer_refl0; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*            Parameters for local first order LPC analysis          */ 
	 /*-------------------------------------------------------------------*/ 
	  
	 FLOAT64 *siglpc1, rxx1[2]; 
 
	 /*-------------------------------------------------------------------*/ 
         /*                       Pitch lag evolution                         */ 
  	 /*-------------------------------------------------------------------*/ 
 
	 FLOAT64 Lp_avg, Lp_var; 
 
	 /*-------------------------------------------------------------------*/ 
         /*                         Maximum Tracking                          */ 
  	 /*-------------------------------------------------------------------*/ 
 
	 INT16 start_max, end_max, start_ene, end_ene; 
	 FLOAT64 max; 
 
	 /*-------------------------------------------------------------------*/ 
         /*             Weighted spectral tilt and weighted maximum           */ 
  	 /*-------------------------------------------------------------------*/ 
 
	 FLOAT64 E_seg, nsr, min_slope_wtilt, max_slope_wmax,  
		 sum_slope_wmax, wRp; 
	 FLOAT64 num_tilt, num_max, slope_wtilt, slope_wmax; 
	 FLOAT64 min_wtilt, avg_wtilt, max_wRp, avg_wRp; 
	 INT16 onstplsv, voiced, cond[5]; 
 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                    Temporary Memory Allocation                    */ 
	 /*-------------------------------------------------------------------*/ 
 
	 buffer_refl0 = dvector (0, CLA_MEM_SIZE-1); 
	 siglpc1 = dvector (0, LPC_WIN1-1); 
 
	 /*-------------------------------------------------------------------*/ 
	 /* =============== Calculate evolution of pitch lag ================ */ 
	 /*-------------------------------------------------------------------*/ 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                  Update buffer with pitch lags                    */ 
	 /*-------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
         WMP_cnt_move(1); 
#endif 
 
	 cpy_dvector (Lp_buffer+1, Lp_buffer, 0, N_LP-2); 
	 Lp_buffer[N_LP-1] = Lp; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                       Average pitch lag                           */ 
	 /*-------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
         WMP_cnt_mult(1); 
#endif 
 
	 sum_dvector(Lp_buffer, &Lp_avg, 0, N_LP-1); 
	 Lp_avg /= (FLOAT64)N_LP; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                Standard deviation of pitch lag                    */ 
	 /*-------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
         WMP_cnt_sub(N_LP); 
         WMP_cnt_mac(N_LP); 
#endif 
	 for(Lp_var=0.0, k=0; k MAX_L_SF){ 
 
#ifdef WMOPS 
	       WMP_cnt_mult(2); 
	       WMP_cnt_add(2); 
	       WMP_cnt_test(1); 
	       WMP_cnt_move(1); 
#endif 
 
	       /* starting index for maximum */ 
	       start_max = (MEM_CLASS+MAX_L_SF+L_LPCLHD)- 
                           (INT16)(1.5*Lp) + i_sf*MAX_L_SF; 
	       start_max = (start_max < 0 ? 0 : start_max); 
 
#ifdef WMOPS 
	       WMP_cnt_mult(2); 
	       WMP_cnt_add(2); 
	       WMP_cnt_test(1); 
	       WMP_cnt_move(1); 
#endif 
 
	       /* starting index for energy */ 
	       start_ene = (MEM_CLASS+MAX_L_SF)-(INT16)(2.0*Lp)+i_sf*MAX_L_SF; 
	       start_ene = (start_ene < 0 ? 0 : start_ene); 
	     } 
	     else{ 
 
#ifdef WMOPS 
	       WMP_cnt_mult(1); 
	       WMP_cnt_add(1); 
#endif 
 
	       /* starting index for maximum */ 
	       start_max = (MEM_CLASS+L_LPCLHD)+i_sf*MAX_L_SF; 
 
#ifdef WMOPS 
	       WMP_cnt_mult(1); 
	       WMP_cnt_add(1); 
#endif 
 
	       /* starting index for energy */ 
	       start_ene = MEM_CLASS+i_sf*MAX_L_SF; 
	     } 
 
#ifdef WMOPS 
	     WMP_cnt_mult(1); 
	     WMP_cnt_add(1); 
#endif 
 
	     /* end index for energy */ 
	     end_max = (MEM_CLASS+MAX_L_SF+L_LPCLHD)+i_sf*MAX_L_SF; 
 
#ifdef WMOPS 
	     WMP_cnt_mult(1); 
	     WMP_cnt_add(1); 
#endif 
 
	     /* end index for energy */ 
	     end_ene = (MEM_CLASS+MAX_L_SF)+i_sf*MAX_L_SF; 
 
	     /*-----------------------------------------------------------*/ 
	     /*                  Find max in pitch cycle                  */ 
	     /*-----------------------------------------------------------*/ 
 
	     abs_max_dvector (sig_ppr_mem, &max, start_max, end_max-1); 
 
			 
	     /*-----------------------------------------------------------*/ 
	     /*                Find energy in pitch cycle                 */ 
	     /*-----------------------------------------------------------*/ 
		  
	     dot_dvector(sig_ppr_mem, sig_ppr_mem, &E_seg, start_ene, end_ene-1); 
 
			  
	     /*-----------------------------------------------------------*/  
	     /*                     Normalize energy                      */ 
	     /*-----------------------------------------------------------*/ 
 
#ifdef WMOPS 
	     WMP_cnt_add(1); 
	     WMP_cnt_div(1); 
#endif 
 
	     E_seg = 1.0+E_seg/(FLOAT64)(end_ene-start_ene+1); 
 
	     /*-----------------------------------------------------------*/  
	     /*           Update noise level, noise tilt, noise max       */ 
	     /*-----------------------------------------------------------*/ 
 
#ifdef WMOPS 
	     WMP_cnt_mac(4); 
	     WMP_cnt_mult(5); 
	     WMP_cnt_test(3); 
	     WMP_cnt_logic(1); 
#endif 
 
	     idx = (INT16)((FLOAT64)i_sf/2.0); 
 
	     if (frm_count < 6 && buffer_refl0[idx] < -0.4 && Rp < 0.50) 
	       { 
		 /*-------------------------------------------------------*/ 
		 /*           Relaxed update with fast adaptation         */ 
		 /*-------------------------------------------------------*/ 
		  
		 E_noi = 0.800 * E_noi + 0.200 * E_seg; 
		 T_noi = 0.750 * T_noi + 0.250 * buffer_refl0[idx]; 
		 M_noi = 0.750 * M_noi + 0.250 * max; 
		 R_noi = 0.750 * R_noi + 0.250 * Rp; 
	       } 
	     else if(!Vad) 
	       { 
		 /*-------------------------------------------------------*/ 
		 /*          Stringent update with slow adaptation        */ 
		 /*-------------------------------------------------------*/ 
		  
		 E_noi = 0.999 * E_noi + 0.001 * E_seg; 
		 T_noi = 0.990 * T_noi + 0.010 * buffer_refl0[idx]; 
		 M_noi = 0.990 * M_noi + 0.010 * max; 
		 R_noi = 0.990 * R_noi + 0.010 * Rp; 
	       } 
	      
	     /*-----------------------------------------------------------*/ 
	     /*                  Calculate weighting factor               */ 
	     /*-----------------------------------------------------------*/ 
	      
#ifdef WMOPS 
	     WMP_cnt_div(1); 
	     WMP_cnt_sqrt(1); 
	     WMP_cnt_test(1); 
#endif 
	     nsr = sqrt(E_noi/E_seg); 
		  
	     /*-----------------------------------------------------------*/ 
	     /*              Limit noise suppression to 30dB.             */ 
	     /*                 nsr_lim=1-10^(-dB_lim/20)                 */ 
	     /*            10dB: 0.684, 20dB: 0.900, 30dB: 0.968          */ 
	     /*-----------------------------------------------------------*/ 
 
	     nsr = (nsr > 0.968 ? 0.968 : nsr); 
 
	     /*-----------------------------------------------------------*/ 
	     /*   Update buffer of weighted tilt, weighted maximum, and   */ 
	     /* 	          weighted pitch correlation                  */ 
	     /*-----------------------------------------------------------*/ 
	      
	     cpy_dvector (buffer_wtilt+1, buffer_wtilt, 0, MAX_N_SF-2); 
	     cpy_dvector (buffer_wmax+1 , buffer_wmax , 0, MAX_N_SF-2); 
	      
 
	     /*-----------------------------------------------------------*/ 
	     /*   Suppress noise component in tilt, maximum, and pitch    */ 
	     /*                       correlation                         */ 
	     /*-----------------------------------------------------------*/ 
 
#ifdef WMOPS 
	     WMP_cnt_mult(2); 
	     WMP_cnt_sub(2); 
#endif 
 
	     buffer_wtilt[MAX_N_SF-1] = buffer_refl0[idx] - nsr*T_noi; 
	     buffer_wmax[MAX_N_SF-1]  = max               - nsr*M_noi; 
	     wRp                      = Rp                - nsr*R_noi; 
 
	     /*-----------------------------------------------------------*/ 
	     /*   Calculate slope of weighted tilt and weighted maximum   */ 
	     /*-----------------------------------------------------------*/ 
	      
#ifdef WMOPS 
	     WMP_cnt_mac(2*MAX_N_SF); 
	     WMP_cnt_sub(2*MAX_N_SF); 
#endif 
	     num_tilt = 0.0; 
	     num_max = 0.0; 
	     for (k = 1; k < MAX_N_SF; k++) 
	       { 
		 num_tilt += (FLOAT64)k*(buffer_wtilt[k]-buffer_wtilt[0]); 
		 num_max  += (FLOAT64)k*(buffer_wmax[k]-buffer_wmax[0]); 
	       } 
 
	     /*-----------------------------------------------------------*/ 
	     /*      Update sum, max, min, avg, etc of weighted tilt,     */ 
	     /*      weighted maximum, and weighted pitch correlation     */ 
	     /*-----------------------------------------------------------*/ 
			 
#ifdef WMOPS 
	     WMP_cnt_test(2); 
	     WMP_cnt_mult(2); 
	     WMP_cnt_add(2); 
#endif 
 
	     slope_wtilt = num_tilt*INV_SUM_SQR_MAX_N_SF; 
	     if(slope_wtilt < min_slope_wtilt) 
	       min_slope_wtilt = slope_wtilt; 
	      
	     slope_wmax = num_max*INV_SUM_SQR_MAX_N_SF; 
	     if(slope_wmax > max_slope_wmax) 
	       max_slope_wmax = slope_wmax; 
	     sum_slope_wmax += slope_wmax; 
	      
	     /*-----------------------------------------------------------*/ 
	     /*        Update minimum and average of weighted tilt        */ 
	     /*-----------------------------------------------------------*/ 
 
#ifdef WMOPS 
	     WMP_cnt_test(2); 
	     WMP_cnt_add(2); 
#endif 
	     if(buffer_wtilt[MAX_N_SF-1] < min_wtilt) 
	       min_wtilt = buffer_wtilt[MAX_N_SF-1]; 
	     avg_wtilt += buffer_wtilt[MAX_N_SF-1]; 
	      
	     /*-----------------------------------------------------------*/ 
	     /*  Update maximum and average of weighted pitch correlation */ 
	     /*-----------------------------------------------------------*/ 
	      
	     if(wRp > max_wRp) 
	       max_wRp = wRp; 
	      
	     avg_wRp += wRp; 
 
	     /*-----------------------------------------------------------*/ 
	   } 
 
#ifdef WMOPS 
         WMP_cnt_mult(2); 
         WMP_cnt_mac(2); 
#endif 
 
	 /*-------------------------------------------------------------------*/ 
	 /*       Normalize average weighted tilt and pitch correlation       */ 
	 /*                   (put into the thresholds)                       */ 
	 /*-------------------------------------------------------------------*/ 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                Update smoothed weighted spectral tilt             */ 
	 /*-------------------------------------------------------------------*/ 
 
	 AR_min_wtilt = 0.75*AR_min_wtilt + 0.25*min_wtilt; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                   Update smoothed pitch correlation               */ 
	 /*-------------------------------------------------------------------*/ 
 
	 AR_avg_wRp = 0.75*AR_avg_wRp + 0.25*avg_wRp; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                          Onset detection                          */ 
	 /*-------------------------------------------------------------------*/ 
	  
#ifdef WMOPS 
         WMP_cnt_test(30); 
         WMP_cnt_move(3); 
	 WMP_cnt_add(14); 
#endif 
 
	 onstplsv = 0; 
	 cond[0] = (min_slope_wtilt < -0.09090); 
	 cond[1] = (min_slope_wtilt < -0.11365); 
	 cond[2] = (min_slope_wtilt < -0.06820); 
	 cond[3] = (sum_slope_wmax > 150.0); 
 
	 if(buffer_wtilt[MAX_N_SF-1] < 0.075) 
	   { 
	     onstplsv += (min_slope_wtilt < -0.17045 && sum_slope_wmax > 250.0); 
	     onstplsv += cond[1] * (sum_slope_wmax > 270.0); 
	     onstplsv += cond[0] * (sum_slope_wmax > 310.0); 
	     onstplsv += cond[0] * (sum_slope_wmax > 250.0 && max_slope_wmax > 100.0); 
	     onstplsv += cond[2] * cond_mem[0] * cond[3] * cond_mem[1]; 
	   } 
	 if(buffer_wtilt[MAX_N_SF-1] < -0.075) 
	   { 
	     onstplsv += (min_slope_wtilt < -0.04545 && sum_slope_wmax > 400.0); 
	     onstplsv += (min_slope_wtilt < -0.03865 && sum_slope_wmax > 600.0); 
	   } 
	 if(buffer_wtilt[MAX_N_SF-1] < -0.100) 
	   { 
	     onstplsv += (min_slope_wtilt < -0.02955 && sum_slope_wmax > 800.0); 
	     onstplsv += (min_slope_wtilt < -0.01818 && sum_slope_wmax > 1000.0); 
	   } 
	 if(buffer_wtilt[MAX_N_SF-1] < -0.150) 
	   { 
	     onstplsv += cond[2] * (max_slope_wmax > 120.0); 
	     onstplsv += cond[1] * (max_slope_wmax > 70.0); 
	     onstplsv += (min_slope_wtilt < -0.12500 && max_slope_wmax > 60.0); 
	     onstplsv += (min_slope_wtilt < -0.20455 && max_slope_wmax > 30.0); 
	   } 
	 onstplsv += cond[0] * cond_mem[2] * (buffer_wtilt[MAX_N_SF-1] < 0.000); 
 
	 /*-------------------------------------------------------------------*/ 
	 /*           update buffered conditions for onset detection          */ 
	 /*-------------------------------------------------------------------*/ 
 
	 cond_mem[0] = cond[2]; 
	 cond_mem[1] = cond[3]; 
	  
	 cond_mem[2] = (max_slope_wmax > 300.0); 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                      Voiced speech detection                      */ 
	 /*-------------------------------------------------------------------*/   
	  
#ifdef WMOPS 
         WMP_cnt_test(15); 
         WMP_cnt_move(1); 
	 WMP_cnt_add(7); 
#endif 
 
#ifdef WMOPS 
         WMP_cnt_mult(2); 
#endif 
	 k = (Lp_var < 0.03 * Lp_avg * Lp_avg); 
	  
	 voiced = (avg_wRp >= 5.6 || avg_wtilt < -6.0); 
	  
	 if(avg_wRp >= 4.0) 
	   { 
	     voiced += k; 
	     voiced += (min_wtilt < -0.05); 
	   } 
	 if(avg_wRp >= 1.6) 
	   { 
	     voiced += (avg_wtilt < -4.0); 
	     voiced += (min_wtilt < -0.70); 
	   } 
	 voiced += (AR_avg_wRp >= 4.8 && k); 
	 voiced += (AR_avg_wRp > 4.0 && max_wRp > 0.40 && min_wtilt < -0.40); 
	  
	 /*-------------------------------------------------------------------*/ 
	 /*                          Offset handling                          */ 
	 /*-------------------------------------------------------------------*/   
		 
#ifdef WMOPS 
         WMP_cnt_mult(2); 
#endif 
	 if(voiced_mem[1] && voiced_mem[0]) 
	   { 
	     voiced += (AR_avg_wRp > 4.4 && AR_min_wtilt < -0.10 && 
			Lp_var < 0.0075 * Lp_avg * Lp_avg); 
	   } 
	  
#ifdef WMOPS 
	 WMP_cnt_test(6); 
	 WMP_cnt_logic(8); 
	 WMP_cnt_move(3); 
#endif 
	 if (!Vad) 
	   { 
	     voiced = 0; 
	   } 
 
	 /*-------------------------------------------------------------------*/ 
	 /*==================== Make frame_class decision ====================*/ 
	 /*-------------------------------------------------------------------*/ 
 
	 frame_class = UNVOICED; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                     Onset frame_classification                    */ 
	 /*-------------------------------------------------------------------*/ 
 
       	 if ((frame_class_mem == SILENCE || frame_class_mem == NOISE_LIKE || 
	 	      frame_class_mem == UNVOICED) && voiced) 
	   frame_class = ONSET; 
	 else if (onstplsv) 
	   frame_class = ONSET; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                     Voiced frame_classification                   */ 
	 /*-------------------------------------------------------------------*/ 
 
	 else if((onstplsv_mem && !voiced) || voiced) 
	   frame_class = NON_STATIONARY_VOICED; 
 
	 if(!Vad) 
	   frame_class = SILENCE; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                      Update frame_class buffer                    */ 
	 /*-------------------------------------------------------------------*/ 
   
#ifdef WMOPS 
         WMP_cnt_move(4); 
#endif 
		 
	 frame_class_mem = frame_class; 
	 onstplsv_mem    = onstplsv; 
	 voiced_mem[0]   = voiced_mem[1]; 
	 voiced_mem[1]   = voiced; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                   Temporary Memory Deallocation                   */ 
	 /*-------------------------------------------------------------------*/ 
 
	 free_dvector (buffer_refl0, 0, CLA_MEM_SIZE-1); 
	 free_dvector (siglpc1,      0, LPC_WIN1-1); 
 
	 /*-------------------------------------------------------------------*/ 
 
	 return frame_class; 
	  
	 /*-------------------------------------------------------------------*/ 
	} 
 
/*----------------------------------------------------------------------------*/ 
 
/*===================================================================*/ 
/* FUNCTION:  CLA_NoisUnvoiceDetect ().                              */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE:  This function temporally detects noise-like unvoiced    */ 
/*           speech to reset the delay for the LT preprocessing and  */ 
/*           control the excitation weighting. The decision will be  */ 
/*           finalized after pitch-preprocessing.                    */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*         _ (FLOAT64 [])  residu:    input residual signal.         */ 
/*         _ (FLOAT64 [])  sig:       input speech signal.           */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*         _ (INT16    *)  uv_mode:     voiced/unvoiced decision.    */ 
/*         _ (FLOAT64 [])  frm_erg:     sub-frame energies.          */ 
/*         _ (FLOAT64  *)  frm_sharp:   frame sharpness.             */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*                            _ None.                                */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*                            _ None.                                */ 
/*===================================================================*/ 
 
void CLA_NoisUnvoiceDetect (FLOAT64 residu[], FLOAT64 sig[], INT16 *uv_mode,  
			    FLOAT64 frm_erg[], FLOAT64 *frm_sharp) 
	{ 
	 /*-------------------------------------------------------------------*/ 
	  
	 FLOAT64 P1_SHP, P2_R1, P3_ZC, P4_RE; 
	 FLOAT64 X, X1, X2, val; 
	 INT16	 i, N; 
	  
	 /*-------------------------------------------------------------------*/ 
	 /*                   Calculate parameter 1 : P1_SHP                  */ 
	 /*-------------------------------------------------------------------*/ 
   
#ifdef WMOPS 
         WMP_cnt_move(1); 
#endif 
 
	 P1_SHP = PPP_sharpness (L_FRM, residu); 
	 (*frm_sharp) =  P1_SHP; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                   Calculate parameter 2 : P2_R1                   */ 
	 /*-------------------------------------------------------------------*/ 
 
	 dot_dvector (sig+L_FRM/2, sig+L_FRM/2+1, &X,  0, L_FRM/2-2); 
	 dot_dvector (sig+L_FRM/2, sig+L_FRM/2,   &X2, 0, L_FRM/2-1); 
	 P2_R1 = X / MAX(X2, 1.0); 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                   Calculate parameter 3 : P3_ZC                   */ 
	 /*-------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
         WMP_cnt_test(L_FRM/2-1); 
         WMP_cnt_add(L_FRM/2-1); 
         WMP_cnt_mult(L_FRM/2); 
#endif 
	 N = 0; 
	 for (i = 0; i < L_FRM/2-1; i++) 
	   { 
	     if (sig[i+L_FRM/2]*sig[i+L_FRM/2+1] < 0) 
	       N++; 
	   } 
	  
	 P3_ZC = (FLOAT64)N/(L_FRM/2-1.0); 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                   Calculate parameter 4 : P4_RE                   */ 
	 /*-------------------------------------------------------------------*/ 
  
	 dot_dvector (residu, residu, frm_erg,  0, L_FRM/2-1); 
	 dot_dvector (residu+L_FRM/2, residu+L_FRM/2, frm_erg+1, 0, L_FRM/2-1); 
 
#ifdef WMOPS 
         WMP_cnt_test(2); 
         WMP_cnt_sub(1); 
         WMP_cnt_sqrt(1); 
         WMP_cnt_div(1); 
#endif 
	 X1 = frm_erg[1]; 
	 X1 = sqrt(X1/MAX(1.0, X2)); 
	 P4_RE = 1.0-MIN(1.0, X1);  
         
	 /*-------------------------------------------------------------------*/ 
	 /*                     Make noise-like decision                      */ 
	 /*-------------------------------------------------------------------*/ 
 
  
#ifdef WMOPS 
         WMP_cnt_test(L_FRM+5); 
         WMP_cnt_abs(L_FRM); 
         WMP_cnt_add(L_FRM); 
         WMP_cnt_logic(4); 
         WMP_cnt_div(1); 
         WMP_cnt_mult(1); 
         WMP_cnt_move(5); 
#endif 
	 N = 0; 
	  
	 for (i = 0; i < L_FRM; i++) 
	   { 
	     if (fabs(sig[i]) < 0.1)  
	       N++; 
	   }  
	  
	 dot_dvector (sig, sig, &val, 0, (INT32)(L_FRM*0.75)-1); 
	 X2 =  val / (L_FRM*0.75); 
	 if (X2 < 1000.0 || N*1.0/L_FRM > 0.5)  
	   *uv_mode=0; 
	 	 
         if (P1_SHP>0.15) *uv_mode=1; 
	 if ((P2_R1 < 0.60) && (P1_SHP > 0.20)) 
	   (*uv_mode) = 2; 
	 if ((P3_ZC > 0.40) && (P1_SHP > 0.18)) 
	   (*uv_mode) = 2; 
	 if ((P4_RE < 0.40) && (P1_SHP > 0.20)) 
	   (*uv_mode) = 2; 
 
	 /*-------------------------------------------------------------------*/ 
	  
	 return; 
	  
	 /*-------------------------------------------------------------------*/ 
	} 
 
/*----------------------------------------------------------------------------*/ 
 
/*===================================================================*/ 
/* FUNCTION:  CLA_Class_Correct ().                                  */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE:  This function works for                                 */ 
/*              (1) improving voiced-unvoiced decision;              */ 
/*              (2) improving noise-like unvoiced decision;          */ 
/*              (3) detecting specific "onset";                      */ 
/*              (4) making classification for pitch preprocessing;   */ 
/*              (5) improving vad;                                   */ 
/*              (6) detecting voied speech with high prediction gain */ 
/*                    but noise-like HF excitation.                  */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*  _ (INT16     ) music_flg     : music detection flag.             */ 
/*  _ (INT16     ) frame_class_m : last frame class.                 */ 
/*  _ (FLOAT64 []) Rp_sub[]      : autocorr. maxima values.          */ 
/*  _ (FLOAT64   ) nsr           : noise-to-signal ratio.            */ 
/*  _ (FLOAT64 **) refl          : reflection coeffciients.          */ 
/*  _ (FLOAT64   ) frm_sharp     : frame sharpeness estimation.      */ 
/*  _ (FLOAT64   ) energy        : frame energy.                     */ 
/*  _ (FLOAT64   ) energy_m      : last frame energy.                */ 
/*  _ (FLOAT64   ) FrmResEng     : residual frame energy.            */ 
/*  _ (FLOAT64   ) lpcgain       : LPC analysisprediction gain.      */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*     _ (INT16 *) OnSetFlag    : on-set detction flag.              */ 
/*     _ (INT16 *) NoisyV_flag  : noisy voiced detction flag.        */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*     _ (INT16 *) frame_class   : current frame class.              */ 
/*     _ (INT16 *) frame_class_pp: current frame class for           */ 
/*                                  pitch pre-processing.            */ 
/*     _ (INT16 *) Vad           : voice activity detect. flag.      */ 
/*     _ (INT16 *) VUV           : voiced/unvoiced detection flag    */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*            _None                                                  */ 
/*===================================================================*/ 
 
void CLA_Class_Correct (INT16 music_flg, INT16 *frame_class,  
			INT16 *frame_class_pp, 
			INT16 *Vad, INT16 *VUV, INT16 *OnSetFlag,  
			INT16 *LPC_OnSet,  
			INT16 frame_class_m, FLOAT64 Rp_sub[], FLOAT64 NSR, 
			FLOAT64 **refl, FLOAT64 frm_sharp, INT16 smv_mode, 
			FLOAT64 energy_m, FLOAT64 energy,  
			FLOAT64 FrmResEng, FLOAT64 lpcgain, 
			INT16 *NoisyV_flag) 
	{ 
	 /*-------------------------------------------------------------------*/ 
 
	 FLOAT64 x, y; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                        Class correction                           */ 
	 /*-------------------------------------------------------------------*/ 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                         voiced to unvoiced                        */ 
	 /*-------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
         WMP_cnt_test(6); 
         WMP_cnt_logic(3); 
         WMP_cnt_sub(1); 
         WMP_cnt_mult(1); 
         WMP_cnt_move(2); 
#endif 
	 x = MIN(3*frm_sharp-0.2, 0.65); 
	  
	 if ((*frame_class>UNVOICED) && (Rp_m_cla0.75) || (Rp_sub[1]>0.75))) 
	 	(*VUV) = 3;   
	 	 
	 if ((*VUV==2) && (refl[2][0]>-0.1) &&  
	 	((Rp_sub[0]>0.55) || (Rp_sub[1]>0.55)))  
	 		(*VUV) = 3;   
	 		 
	 if ((*VUV==2) && (frm_sharp>0.2) && ((Rp_m_cla>0.75) ||  
      	     (Rp_sub[2]>0.75))) 
	        (*VUV) = 3;  
	 		 
	 if ((*frame_classSILENCE) &&  
		 (((NSR < 0.25) && (Rp_sub[1]<0.7) && (frm_sharp>0.16)) || 
                                 ((Rp_sub[1]<0.5) && ((*VUV)==2)))) 
		                          (*frame_class) = NOISE_LIKE; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                         unvoiced to voiced                        */ 
	 /*-------------------------------------------------------------------*/ 
	  
#ifdef WMOPS 
         WMP_cnt_test(13); 
         WMP_cnt_logic(9); 
         WMP_cnt_add(3); 
         WMP_cnt_move(3); 
#endif 
 
         if (((*frame_class) < ONSET) && (frame_class_m > UNVOICED) &&  
             (Rp_sub[0] > 0.43) && (Rp_sub[1] > 0.43)) 
	   { 
	     if ((frm_sharp < 0.15) && ((Rp_sub[0] + Rp_sub[1]) > 0.9) &&  
		 (refl[2][0] > -0.4)) 
	       (*frame_class) = NON_STATIONARY_VOICED; 
	     if ((frm_sharp < 0.25) && ((Rp_sub[0]+Rp_sub[1]) > 1.0) &&  
		 (refl[2][0] > -0.1)) 
	       (*frame_class) = NON_STATIONARY_VOICED; 
	     if ((frm_sharp < 0.3) && ((Rp_sub[0]+Rp_sub[1]) > 1.15) &&  
		 (refl[2][0] >-0.1)) 
	       (*frame_class) = NON_STATIONARY_VOICED; 
	   }	 
	  
         if ( ((*frame_class) < ONSET) && (frame_class_m > UNVOICED) && 
	      ((Rp_sub[0] > 0.75) || (Rp_sub[1] > 0.75) )) 
	   (*frame_class) = NON_STATIONARY_VOICED; 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                           Onset detection                         */ 
	 /*-------------------------------------------------------------------*/ 
	  
#ifdef WMOPS 
         WMP_cnt_test(4); 
         WMP_cnt_logic(2); 
         WMP_cnt_sub(1); 
         WMP_cnt_div(1); 
         WMP_cnt_move(1); 
#endif 
	 x = energy/MAX(energy_m, 0.1); 
	 y = refl[0][0]-refl[N_SF4-1][0]; 
	 (*OnSetFlag) = (*frame_class == ONSET) && (y+0.2*x-0.6>0) && (Rp_sub[2]>0.5); 
       
	 (*LPC_OnSet) =  (*OnSetFlag==1) && (Rp_sub[0]<0.75) && 
	   ( (x>8)|| ((x>2.5) && (Rp_sub[0]<1.1*Rp_sub[1])) || (y>0.4) ); 
 
	 /*-------------------------------------------------------------------*/ 
	 /*                Classification for pitch preprocessing             */ 
	 /*-------------------------------------------------------------------*/ 
	  
#ifdef WMOPS 
         WMP_cnt_test(8); 
         WMP_cnt_logic(3); 
         WMP_cnt_move(5); 
#endif 
 
         if (*frame_class >= ONSET) 
         	{ 
		  (*frame_class_pp) = 2;  
		 if (Rp_sub[1] > 0.5) 
		   (*frame_class_pp) = 3; 
		 if (Rp_sub[1] > 0.75) 
		   (*frame_class_pp) = 4; 
		} 
	 else 
	 	(*frame_class_pp) = 0;  
        if ((*VUV) == 0) 
        	(*frame_class_pp) = 0; 
        if ((frame_class_m < ONSET) &&  
           ((*frame_class < UNVOICED) || (((*frame_class) == UNVOICED) &&  
	      (Rp_sub[1] < 0.5))))  
		(*frame_class_pp) = -1; 
  
	 /*-------------------------------------------------------------------*/ 
	 /*                   Making the VAD more agressive                   */ 
	 /*-------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
         WMP_cnt_test(6); 
         WMP_cnt_logic(4); 
         WMP_cnt_move(2); 
#endif 
 
	 if(music_flg == 0) 
	   { 
	     if (smv_mode == 2 || smv_mode == 3) 
	       { 
		 if ((FrmResEng < 400*L_FRM) && (refl[2][0] < 0.125) && 
		     (Rp_sub[1] < 0.55) && ((*frame_class) < ONSET) && 
		     (frm_sharp > 0.15)) 
		   { 
		     (*frame_class) = SILENCE; 
		     (*Vad) = 0; 
		   } 
	       } 
	     else 
	       { 
		 if (((NSR > 0.6) || (FrmResEng < 100*L_FRM)) &&  
		     (refl[2][0] < 0) && (Rp_sub[1] < 0.4) && 
		     ((*frame_class) < ONSET) && (frm_sharp > 0.18)) 
		   { 
		     (*frame_class) = SILENCE; 
		     (*Vad) = 0; 
		   } 
	       } 
	   } 
  
	 /*-------------------------------------------------------------------*/ 
	 /*       Voiced speech with high prediction gain but noise-like      */ 
	 /*                          HF excitation                            */ 
	 /*-------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
         WMP_cnt_test(5); 
         WMP_cnt_logic(3); 
         WMP_cnt_mult(2); 
         WMP_cnt_add(2); 
         WMP_cnt_sub(1); 
         WMP_cnt_move(3); 
#endif 
 
	 x = 10*frm_sharp - 1.5*refl[2][0] + Rp_sub[1] + lpcgain/20.0;  
	 *NoisyV_flag = 0; 
	 if ((*frame_class) > UNVOICED) 
	   { 
	     if (x > 4.5) 
	       (*NoisyV_flag) = NOISE_LIKE; 
	     if ((x > 5.7) && (frm_sharp > 0.29) && (refl[2][0] < -0.92)) 
	       (*NoisyV_flag) = UNVOICED; 
	   } 
	  
	 Rp_m_cla = Rp_sub[1];       
 
	 /*-------------------------------------------------------------------*/ 
	 
	 return; 
    
	 /*-------------------------------------------------------------------*/ 
	} 
 
/*----------------------------------------------------------------------------*/ 
 
/*===================================================================*/ 
/* FUNCTION:  CLA_Rate_Select ().                                    */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE:   This function select the SMV bitrate.                  */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*  _ (INT16     ) music_flg     : music detection flag.             */ 
/*  _ (INT16     ) smv_mode      : codec requested mode.             */ 
/*  _ (INT16     ) frame_class   : current frame class.              */ 
/*  _ (INT16     ) frame_class_m : last frame class.                 */ 
/*  _ (INT16     ) onsetflag     : on-set detction flag.             */ 
/*  _ (FLOAT64 []) Rp            : autocorr. maxima values.          */ 
/*  _ (FLOAT64   ) nsr           : noise-to-signal ratio.            */ 
/*  _ (FLOAT64   ) sharp         : frame sharpeness estimation.      */ 
/*  _ (FLOAT64   ) refl0         : first reflection coeff.           */ 
/*  _ (FLOAT64   ) frm_eng       : residual frame energy.            */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*  _ (INT16 *) codec_rate    : selected codec rate.                 */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*            _None                                                  */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*            _None                                                  */ 
/*===================================================================*/ 
 
void CLA_Rate_Select(INT16 music_flg, INT16 smv_mode, INT16 frame_class,  
		     INT16 frame_class_m, INT16 onsetflag, FLOAT64 Rp[], 
		     FLOAT64 NSR, FLOAT64 sharp, FLOAT64 refl0,  
		     FLOAT64 frm_eng, INT16 *codec_rate) 
	{ 
	 /*-------------------------------------------------------------------*/ 
	  
	 FLOAT64 x; 
	  
	 /*-------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
         WMP_cnt_test(1); 
#endif 
	  
	 if (music_flg == 1) 
            (*codec_rate) = RATE8_5K; 
         else  
	   { 
#ifdef WMOPS 
	     WMP_cnt_test(1); 
#endif 
	     if (smv_mode==0) 
	       { 
#ifdef WMOPS 
		 WMP_cnt_move (7); 
		 WMP_cnt_test (15); 
#endif 
		 /*-----------------------------------------------------------*/ 
		 /*      Mode 0. Note: average rate must be equal to EVRC     */ 
		 /*-----------------------------------------------------------*/ 
		  
		 (*codec_rate) = RATE8_5K; 	   	     
                    
		 /*-----------------------------------------------------------*/ 
		    
		 if ((frame_class == NOISE_LIKE) && (sharp > 0.2) &&  
		     (Rp[1] < 0.355) && (Rp[2] < 0.332))  
		   (*codec_rate) = RATE4_0K; 
	  
		 if ((frame_class == NOISE_LIKE) && (NSR > 0.15) &&  
		     (Rp[1] < 0.5) && (Rp[2] < 0.5))  
		   (*codec_rate) = RATE4_0K; 
		  
		 if ((frame_class < ONSET) && (NSR > 0.5) && (refl0 < 0.0) && 
		     (Rp[1] < 0.5))  
		   (*codec_rate) = RATE4_0K;	 
		  
		 if (frame_class==SILENCE) 
		   (*codec_rate) = RATE4_0K; 
		  
		 /*-----------------------------------------------------------*/ 
		  
		 if ((frame_class == SILENCE) && (frame_class_m==SILENCE)) 
		   (*codec_rate) = RATE0_8K; 
		  
		 /*-----------------------------------------------------------*/  
	       } 
	     else 
	       { 
#ifdef WMOPS 
		 WMP_cnt_test (1); 
#endif 
		 if (smv_mode==1) 
		   { 
		     /*-----------------------------------------------------*/ 
		     /*    Mode 1. Note: average rate must be =  0.7 EVRC   */ 
		     /*-----------------------------------------------------*/ 
#ifdef WMOPS 
		     WMP_cnt_move (1); 
#endif 
		      
		     (*codec_rate) = RATE8_5K; 
		      
		     /*-----------------------------------------------------*/ 
 
#ifdef WMOPS 
		     WMP_cnt_test (1); 
		     WMP_cnt_move (1); 
		     WMP_cnt_mac  (1); 
		     WMP_cnt_test (4); 
		     WMP_cnt_move (1); 
#endif 
 
#if (defined(VAD_B)||defined(NS_B))   
                     x = MAX(0.85-1.25*NSR, 0.65); 
#else 
		     x = MAX(0.86-NSR, 0.65); 
#endif		   		        
		      
		     if ((frame_class > ONSET) &&  
			 (frame_class_m > NON_STATIONARY_VOICED) &&  
			 (Rp[0] > x) && (Rp[1] > x))  
		       (*codec_rate) = RATE4_0K;	     
		   } 
		 else 
		   {                
		     /*------------------------------------------------------*/ 
		     /*    Mode 2. Note: average rate must be = 0.55 EVRC    */ 
		     /*------------------------------------------------------*/ 
		     /*    Mode 3. Note: average rate must be < 0.55 EVRC    */ 
		     /*------------------------------------------------------*/ 
#ifdef WMOPS 
		     WMP_cnt_move (2); 
		     WMP_cnt_test (6); 
#endif 
		      
		     (*codec_rate) = RATE4_0K;	    		 
		      
		     /*------------------------------------------------------*/ 
		      
		     if ((OnSet == 1) && (NSR > 0.02) && (frame_class > UNVOICED))  
		       (*codec_rate) = RATE8_5K; 
		      
		     if ((OnSet == 1) && (Rp[0] < 0.85) && (frame_class > UNVOICED)) 
		       (*codec_rate) = RATE8_5K; 
 
#ifdef WMOPS 
		     WMP_cnt_move (2); 
		     WMP_cnt_test (7); 
#endif 
 
		     OnSet = 0; 
		     if ((onsetflag == 1) || ((frame_class_m < ONSET) &&  
			  (frame_class >= ONSET)) ) 
		       OnSet = 1; 
		     if ((frame_class == ONSET)&&(frame_class_m != ONSET || Rp[0]<0.85  
		          || Rp[1]<0.85))  
		       OnSet = 1;  
 
#ifdef WMOPS 
		     WMP_cnt_move (1); 
		     WMP_cnt_test (4); 
#endif 
	    
		     if (OnSet == 1)  
		       (*codec_rate) = RATE8_5K;	     
	      
		     if ((NSR > 0.025) && (frame_class > UNVOICED) && (Rp[1] < 0.57))  
		       (*codec_rate) = RATE8_5K; 
		      
		     /*-----------------------------------------------------*/ 
#ifdef WMOPS 
		     WMP_cnt_test (1); 
#endif 
		      
		     if (smv_mode==2) 
		       {      
#ifdef WMOPS 
			 WMP_cnt_test (11); 
			 WMP_cnt_move (6); 
#endif 
 
			 if ((frame_class > ONSET) && (refl0 < -0.4) &&  
			     (Rp[0] < 0.72 || Rp[1] < 0.72) && (sharp < 0.15)) 
			   (*codec_rate) = RATE8_5K; 
			  
			 if ((frame_class>ONSET) && (frm_eng < 2500*L_FRM)) 
			   (*codec_rate) = RATE4_0K; 
			  
			 if ((frame_class>ONSET) && (frm_eng < 5000*L_FRM) &&  
			     (frame_class_m < ONSET) && (Rp[1] < 0.6)) 
			   (*codec_rate) = RATE4_0K;	 			  
		       } 
		     else 
		       { 
#ifdef WMOPS 
			 WMP_cnt_test (21); 
			 WMP_cnt_move (3); 
			 WMP_cnt_sub  (2); 
			 WMP_cnt_abs  (2); 
#endif 
			 if ((frame_class > ONSET) && (refl0 < -0.8) && 
			     (Rp[1] < 0.5) && (sharp < 0.15)) 
			   (*codec_rate) = RATE8_5K; 
			  
			 if (frame_class == NON_STATIONARY_VOICED) 
			   (*codec_rate) = RATE4_0K; 
			  
			 if (frame_class== ONSET && frame_class_m==0 && refl0>-0.8) 
			   (*codec_rate) = RATE4_0K; 
			  
			 if (frame_class== ONSET&& Rp_m1>0.5 && Rp[0]>0.6 &&  
			     Rp[1]>0.6 && fabs(Rp[0]-Rp_m1)<0.2 && 
			     fabs(Rp[1]-Rp[0])<0.2 && refl0>-0.8) 
			   (*codec_rate) = RATE4_0K; 
 
			  
			 if ((frame_class>= ONSET) && (frm_eng < 2500*L_FRM)) 
			   (*codec_rate) = RATE4_0K; 
			  
			 if ((frame_class>=ONSET) && (frm_eng < 5000*L_FRM) && 
			     (frame_class_m < ONSET) && (Rp[1] < 0.6)) 
			   (*codec_rate) = RATE4_0K; 
		       } 
		   } 
		  
		 /*--------------------------------------------------------*/ 
#ifdef WMOPS 
		 WMP_cnt_test  (1); 
		 WMP_cnt_move  (1); 
#endif 
		 if (frame_class < ONSET)  
		   (*codec_rate) = RATE4_0K;		   		     
		  
		 /*--------------------------------------------------------*/ 
 
#ifdef WMOPS 
		 WMP_cnt_test  (2); 
		 WMP_cnt_move  (1); 
 
		 WMP_cnt_test  (7); 
		 WMP_cnt_move  (2); 
		 WMP_cnt_sub   (1); 
#endif 
 
		 if (smv_mode==1)  
		   x = MIN(NSR, 0.5); 
		 else	    
		   x = 0.75*NSR; 
	  
		 if ( (frame_class == NOISE_LIKE) && (sharp > 0.1) && 
		      (Rp[0]< 0.75-x) && (Rp[1]< 0.75-x) && (Rp[2]< 0.55) ) 
		   (*codec_rate) = RATE2_0K; 
		 if ( (frame_class == NOISE_LIKE) && (frm_eng < 1000*L_FRM) ) 
		   (*codec_rate) = RATE2_0K;     
 
#ifdef WMOPS 
		 WMP_cnt_test  (6); 
		 WMP_cnt_move  (3); 
#endif		  
		  
		 if (frame_class == UNVOICED)  
		   { 
		     if (frm_eng < 1000*L_FRM) 
		       (*codec_rate) = RATE2_0K; 
		     if ((sharp > 0.1) && (frm_eng < 2500*L_FRM) )  
		       (*codec_rate) = RATE2_0K; 
		     if ((sharp > 0.15) && (Rp[1] < 0.75-x)) 
		       (*codec_rate) = RATE2_0K; 
		   } 
 
#ifdef WMOPS 
		 WMP_cnt_test  (9); 
		 WMP_cnt_sub   (2); 
		 WMP_cnt_move  (4); 
#endif		  
		  
		 if ((frame_class == ONSET) && (Rp[1] < 0.55-x) &&  
		     (Rp[2] < 0.55-x)  )  
		   { 
		     if (frm_eng < 500*L_FRM) 
		       (*codec_rate) = RATE2_0K; 
		     if ((sharp > 0.15) && (frm_eng < 2000*L_FRM) )  
		       (*codec_rate) = RATE2_0K; 
		     if (sharp > 0.18 && Rp[2] < 0.5-x) 
		       (*codec_rate) = RATE2_0K; 
		     if (sharp > 0.25) 
		       (*codec_rate) = RATE2_0K; 
		   }	 
 
		 /*---------------------------------------------------------*/ 
#ifdef WMOPS 
		 WMP_cnt_test  (1); 
		 WMP_cnt_move  (1); 
#endif		  
		  
		 if (frame_class == SILENCE)  
		   (*codec_rate) = RATE0_8K;   
	       } 
 
	     /*-------------------------------------------------------------*/ 
 
	     if ((frame_class_m == STATIONARY_VOICED) &&  
		 (*codec_rate <= RATE2_0K)) 
	       (*codec_rate) = RATE4_0K; 
 
	     /*---------------------------------------------------------*/ 
	   } 
	  
	  
	 /*-------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
	 WMP_cnt_move  (1); 
#endif		  
	  
	 frame_class_mm = frame_class_m; 
	  
	 /*-------------------------------------------------------------------*/ 
	  
	 return; 
	  
	 /*-------------------------------------------------------------------*/ 
       } 
#endif 
 
/*----------------------------------------------------------------------------*/ 
 
/*===================================================================*/ 
/* FUNCTION:  CLA_Identify_InputSpeech ().                           */ 
/*-------------------------------------------------------------------*/ 
/* PURPOSE:  This function identifys input speech as flat speech.    */ 
/*           The decision needs at least two sentences to update     */ 
/*           the memory.                                             */ 
/*-------------------------------------------------------------------*/ 
/* INPUT ARGUMENTS  :                                                */ 
/*         _ (INT16     ) pp_mode:   = mode 1  or mode 0.            */ 
/*         _ (FLOAT64 []) pdcfq  :   quantized prediction coeff.     */ 
/*         _ (FLOAT64   ) lsf0   :   first lsf                       */ 
/*         _ (FLOAT64   ) ltp_g  :   past LTP gain.                  */ 
/*-------------------------------------------------------------------*/ 
/* OUTPUT ARGUMENTS :                                                */ 
/*         _ (FLOAT64  ) lpc_g     : current LPC gain.               */ 
/*         _ (INT16   *) flat_deci : flat speech decision.           */ 
/*-------------------------------------------------------------------*/ 
/* INPUT/OUTPUT ARGUMENTS :                                          */ 
/*         _ (INT16   *) SVS_Count   :  Stat. Voiced frame counter.  */ 
/*         _ (INT16   *) Vad_0_Count :  noisy frame counter.         */ 
/*         _ (FLOAT64 *) Av_value  : current LPC gain.               */ 
/*-------------------------------------------------------------------*/ 
/* RETURN ARGUMENTS :                                                */ 
/*                            _ None.                                */ 
/*===================================================================*/ 
 
void CLA_Identify_Input(INT16 pp_mode, FLOAT64 *pdcfq, FLOAT64 lsf0,  
			FLOAT64 ltp_g, FLOAT64 *lpc_g, INT16 fix_rate, 
			INT16 *flat_deci, INT16	*SVS_Count, INT16 *Vad_0_Count, 
			FLOAT64 *Av_value) 
	{ 
	 /*-------------------------------------------------------------------*/ 
 
         INT16	 i; 
         FLOAT64 sub_refl[NP], x; 
  
	 /*-------------------------------------------------------------------*/ 
	 /*       Calculate the quantized predictio coeff. prediction gain    */ 
	 /*-------------------------------------------------------------------*/ 
 
         LPC_pred2refl(pdcfq, sub_refl, NP); 
	  
#ifdef WMOPS 
         WMP_cnt_transc(1); 
         WMP_cnt_sub(NP); 
         WMP_cnt_mult(2*NP+1); 
         WMP_cnt_move(1); 
#endif 
	  
         (*lpc_g) = 1.0; 
         for (i = 0; i < NP; i++) 
           (*lpc_g) *= (1.0-sqr(sub_refl[i])); 
         (*lpc_g) = -10.0*log10((*lpc_g)+EPSI); 
 
	/*--------------------------------------------------------------------*/ 
	/*                            Vad counter                             */ 
	/*--------------------------------------------------------------------*/ 
 
#ifdef WMOPS 
         WMP_cnt_test(6); 
         WMP_cnt_mac(1); 
         WMP_cnt_mult(1); 
         WMP_cnt_add(2); 
         WMP_cnt_move(1); 
#endif 
	 if (fix_rate==RATE0_8K) 
	   (*Vad_0_Count)++; 
	 else 
	   (*Vad_0_Count) = 0; 
  
 	/*--------------------------------------------------------------------*/ 
	/*                            Decision                                */ 
	/*--------------------------------------------------------------------*/ 
	 
         if (pp_mode == 1)  
	   { 
	     /*---------------------------------------------------------------*/ 
 
	     x = MIN((*lpc_g)/35.0, 1.0) - 2.0*sub_refl[0]+ MIN(ltp_g, 1.0)  
	          - lsf0*15; 
	     x *= 2.5; 
	     
	     /*---------------------------------------------------------------*/ 
 
	     if ((*SVS_Count) == 0)  
	       (*Av_value) = 0.75*x; 
	     else 
	       { 
		 if ((*SVS_Count) < 10)  
		   (*Av_value) = 0.9*(*Av_value) + 0.1*x; 
                 else 
		   { 
		     if ((*SVS_Count) < 100)  
		       (*Av_value) = 0.99*(*Av_value) + 0.01*x; 
		     else (*Av_value) = 0.998*(*Av_value) + 0.002*x; 
		   } 
	       }  
		  
	     (*SVS_Count)++; 
	     (*SVS_Count) = MIN((*SVS_Count), 10000); 
 
	     /*---------------------------------------------------------------*/ 
	   } 
	 else 
	   { 
             if (((*Vad_0_Count)  > 10) && ((*SVS_Count)  > 200)) 
	       { 
		 if ((*Av_value) > 5.8)  
		   (*flat_deci) = 1; 
		  
		 if ((*Av_value) < 5.3) 
		   (*flat_deci) = 0;	  
	       } 
	   } 
 
	 /*-------------------------------------------------------------------*/ 
	 
	 return ; 
    
	 /*-------------------------------------------------------------------*/ 
	} 
 
/*----------------------------------------------------------------------------*/ 
 
/*============================================================================*/ 
/*------------------------------------- END ----------------------------------*/ 
/*============================================================================*/