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

语音压缩

开发平台:

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. #include "typedef.h"
  9. #include "basic_op.h"
  10. #include "ld8a.h"
  11. #include "tab_ld8a.h"
  12. /*___________________________________________________________________________
  13.  |                                                                           |
  14.  |   Function Name : Pow2()                                                  |
  15.  |                                                                           |
  16.  |     L_x = pow(2.0, exponent.fraction)                                     |
  17.  |---------------------------------------------------------------------------|
  18.  |  Algorithm:                                                               |
  19.  |                                                                           |
  20.  |   The function Pow2(L_x) is approximated by a table and linear            |
  21.  |   interpolation.                                                          |
  22.  |                                                                           |
  23.  |   1- i = bit10-b15 of fraction,   0 <= i <= 31                            |
  24.  |   2- a = bit0-b9   of fraction                                            |
  25.  |   3- L_x = tabpow[i]<<16 - (tabpow[i] - tabpow[i+1]) * a * 2                 |
  26.  |   4- L_x = L_x >> (30-exponent)     (with rounding)                       |
  27.  |___________________________________________________________________________|
  28. */
  29. Word32 Pow2(        /* (o) Q0  : result       (range: 0<=val<=0x7fffffff) */
  30.   Word16 exponent,  /* (i) Q0  : Integer part.      (range: 0<=val<=30)   */
  31.   Word16 fraction   /* (i) Q15 : Fractional part.   (range: 0.0<=val<1.0) */
  32. )
  33. {
  34.   Word16 exp, i, a, tmp;
  35.   Word32 L_x;
  36.   L_x = L_mult(fraction, 32);           /* L_x = fraction<<6           */
  37.   i   = extract_h(L_x);                 /* Extract b10-b15 of fraction */
  38.   L_x = L_shr(L_x, 1);
  39.   a   = extract_l(L_x);                 /* Extract b0-b9   of fraction */
  40.   a   = a & (Word16)0x7fff;
  41.   L_x = L_deposit_h(tabpow[i]);         /* tabpow[i] << 16        */
  42.   tmp = sub(tabpow[i], tabpow[i+1]);    /* tabpow[i] - tabpow[i+1] */
  43.   L_x = L_msu(L_x, tmp, a);             /* L_x -= tmp*a*2        */
  44.   exp = sub(30, exponent);
  45.   L_x = L_shr_r(L_x, exp);
  46.   return(L_x);
  47. }
  48. /*___________________________________________________________________________
  49.  |                                                                           |
  50.  |   Function Name : Log2()                                                  |
  51.  |                                                                           |
  52.  |       Compute log2(L_x).                                                  |
  53.  |       L_x is positive.                                                    |
  54.  |                                                                           |
  55.  |       if L_x is negative or zero, result is 0.                            |
  56.  |---------------------------------------------------------------------------|
  57.  |  Algorithm:                                                               |
  58.  |                                                                           |
  59.  |   The function Log2(L_x) is approximated by a table and linear            |
  60.  |   interpolation.                                                          |
  61.  |                                                                           |
  62.  |   1- Normalization of L_x.                                                |
  63.  |   2- exponent = 30-exponent                                               |
  64.  |   3- i = bit25-b31 of L_x,    32 <= i <= 63  ->because of normalization.  |
  65.  |   4- a = bit10-b24                                                        |
  66.  |   5- i -=32                                                               |
  67.  |   6- fraction = tablog[i]<<16 - (tablog[i] - tablog[i+1]) * a * 2            |
  68.  |___________________________________________________________________________|
  69. */
  70. void Log2(
  71.   Word32 L_x,       /* (i) Q0 : input value                                 */
  72.   Word16 *exponent, /* (o) Q0 : Integer part of Log2.   (range: 0<=val<=30) */
  73.   Word16 *fraction  /* (o) Q15: Fractional  part of Log2. (range: 0<=val<1) */
  74. )
  75. {
  76.   Word16 exp, i, a, tmp;
  77.   Word32 L_y;
  78.   if( L_x <= (Word32)0 )
  79.   {
  80.     *exponent = 0;
  81.     *fraction = 0;
  82.     return;
  83.   }
  84.   exp = norm_l(L_x);
  85.   L_x = L_shl(L_x, exp );               /* L_x is normalized */
  86.   *exponent = sub(30, exp);
  87.   L_x = L_shr(L_x, 9);
  88.   i   = extract_h(L_x);                 /* Extract b25-b31 */
  89.   L_x = L_shr(L_x, 1);
  90.   a   = extract_l(L_x);                 /* Extract b10-b24 of fraction */
  91.   a   = a & (Word16)0x7fff;
  92.   i   = sub(i, 32);
  93.   L_y = L_deposit_h(tablog[i]);         /* tablog[i] << 16        */
  94.   tmp = sub(tablog[i], tablog[i+1]);    /* tablog[i] - tablog[i+1] */
  95.   L_y = L_msu(L_y, tmp, a);             /* L_y -= tmp*a*2        */
  96.   *fraction = extract_h( L_y);
  97.   return;
  98. }
  99. /*___________________________________________________________________________
  100.  |                                                                           |
  101.  |   Function Name : Inv_sqrt                                                |
  102.  |                                                                           |
  103.  |       Compute 1/sqrt(L_x).                                                |
  104.  |       L_x is positive.                                                    |
  105.  |                                                                           |
  106.  |       if L_x is negative or zero, result is 1 (3fff ffff).                |
  107.  |---------------------------------------------------------------------------|
  108.  |  Algorithm:                                                               |
  109.  |                                                                           |
  110.  |   The function 1/sqrt(L_x) is approximated by a table and linear          |
  111.  |   interpolation.                                                          |
  112.  |                                                                           |
  113.  |   1- Normalization of L_x.                                                |
  114.  |   2- If (30-exponent) is even then shift right once.                      |
  115.  |   3- exponent = (30-exponent)/2  +1                                       |
  116.  |   4- i = bit25-b31 of L_x,    16 <= i <= 63  ->because of normalization.  |
  117.  |   5- a = bit10-b24                                                        |
  118.  |   6- i -=16                                                               |
  119.  |   7- L_y = tabsqr[i]<<16 - (tabsqr[i] - tabsqr[i+1]) * a * 2                 |
  120.  |   8- L_y >>= exponent                                                     |
  121.  |___________________________________________________________________________|
  122. */
  123. Word32 Inv_sqrt(   /* (o) Q30 : output value   (range: 0<=val<1)           */
  124.   Word32 L_x       /* (i) Q0  : input value    (range: 0<=val<=7fffffff)   */
  125. )
  126. {
  127.   Word16 exp, i, a, tmp;
  128.   Word32 L_y;
  129.   if( L_x <= (Word32)0) return ( (Word32)0x3fffffffL);
  130.   exp = norm_l(L_x);
  131.   L_x = L_shl(L_x, exp );               /* L_x is normalize */
  132.   exp = sub(30, exp);
  133.   if( (exp & 1) == 0 )                  /* If exponent even -> shift right */
  134.       L_x = L_shr(L_x, 1);
  135.   exp = shr(exp, 1);
  136.   exp = add(exp, 1);
  137.   L_x = L_shr(L_x, 9);
  138.   i   = extract_h(L_x);                 /* Extract b25-b31 */
  139.   L_x = L_shr(L_x, 1);
  140.   a   = extract_l(L_x);                 /* Extract b10-b24 */
  141.   a   = a & (Word16)0x7fff;
  142.   i   = sub(i, 16);
  143.   L_y = L_deposit_h(tabsqr[i]);         /* tabsqr[i] << 16          */
  144.   tmp = sub(tabsqr[i], tabsqr[i+1]);    /* tabsqr[i] - tabsqr[i+1])  */
  145.   L_y = L_msu(L_y, tmp, a);             /* L_y -=  tmp*a*2         */
  146.   L_y = L_shr(L_y, exp);                /* denormalization */
  147.   return(L_y);
  148. }