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

语音压缩

开发平台:

C/C++

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