⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ol_ltp.c

📁 AMR-NB 的编码实现,纯C, VC下建立工程即可用.
💻 C
📖 第 1 页 / 共 4 页
字号:
                     frame_offset = (i_subfr == 0) ? 0 : frame_offset;             

			/* get T_op from the corresponding half frame and */
			/* set t0_min, t0_max */

			//getRange (pT_op[frame_offset], delta_int_low, delta_int_range, pit_min, PIT_MAX, &t0_min, &t0_max);

			t0_min  =  pT_op[frame_offset] - delta_int_low;
			t0_min  =  (t0_min) < pit_min ? pit_min : t0_min  ;     
			t0_max =  (t0_min) + delta_int_range;
			t0_min  =  (t0_max) > PIT_MAX ?   PIT_MAX -delta_int_range : t0_min;
			t0_max =  (t0_max) > PIT_MAX ?  PIT_MAX : t0_max ;   
		}
		else
		{

			/* mode MR475, MR515 and 3. Subframe: delta search as well */
			//getRange (pst->T0_prev_subframe, delta_frc_low, delta_frc_range,  pit_min, PIT_MAX, &t0_min, &t0_max);

			t0_min  =  pst->T0_prev_subframe - delta_frc_low;
			t0_min  =  (t0_min) < pit_min ? pit_min : t0_min  ;     
			t0_max =  (t0_min) + delta_frc_range;
			t0_min  = (t0_max) > PIT_MAX ?   PIT_MAX -delta_frc_range : t0_min;
			t0_max = (t0_max) > PIT_MAX ?  PIT_MAX : t0_max ;   
			
		}
	}
	else
	{

		/* for Subframe 2 and 4 . get range around T0 of previous subframe for delta search */

		//getRange (st->T0_prev_subframe, delta_frc_low, delta_frc_range,  pit_min, PIT_MAX, &t0_min, &t0_max);
		t0_min  =  pst->T0_prev_subframe - delta_frc_low;
		t0_min  =  (t0_min) < pit_min ? pit_min : t0_min  ;     
		t0_max =  (t0_min) + delta_frc_range;
		t0_min  = (t0_max) > PIT_MAX ?   PIT_MAX -delta_frc_range : t0_min;
		t0_max = (t0_max) > PIT_MAX ?  PIT_MAX : t0_max ; 
		
	}
    /*-----------------------------------------------------------------------*     *           Find interval to compute normalized correlation             *     *-----------------------------------------------------------------------*/    t_min   =  t0_min - L_INTER_SRCH;
    t_max  =  t0_max + L_INTER_SRCH;
    corr     =  &corr_v[-t_min];                   
    /*-----------------------------------------------------------------------*     * Compute normalized correlation between target and filtered excitation *     *-----------------------------------------------------------------------*/    Norm_Corr (exc, xn, h, L_subfr, t_min, t_max, corr);    /*-----------------------------------------------------------------------*     *                           Find integer pitch                          *     *-----------------------------------------------------------------------*/    max = corr[t0_min];                                               lag = t0_min;                                                 for (i = t0_min + 1; i <= t0_max; i++)    {
        lag   = corr[i] >= max ? i : lag;
	 max = corr[i] >= max ? corr[i] : max;

    }    /*-----------------------------------------------------------------------*     *                        Find fractional pitch                          *     *-----------------------------------------------------------------------*/    if ((delta_search == 0) && (lag > max_frac_lag))    {        /* full search and integer pitch greater than max_frac_lag . fractional search is not needed, set fractional to zero */
        frac = 0;                                    }    else    {        /* if differential search AND mode MR475 OR MR515 OR MR59 OR MR67  .  then search fractional with 4 bits resolution           */
             
       if (  (delta_search != 0) &&
              ((mode == MR475) ||(mode == MR515) || (mode == MR59) || (mode ==MR67) ) )
         {          /* modify frac or last_frac according to position of last .  integer pitch: either search around integer pitch,  or only on left or right side */
                    tmp_lag = pst->T0_prev_subframe;           
                  if (  tmp_lag >  t0_min+ 5)             tmp_lag =  t0_min+5;                if ( t0_max > tmp_lag + 4)               tmp_lag = t0_max -4;                         if ((lag == tmp_lag) ||(lag == tmp_lag - 1) ) 
           {                          /* normal search in fractions around T0 */           
             searchFrac (&lag, &frac, last_frac, corr, flag3);                       }          else if (lag == tmp_lag - 2 ) 	{            
             /* limit search around T0 to the right side */             frac = 0;                                                      searchFrac (&lag, &frac, last_frac, corr, flag3);          }          else if ( lag == tmp_lag+ 1 ) 	    {                     /* limit search around T0 to the left side */             last_frac = 0;                                           searchFrac (&lag, &frac, last_frac, corr, flag3);          }          else 	   {         
             /* no fractional search */             frac = 0;                                                  }       }       else          /* test the fractions around T0 */          searchFrac (&lag, &frac, last_frac, corr, flag3);    }        /*-----------------------------------------------------------------------*     *                           encode pitch                                *     *-----------------------------------------------------------------------*/
    if (flag3 != 0)     {              /* flag4 indicates encoding with 4 bit resolution;         */       /* this is needed for mode MR475, MR515 and MR59           */              flag4 = 0;                                                      if ( ( mode ==  MR475) ||(mode ==  MR515) ||(mode == MR59) ||(mode == MR67) )       {          flag4 = 1;                                                  }              /* encode with 1/3 subsample resolution */       
       // *ana_index = Enc_lag3(lag, frac, pst->T0_prev_subframe, t0_min, t0_max, delta_search, flag4); 
  
	if (delta_search == 0)
	{ 
	     /* if 1st or 3rd subframe */
              index = lag <= 85 ? 3*lag  -58+ frac : lag+112;
	}
	else
	{   /* if second or fourth subframe */

		if (flag4 == 0) 
		{
			/* 'normal' encoding: either with 5 or 6 bit resolution .  index = 3*(T0 - T0_min) + 2 + T0_frac */
			index = 3*(lag - t0_min)  + 2 + frac;
		}
		else
		{

			/* encoding with 4 bit resolution */
		       tmp_lag = pst->T0_prev_subframe > t0_min+5 ? t0_min+5 : pst->T0_prev_subframe;
			tmp_lag = t0_max > tmp_lag +4 ? t0_max -4 : tmp_lag;
                     uplag = 3*lag + frac ;
			i = tmp_lag - 2;
			tmp_ind = 3*i ;

                     index = tmp_ind >= uplag ? lag - tmp_lag + 5 :3* (tmp_lag+ 1) > uplag ? uplag-tmp_ind+ 3 :  lag -  tmp_lag + 11 ;	

		} /* end if (encoding with 4 bit resolution) */
	}   /* end if (second of fourth subframe) */

	*ana_index = index;

	
    }      else    {       /* encode with 1/6 subsample resolution */       // *ana_index = Enc_lag6(lag, frac, t0_min, delta_search);

	if (delta_search == 0)          /* if 1st or 3rd subframe */
       {
       /* encode pitch delay (with fraction) */       index = lag <= 94 ? 6*lag -105 + frac : lag +368 ;
	       
       }
       else
       /* if second or fourth subframe */      {
       /* index = 6*(T0-T0_min) + 3 + T0_frac  */
        index = 6*(lag - t0_min) + 3 + frac;
      }

	*ana_index = index;

	
    }        /*-----------------------------------------------------------------------*     *                          update state variables                       *     *-----------------------------------------------------------------------*/        pst->T0_prev_subframe = lag;                                 
        /*-----------------------------------------------------------------------*     *                      update output variables                          *     *-----------------------------------------------------------------------*/        *resu3    = flag3;                                           
    *pit_frac = frac;                                             return (lag);
}


#define THRESHOLD 27853
/************************************************************************* * *  FUNCTION:  Lag_max * *  PURPOSE: Find the lag that has maximum correlation of scal_sig[] in a *           given delay range. * *  DESCRIPTION: *      The correlation is given by *           cor[t] = <scal_sig[n],scal_sig[n-t]>,  t=lag_min,...,lag_max *      The functions outputs the maximum correlation after normalization *      and the corresponding lag. * *************************************************************************/#ifdef VAD2static Word16 Lag_max (  Word32 corr[],  Word16 scal_sig[],  Word16 scal_fac,     Word16 scal_flag,      
    Word16 L_frame,     Word16 lag_max,   Word16 lag_min, Word16 *cor_max,  Word32 *rmax, Word32 *r0, Flag dtx  )
#elsestatic Word16 Lag_max ( vadState *vadSt, Word32 corr[],Word16 scal_sig[], Word16 scal_fac,  Word16 scal_flag, 
    Word16 L_frame,  Word16 lag_max,  Word16 lag_min, Word16 *cor_max,   Flag dtx   )
#endif{
	Word16 i, j,temp;
	Word16 *p;
	Word32 max, t0,t1,t2;
	Word16 max_h, max_l, ener_h, ener_l;
	Word16 p_max = 0; /* initialization only needed to keep gcc silent */

	Word16*pscal_sig = scal_sig;
	Word32 *pcorr = corr;
	
#ifndef VAD2
	vadState *pvadSt = vadSt;
#endif
	max = MIN_32;              
	p_max = lag_max;           

	for (i = lag_max, j = (PIT_MAX-lag_max-1); i >= lag_min; i--, j--)  
	{
		p_max = ( pcorr[-i] >= max)  ? i : p_max;
		max     = ( pcorr[-i] >= max) ? pcorr[-i] : max;
	}
    
    /* compute energy */
	t0 = 0;                    
	p = &pscal_sig[-p_max];      
	for (i = 0; i < L_frame; i+=8, p+=8)
	{
		t1 = (p[0]*p[0] + p[1]*p[1] + p[2]*p[2] + p[3]*p[3]);
		t2 = (p[4]*p[4] + p[5]*p[5] + p[6]*p[6] + p[7]*p[7]);
		t0 += (t1+t2)<<1; 
	}
    /* 1/sqrt(energy) */

    if (dtx)
    { 

	 /* no test() call since this if is only in simulation env */
#ifdef VAD2
       *rmax = max;	
       *r0 = t0;		
#else
	/* check tone */
	//  vad_tone_detection (vadSt, max, t0);
	temp = (t0 + 0x00008000) >> 16 ;


	if ((temp > 0) && (max > (temp*TONE_THR << 1)) )
	{
		pvadSt->tone = pvadSt->tone | 0x4000;           
	}
   
#endif
    }
    
	t0 = Inv_sqrt (t0); 
	t0 <<= (0!= scal_flag );

	/* max = max/sqrt(energy)  */  
	max_h = (Word16)(max >>16);
	max_l  = (max - (max_h <<16)) >>1;

	ener_h = (Word16)(t0 >>16);
	ener_l  = (t0 - (ener_h <<16)) >>1;
	t0 = (ener_h*max_h <<1) + (((max_h*ener_l >> 15) + (ener_h*max_l >>15))<<1);

	if (scal_flag)
	{
		t0 = scal_fac <0 ? t0<<(-scal_fac) : t0 >> scal_fac;
		*cor_max =(Word16)(t0 >>1 );  
	}
	else
	{
		*cor_max = (Word16)t0;
	}

	return (p_max);
}
/************************************************************************* * *  FUNCTION:  Pitch_ol * *  PURPOSE: Compute the open loop pitch lag. * *  DESCRIPTION: *      The open-loop pitch lag is determined based on the perceptually *      weighted speech signal. This is done in the following steps: *        - find three maxima of the correlation <sw[n],sw[n-T]>, *          dividing the search range into three parts: *               pit_min ... 2*pit_min-1 *             2*pit_min ... 4*pit_min-1 *             4*pit_min ...   pit_max *        - divide each maximum by <sw[n-t], sw[n-t]> where t is the delay at *          that maximum correlation. *        - select the delay of maximum normalized correlation (among the *          three candidates) while favoring the lower delay ranges. * *************************************************************************/Word16 Pitch_ol ( vadState *vadSt, Mode mode,  Word16 signal[],   Word16 pit_min, Word16 pit_max, Word16 L_frame, Word16 idx, Flag dtx )
{    Word16 i, j;
    Word16 max1, max2, max3;
    Word16 p_max1, p_max2, p_max3;
    Word16 scal_flag = 0;
    Word32 t0;
	
#ifdef VAD2
    Word32  r01, r02, r03;
    Word32  rmax1, rmax2, rmax3;
#else
    Word16 corr_hp_max;
#endif
    Word32 corr[PIT_MAX+1], *corr_ptr;
    
    /* Scaled signal */

    Word16 scaled_signal[L_FRAME + PIT_MAX];
    Word16 *scal_sig, scal_fac;

    vadState *pvadSt = vadSt;
    Word16 *psignal  = signal;
	

#ifndef VAD2
    if (dtx)
    {  /* no test() call since this if is only in simulation env */
       /* update tone detection */
      
       if ((mode == MR475) || (mode== MR515))
       {
         // vad_tone_detection_update (pvadSt, 1);          
	      pvadSt->tone >>= 2;            
	      pvadSt->tone = pvadSt->tone | 0x2000;          

       }
       else
       {
         // vad_tone_detection_update (vadSt, 0);
	   pvadSt->tone >>= 1 ;
       }
    }
#endif

    
    scal_sig = &scaled_signal[pit_max]; 

    t0 = 0L;                       
    for (i = -pit_max; i < L_frame; i++)
    {
        t0 +=  psignal[i]* psignal[i] << 1;
	 if(t0 < 0)
	 {
	    t0 = MAX_32; break;
	 }
	
    }
   
    /*--------------------------------------------------------*
     * Scaling of input signal.                               *
     *                                                        *
     *   if Overflow        -> scal_sig[i] = signal[i]>>3     *
     *   else if t0 < 1^20  -> scal_sig[i] = signal[i]<<3     *
     *   else               -> scal_sig[i] = signal[i]        *
     *--------------------------------------------------------*/

    /*--------------------------------------------------------*
     *  Verification for risk of overflow.                    *
     *--------------------------------------------------------*/

   
    if (t0 == MAX_32 )               /* Test for overflow */
    {
        for (i = -pit_max; i < L_frame; i++)
        {
            scal_sig[i] = psignal[i]>> 3;  
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -