LPCFUNC.C
上传用户:meifeng08
上传日期:2013-06-18
资源大小:5304k
文件大小:10k
源码类别:

语音压缩

开发平台:

C/C++

  1. /*
  2.    ITU-T G.729A Speech Coder    ANSI-C Source Code
  3.    Version 1.1    Last modified: September 1996
  4.    Copyright (c) 1996,
  5.    AT&T, France Telecom, NTT, Universite de Sherbrooke
  6.    All rights reserved.
  7. */
  8. /*-------------------------------------------------------------*
  9.  *  Procedure Lsp_Az:                                          *
  10.  *            ~~~~~~                                           *
  11.  *   Compute the LPC coefficients from lsp (order=10)          *
  12.  *-------------------------------------------------------------*/
  13. #include "typedef.h"
  14. #include "basic_op.h"
  15. #include "oper_32b.h"
  16. #include "ld8a.h"
  17. #include "tab_ld8a.h"
  18. /* local function */
  19. static void Get_lsp_pol(Word16 *lsp, Word32 *f);
  20. void Lsp_Az(
  21.   Word16 lsp[],    /* (i) Q15 : line spectral frequencies            */
  22.   Word16 a[]       /* (o) Q12 : predictor coefficients (order = 10)  */
  23. )
  24. {
  25.   Word16 i, j;
  26.   Word32 f1[6], f2[6];
  27.   Word32 t0;
  28.   Get_lsp_pol(&lsp[0],f1);
  29.   Get_lsp_pol(&lsp[1],f2);
  30.   for (i = 5; i > 0; i--)
  31.   {
  32.     f1[i] = L_add(f1[i], f1[i-1]);        /* f1[i] += f1[i-1]; */
  33.     f2[i] = L_sub(f2[i], f2[i-1]);        /* f2[i] -= f2[i-1]; */
  34.   }
  35.   a[0] = 4096;
  36.   for (i = 1, j = 10; i <= 5; i++, j--)
  37.   {
  38.     t0   = L_add(f1[i], f2[i]);                 /* f1[i] + f2[i]             */
  39.     a[i] = extract_l( L_shr_r(t0, 13) );        /* from Q24 to Q12 and * 0.5 */
  40.     t0   = L_sub(f1[i], f2[i]);                 /* f1[i] - f2[i]             */
  41.     a[j] = extract_l( L_shr_r(t0, 13) );        /* from Q24 to Q12 and * 0.5 */
  42.   }
  43.   return;
  44. }
  45. /*-----------------------------------------------------------*
  46.  * procedure Get_lsp_pol:                                    *
  47.  *           ~~~~~~~~~~~                                     *
  48.  *   Find the polynomial F1(z) or F2(z) from the LSPs        *
  49.  *-----------------------------------------------------------*
  50.  *                                                           *
  51.  * Parameters:                                               *
  52.  *  lsp[]   : line spectral freq. (cosine domain)    in Q15  *
  53.  *  f[]     : the coefficients of F1 or F2           in Q24  *
  54.  *-----------------------------------------------------------*/
  55. static void Get_lsp_pol(Word16 *lsp, Word32 *f)
  56. {
  57.   Word16 i,j, hi, lo;
  58.   Word32 t0;
  59.    /* All computation in Q24 */
  60.    *f = L_mult(4096, 2048);             /* f[0] = 1.0;             in Q24  */
  61.    f++;
  62.    *f = L_msu((Word32)0, *lsp, 512);    /* f[1] =  -2.0 * lsp[0];  in Q24  */
  63.    f++;
  64.    lsp += 2;                            /* Advance lsp pointer             */
  65.    for(i=2; i<=5; i++)
  66.    {
  67.      *f = f[-2];
  68.      for(j=1; j<i; j++, f--)
  69.      {
  70.        L_Extract(f[-1] ,&hi, &lo);
  71.        t0 = Mpy_32_16(hi, lo, *lsp);         /* t0 = f[-1] * lsp    */
  72.        t0 = L_shl(t0, 1);
  73.        *f = L_add(*f, f[-2]);                /* *f += f[-2]         */
  74.        *f = L_sub(*f, t0);                   /* *f -= t0            */
  75.      }
  76.      *f   = L_msu(*f, *lsp, 512);            /* *f -= lsp<<9        */
  77.      f   += i;                               /* Advance f pointer   */
  78.      lsp += 2;                               /* Advance lsp pointer */
  79.    }
  80.    return;
  81. }
  82. /*___________________________________________________________________________
  83.  |                                                                           |
  84.  |   Functions : Lsp_lsf and Lsf_lsp                                         |
  85.  |                                                                           |
  86.  |      Lsp_lsf   Transformation lsp to lsf                                  |
  87.  |      Lsf_lsp   Transformation lsf to lsp                                  |
  88.  |---------------------------------------------------------------------------|
  89.  |  Algorithm:                                                               |
  90.  |                                                                           |
  91.  |   The transformation from lsp[i] to lsf[i] and lsf[i] to lsp[i] are       |
  92.  |   approximated by a look-up table and interpolation.                      |
  93.  |___________________________________________________________________________|
  94. */
  95. void Lsf_lsp(
  96.   Word16 lsf[],    /* (i) Q15 : lsf[m] normalized (range: 0.0<=val<=0.5) */
  97.   Word16 lsp[],    /* (o) Q15 : lsp[m] (range: -1<=val<1)                */
  98.   Word16 m         /* (i)     : LPC order                                */
  99. )
  100. {
  101.   Word16 i, ind, offset;
  102.   Word32 L_tmp;
  103.   for(i=0; i<m; i++)
  104.   {
  105.     ind    = shr(lsf[i], 8);               /* ind    = b8-b15 of lsf[i] */
  106.     offset = lsf[i] & (Word16)0x00ff;      /* offset = b0-b7  of lsf[i] */
  107.     /* lsp[i] = table[ind]+ ((table[ind+1]-table[ind])*offset) / 256 */
  108.     L_tmp   = L_mult(sub(table[ind+1], table[ind]), offset);
  109.     lsp[i] = add(table[ind], extract_l(L_shr(L_tmp, 9)));
  110.   }
  111.   return;
  112. }
  113. void Lsp_lsf(
  114.   Word16 lsp[],    /* (i) Q15 : lsp[m] (range: -1<=val<1)                */
  115.   Word16 lsf[],    /* (o) Q15 : lsf[m] normalized (range: 0.0<=val<=0.5) */
  116.   Word16 m         /* (i)     : LPC order                                */
  117. )
  118. {
  119.   Word16 i, ind, tmp;
  120.   Word32 L_tmp;
  121.   ind = 63;    /* begin at end of table -1 */
  122.   for(i= m-(Word16)1; i >= 0; i--)
  123.   {
  124.     /* find value in table that is just greater than lsp[i] */
  125.     while( sub(table[ind], lsp[i]) < 0 )
  126.     {
  127.       ind = sub(ind,1);
  128.     }
  129.     /* acos(lsp[i])= ind*256 + ( ( lsp[i]-table[ind] ) * slope[ind] )/4096 */
  130.     L_tmp  = L_mult( sub(lsp[i], table[ind]) , slope[ind] );
  131.     tmp = round(L_shl(L_tmp, 3));     /*(lsp[i]-table[ind])*slope[ind])>>12*/
  132.     lsf[i] = add(tmp, shl(ind, 8));
  133.   }
  134.   return;
  135. }
  136. /*___________________________________________________________________________
  137.  |                                                                           |
  138.  |   Functions : Lsp_lsf and Lsf_lsp                                         |
  139.  |                                                                           |
  140.  |      Lsp_lsf   Transformation lsp to lsf                                  |
  141.  |      Lsf_lsp   Transformation lsf to lsp                                  |
  142.  |---------------------------------------------------------------------------|
  143.  |  Algorithm:                                                               |
  144.  |                                                                           |
  145.  |   The transformation from lsp[i] to lsf[i] and lsf[i] to lsp[i] are       |
  146.  |   approximated by a look-up table and interpolation.                      |
  147.  |___________________________________________________________________________|
  148. */
  149. void Lsf_lsp2(
  150.   Word16 lsf[],    /* (i) Q13 : lsf[m] (range: 0.0<=val<PI) */
  151.   Word16 lsp[],    /* (o) Q15 : lsp[m] (range: -1<=val<1)   */
  152.   Word16 m         /* (i)     : LPC order                   */
  153. )
  154. {
  155.   Word16 i, ind;
  156.   Word16 offset;   /* in Q8 */
  157.   Word16 freq;     /* normalized frequency in Q15 */
  158.   Word32 L_tmp;
  159.   for(i=0; i<m; i++)
  160.   {
  161. /*    freq = abs_s(freq);*/
  162.     freq = mult(lsf[i], 20861);          /* 20861: 1.0/(2.0*PI) in Q17 */
  163.     ind    = shr(freq, 8);               /* ind    = b8-b15 of freq */
  164.     offset = freq & (Word16)0x00ff;      /* offset = b0-b7  of freq */
  165.     if ( sub(ind, 63)>0 ){
  166.       ind = 63;                 /* 0 <= ind <= 63 */
  167.     }
  168.     /* lsp[i] = table2[ind]+ (slope_cos[ind]*offset >> 12) */
  169.     L_tmp   = L_mult(slope_cos[ind], offset);   /* L_tmp in Q28 */
  170.     lsp[i] = add(table2[ind], extract_l(L_shr(L_tmp, 13)));
  171.   }
  172.   return;
  173. }
  174. void Lsp_lsf2(
  175.   Word16 lsp[],    /* (i) Q15 : lsp[m] (range: -1<=val<1)   */
  176.   Word16 lsf[],    /* (o) Q13 : lsf[m] (range: 0.0<=val<PI) */
  177.   Word16 m         /* (i)     : LPC order                   */
  178. )
  179. {
  180.   Word16 i, ind;
  181.   Word16 offset;   /* in Q15 */
  182.   Word16 freq;     /* normalized frequency in Q16 */
  183.   Word32 L_tmp;
  184.   ind = 63;           /* begin at end of table2 -1 */
  185.   for(i= m-(Word16)1; i >= 0; i--)
  186.   {
  187.     /* find value in table2 that is just greater than lsp[i] */
  188.     while( sub(table2[ind], lsp[i]) < 0 )
  189.     {
  190.       ind = sub(ind,1);
  191.       if ( ind <= 0 )
  192.         break;
  193.     }
  194.     offset = sub(lsp[i], table2[ind]);
  195.     /* acos(lsp[i])= ind*512 + (slope_acos[ind]*offset >> 11) */
  196.     L_tmp  = L_mult( slope_acos[ind], offset );   /* L_tmp in Q28 */
  197.     freq = add(shl(ind, 9), extract_l(L_shr(L_tmp, 12)));
  198.     lsf[i] = mult(freq, 25736);           /* 25736: 2.0*PI in Q12 */
  199.   }
  200.   return;
  201. }
  202. /*-------------------------------------------------------------*
  203.  *  procedure Weight_Az                                        *
  204.  *            ~~~~~~~~~                                        *
  205.  * Weighting of LPC coefficients.                              *
  206.  *   ap[i]  =  a[i] * (gamma ** i)                             *
  207.  *                                                             *
  208.  *-------------------------------------------------------------*/
  209. void Weight_Az(
  210.   Word16 a[],      /* (i) Q12 : a[m+1]  LPC coefficients             */
  211.   Word16 gamma,    /* (i) Q15 : Spectral expansion factor.           */
  212.   Word16 m,        /* (i)     : LPC order.                           */
  213.   Word16 ap[]      /* (o) Q12 : Spectral expanded LPC coefficients   */
  214. )
  215. {
  216.   Word16 i, fac;
  217.   ap[0] = a[0];
  218.   fac   = gamma;
  219.   for(i=1; i<m; i++)
  220.   {
  221.     ap[i] = round( L_mult(a[i], fac) );
  222.     fac   = round( L_mult(fac, gamma) );
  223.   }
  224.   ap[m] = round( L_mult(a[m], fac) );
  225.   return;
  226. }
  227. /*----------------------------------------------------------------------*
  228.  * Function Int_qlpc()                                                  *
  229.  * ~~~~~~~~~~~~~~~~~~~                                                  *
  230.  * Interpolation of the LPC parameters.                                 *
  231.  *----------------------------------------------------------------------*/
  232. /* Interpolation of the quantized LSP's */
  233. void Int_qlpc(
  234.  Word16 lsp_old[], /* input : LSP vector of past frame              */
  235.  Word16 lsp_new[], /* input : LSP vector of present frame           */
  236.  Word16 Az[]       /* output: interpolated Az() for the 2 subframes */
  237. )
  238. {
  239.   Word16 i;
  240.   Word16 lsp[M];
  241.   /*  lsp[i] = lsp_new[i] * 0.5 + lsp_old[i] * 0.5 */
  242.   for (i = 0; i < M; i++) {
  243.     lsp[i] = add(shr(lsp_new[i], 1), shr(lsp_old[i], 1));
  244.   }
  245.   Lsp_Az(lsp, Az);              /* Subframe 1 */
  246.   Lsp_Az(lsp_new, &Az[MP1]);    /* Subframe 2 */
  247.   return;
  248. }