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

📄 autocorr.c

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

/*******************************************************************************
**      File                  : autocorr.c
*      Purpose           : auto -correations , filter etc. compution
        Date                 : 2006 -01-18   create
**********************************************************************************/
#include "autocorr.h"#include "basic_op.h"

                  /**---- -------------  static local variables declare------------------ **/
#define NC   M/2            
#define grid_points 60
static const Word16 grid[grid_points + 1] ={    32760, 32723, 32588, 32364, 32051, 31651,    31164, 30591, 29935, 29196, 28377, 27481,    26509, 25465, 24351, 23170, 21926, 20621,    19260, 17846, 16384, 14876, 13327, 11743,    10125, 8480, 6812, 5126, 3425, 1714,    0, -1714, -3425, -5126, -6812, -8480,    -10125, -11743, -13327, -14876, -16384, -17846,    -19260, -20621, -21926, -23170, -24351, -25465,    -26509, -27481, -28377, -29196, -29935, -30591,    -31164, -31651, -32051, -32364, -32588, -32723,    -32760};

static const Word16 lag_h[10] ={    32728,    32619,    32438,    32187,    31867,    31480,    31029,    30517,    29946,    29321};static const Word16 lag_l[10] ={    11904,    17280,    30720,    25856,    24192,    28992,    24384,    7360,    19520,    14784};


/***************************************************************************
    Function    : autocorr    
    Purpose     : Compute autocorrelations of signal with windowing

    Input          :  x[]       : Input signal (L_WINDOW) 
                          m         : LPC order 
                          wind[] : window for LPC analysis (L_WINDOW)
    Output        :   
                            r_h[]  : Autocorrelations  (msb)
                            r_l[]   : Autocorrelations  (lsb)
    Return        :
****************************************************************************/
Word16 Autocorr (Word16 x[],  Word16 m,  Word16 r_h[],  Word16 r_l[],  const Word16 wind[] )
{

    Word16 i, j, k, norm, temp ;
    Word16 y[L_WINDOW];    Word32 sum,tmp;
    Word16 overfl, overfl_shft;
    const Word16* pwind,*plag_h, *plag_l; 
    Word16 *pr_h, *pr_l,*px;

    pwind = wind; px = x; pr_h = r_h;pr_l =r_l;
    plag_h = lag_h; plag_l = lag_l;	
	
    /* Windowing of signal */
    for (i = 0; i < L_WINDOW; i+=4 )
    {		y[i] =  (px[i]* pwind[i] + 0x00004000) >>15; 
		y[i+1] =  (px[i+1]* pwind[i+1] + 0x00004000) >>15; 
		y[i+2] =  (px[i+2]* pwind[i+2] + 0x00004000) >>15; 
		y[i+3] =  (px[i+3]* pwind[i+3] + 0x00004000) >>15; 
    }    /* Compute r[0] and test for overflow */    overfl_shft = 0;                 
    j = 0;
    do 
    {
        overfl = 0;    
        sum = 0;
        for (i = 0; i < L_WINDOW; i++)
        {
            temp = y[i]>>(j*2);
	     sum += temp*temp<<1;
	     if( sum < 0)
	     	{
		     overfl = 1; j++;
		     break;		 }		        }
		
    }while(overfl != 0);

  if(j != 0)
 {
     tmp = j<<1;
    for (i = 0; i < L_WINDOW; i++)
     {
          y[i] >>= tmp;
      }
	
   overfl_shft +=  4*j ;
  }
   
    sum ++; 
    /* Normalization of r[0] */
    norm = norm_l (sum);    
    sum = (sum<< norm);
    pr_h[0] = (Word16)(sum>>16);
    pr_l[0] =  (Word16)(( sum  - (pr_h[0]<<16))>>1);
	
    for (i = 1; i <= M; i++)
    {
       k = L_WINDOW - i;
	sum = 0;          
	for (j = 0; j < k; j++)
	{
		sum += y[j]*y[j + i];		 
	}
	sum = (sum<< (norm+1));
	pr_h[i] = (Word16)(sum>>16);
	pr_l[i] =  (Word16)(( sum  - (pr_h[i]<<16))>>1);
	tmp = ((pr_h[i]*plag_h[i-1])<<1) + ((((pr_h[i]*plag_l[i - 1])>>15) + ((pr_l[i]*plag_h[i-1])>>15))<<1);         		 	
	pr_h[i] = (Word16)(tmp>>16);    
	pr_l[i] =  (Word16)(( tmp  - (pr_h[i]<<16))>>1);
		    	}
	
     norm -=  overfl_shft ;    return norm;
}


/***************************************************************************
  Function     : Az_lsp
  Purpose     : Compute the LSPs from  the LP coefficients
  Input          :
                       a[]              : predictor coefficients (MP1)   
                       old_lsp[]    : old lsp[] (in case not found 10 roots) (M) 
  Output :
                       lsp[]           : line spectral pairs (M)                  
   
  Return:
**************************************************************************/
void Az_lsp (Word16 a[],   Word16 lsp[],Word16 old_lsp[] )
{
	Word16 i, j,k,nf, ip;
	Word16 xlow, ylow, xhigh, yhigh, xmid, ymid, xint;
	Word16 x, y, sign, exp,temp;
	Word16 *coef;
	Word16 f1[6], f2[6];
	Word16 b0_h, b0_l, b1_h, b1_l, b2_h, b2_l;
	Word32 t0,t1;

	Word32 L_num,L_denom;

	Word16 *pa, *plsp, *pold_lsp;
	const Word16 *pgrid;
	pa = a;plsp =lsp;
	pold_lsp = old_lsp; 
	pgrid =grid;



	f1[0] = 1024;                 /* f1[0] = 1.0 */
	f2[0] = 1024;                  /* f2[0] = 1.0 */

	t0 = (pa[10] + pa[1] );           t1 = (pa[1] -  pa[M]);  	
	f1[1] =  ((t0>>2)- f1[0]);	   f2[1] = ((t1>>2)+ f2[0]);	


	t0 = (pa[9] + pa[2] );       t1 = (pa[2] -  pa[9]);  	
	f1[2] =  ((t0>>2)- f1[1]);	   f2[2] = ((t1>>2)+ f2[1]);	

	t0 = (pa[8] + pa[3] );       t1 = (pa[3] -  pa[8]);  	
	f1[3] =  ((t0>>2)- f1[2]);	   f2[3] = ((t1>>2)+ f2[2]);

	t0 = (pa[7] + pa[4] );       t1 = (pa[4] -  pa[7]);  	
	f1[4] =  ((t0>>2)- f1[3]);	   f2[4] = ((t1>>2)+ f2[3]);

	t0 = (pa[6] + pa[5] );       t1 = (pa[5] -  pa[6]);  	
	f1[5] =  ((t0>>2)- f1[4]);	   f2[5] = ((t1>>2)+ f2[4]);	


	/*-------------------------------------------------------------*
	* find the LSPs using the Chebychev pol. evaluation           *
	*-------------------------------------------------------------*/

	nf = 0;                      /* number of found frequencies */
	ip = 0;                       /* indicator for f1 or f2      */

	coef = f1; 

	xlow = 32760;             
	//ylow = Chebps (xlow, coef, NC);
	b2_h = 256;                   
	b2_l = 0;                  
	t0 =  (xlow<<10);         
	t0 +=  coef[1]<<14 ;  
	b1_h =  (Word16)(t0>>16);
	b1_l  = (t0 - (b1_h<<16))>>1;

	t0   =   (b1_h*xlow +( b1_l*xlow>>15 ))<< 2 ;
	t0  -=  (b2_h<<16) + (b2_l<<1);
	t0  +=  coef[2]<<14;
	b0_h  =  (Word16)(t0 >>16);
	b0_l   =  (t0 - (b0_h<<16))>>1;
	b2_l   =  b1_l;              
	b2_h  =  b1_h;             
	b1_l   =  b0_l;              
	b1_h  = b0_h; 

	t0   =   (b1_h*xlow +( b1_l*xlow>>15 ))<< 2 ;
	t0  -=  (b2_h<<16) + (b2_l<<1);
	t0  +=  coef[3]<<14;
	b0_h  =  (Word16)(t0 >>16);
	b0_l   =  (t0 - (b0_h<<16))>>1;
	b2_l   =  b1_l;              
	b2_h  =  b1_h;             
	b1_l   =  b0_l;              
	b1_h  = b0_h; 

	t0   =   (b1_h*xlow +( b1_l*xlow >>15 ))<< 2 ;
	t0  -=  (b2_h<<16) + (b2_l<<1);
	t0  +=  coef[4]<<14;
	b0_h  =  (Word16)(t0 >>16);
	b0_l   =  (t0 - (b0_h<<16))>>1;
	b2_l   =  b1_l;              
	b2_h  =  b1_h;             
	b1_l   =  b0_l;              
	b1_h  = b0_h; 

	t0   = (b1_h*xlow+ ( b1_l*xlow >>15 ))<<1;
	t0 -= (b2_h<<16) + (b2_l<<1);
	t0  += coef[5] << 13;
	t0 =  ( t0 > (Word32)0x1ffffff) ? MAX_32: (t0 < (Word32)0xfe000000 ? MIN_32: t0 << 6);

	ylow = (Word16)(t0 >>16);
	j = 0;

	/* while ( (nf < M) && (j < grid_points) ) */
	while ((nf < M ) && (j < grid_points ))
	{
		j++;
		xhigh = xlow;            
		yhigh = ylow;            
		xlow = pgrid[j];    

		//     ylow = Chebps (xlow, coef, NC);

		b2_h = 256;                   
		b2_l = 0;                  
		t0 =  (xlow<<10);         
		t0 +=  coef[1]<<14 ;  
		b1_h = (Word16)(t0>>16);
		b1_l  = (t0 - (b1_h<<16))>>1;

		t0   =   (b1_h*xlow +( b1_l*xlow>>15 ))<< 2 ;
		t0  -=  (b2_h<<16) + (b2_l<<1);
		t0  +=  coef[2]<<14;
		b0_h  =  (Word16)(t0 >>16);
		b0_l   =  (t0 - (b0_h<<16))>>1;
		b2_l   =  b1_l;              
		b2_h  =  b1_h;             
		b1_l   =  b0_l;              
		b1_h  = b0_h; 

		t0   =   (b1_h*xlow +( b1_l*xlow>>15 ))<< 2 ;
		t0  -=  (b2_h<<16) + (b2_l<<1);
		t0  +=  coef[3]<<14;
		b0_h  =  (Word16)(t0 >>16);
		b0_l   =  (t0 - (b0_h<<16))>>1;
		b2_l   =  b1_l;              
		b2_h  =  b1_h;             
		b1_l   =  b0_l;              
		b1_h  = b0_h; 

		t0   =   (b1_h*xlow +( b1_l*xlow >>15 ))<< 2 ;
		t0  -=  (b2_h<<16) + (b2_l<<1);
		t0  +=  coef[4]<<14;
		b0_h  =  (Word16)(t0 >>16);
		b0_l   =  (t0 - (b0_h<<16))>>1;
		b2_l   =  b1_l;              
		b2_h  =  b1_h;             
		b1_l   =  b0_l;              
		b1_h  = b0_h; 

		t0   = (b1_h*xlow+ ( b1_l*xlow >>15 ))<<1;
		t0 -= (b2_h<<16) + (b2_l<<1);
		t0  += coef[5] << 13;
		t0 =  ( t0 > (Word32)0x1ffffff) ? MAX_32: (t0 < (Word32)0xfe000000 ? MIN_32: t0 << 6);

		ylow = (Word16)(t0 >>16);

		if ( (ylow* yhigh ) <= (Word32) 0L)
		{

			/* divide 4 times the interval */

			for (i = 0; i < 4; i++)
			{
				/* xmid = (xlow + xhigh)/2 */
				xmid = (xlow>>1)+(xhigh>> 1);

				//ymid = Chebps (xmid, coef, NC);
				b2_h = 256;                   
				b2_l = 0;                  
				t0 =  (xmid<<10);         
				t0 +=  coef[1]<<14 ;  
				b1_h = (Word16)(t0>>16);
				b1_l  = (t0 - (b1_h<<16))>>1;

				t0   =   (b1_h*xmid +( b1_l*xmid>>15 ))<< 2 ;
				t0  -=  (b2_h<<16) + (b2_l<<1);
				t0  +=  coef[2]<<14;
				b0_h  =  (Word16)(t0 >>16);
				b0_l   =  (t0 - (b0_h<<16))>>1;
				b2_l   =  b1_l;              
				b2_h  =  b1_h;             
				b1_l   =  b0_l;              
				b1_h  = b0_h; 

				t0   =   (b1_h*xmid +( b1_l*xmid>>15 ))<< 2 ;
				t0  -=  (b2_h<<16) + (b2_l<<1);
				t0  +=  coef[3]<<14;
				b0_h  =  (Word16)(t0 >>16);
				b0_l   =  (t0 - (b0_h<<16))>>1;
				b2_l   =  b1_l;              
				b2_h  =  b1_h;             
				b1_l   =  b0_l;              

⌨️ 快捷键说明

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