att_pc2lsf.c
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:11k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /*
  2. This software module was originally developed by
  3. P. Kabal (McGill University)
  4. Peter Kroon (Bell Laboratories, Lucent Technologies)
  5. in the course of development of the MPEG-2 NBC/MPEG-4 Audio standard
  6. ISO/IEC 13818-7, 14496-1,2 and 3. This software module is an
  7. implementation of a part of one or more MPEG-2 NBC/MPEG-4 Audio tools
  8. as specified by the MPEG-2 NBC/MPEG-4 Audio standard. ISO/IEC gives
  9. users of the MPEG-2 NBC/MPEG-4 Audio standards free license to this
  10. software module or modifications thereof for use in hardware or
  11. software products claiming conformance to the MPEG-2 NBC/ MPEG-4 Audio
  12. standards. Those intending to use this software module in hardware or
  13. software products are advised that this use may infringe existing
  14. patents. The original developer of this software module and his/her
  15. company, the subsequent editors and their companies, and ISO/IEC have
  16. no liability for use of this software module or modifications thereof
  17. in an implementation. Copyright is not released for non MPEG-2
  18. NBC/MPEG-4 Audio conforming products. The original developer retains
  19. full right to use the code for his/her own purpose, assign or donate
  20. the code to a third party and to inhibit third party from using the
  21. code for non MPEG-2 NBC/MPEG-4 Audio conforming products. This
  22. copyright notice must be included in all copies or derivative works.
  23. Copyright (c) 1996.
  24.  */
  25. /**---------------------------------------------------------------------------- Telecommunications & Signal Processing Lab ---------------
  26.  *                            McGill University
  27.  * Author / revision:
  28.  *  P. Kabal
  29.  *  $Revision: 1.2 $  $Date: 2002/05/13 15:52:06 $
  30.  *  Revised for speechlib library 8/15/94
  31.  */
  32. #include <math.h>
  33. #include <stdio.h>
  34. #include "att_proto.h"
  35. #define MAXORDER 20
  36. #ifndef PI
  37. #define PI 3.1415927
  38. #endif
  39. #define NBIS 4 /* number of bisections */
  40. #define DW (0.01 * PI) /* resolution for initial search */
  41. /*----------------------------------------------------------------------------
  42.  * pc2lsf -  Convert predictor coefficients to line spectral frequencies
  43.  *
  44.  * Description:
  45.  *  The transfer function of the prediction error filter is formed from the
  46.  *  predictor coefficients.  This polynomial is transformed into two reciprocal
  47.  *  polynomials having roots on the unit circle. The roots of these polynomials
  48.  *  interlace.  It is these roots that determine the line spectral frequencies.
  49.  *  The two reciprocal polynomials are expressed as series expansions in
  50.  *  Chebyshev polynomials with roots in the range -1 to +1.  The inverse cosine
  51.  *  of the roots of the Chebyshev polynomial expansion gives the line spectral
  52.  *  frequencies.  If np line spectral frequencies are not found, this routine
  53.  *  stops with an error message.  This error occurs if the input coefficients
  54.  *  do not give a prediction error filter with minimum phase.
  55.  *
  56.  *  Line spectral frequencies and predictor coefficients are usually expressed
  57.  *  algebraically as vectors.
  58.  *    lsf[0]     first (lowest frequency) line spectral frequency
  59.  *    lsf[i]     1 <= i < np
  60.  *    pc[0]=1.0  predictor coefficient corresponding to lag 0
  61.  *    pc[i]      1 <= 1 <= np
  62.  *
  63.  * Parameters:
  64.  *  ->  float pc[]
  65.  *      Vector of predictor coefficients (Np+1 values).  These are the 
  66.  *      coefficients of the predictor filter, with pc[0] being the predictor 
  67.  *      coefficient corresponding to lag 0, and pc[Np] corresponding to lag Np.
  68.  *      The predictor coeffs. must correspond to a minimum phase prediction
  69.  *      error filter.
  70.  *  <-  float lsf[]
  71.  *      Array of Np line spectral frequencies (in ascending order).  Each line
  72.  *      spectral frequency lies in the range 0 to pi.
  73.  *  ->  long Np
  74.  *      Number of coefficients (at most MAXORDER = 50)
  75.  *----------------------------------------------------------------------------
  76.  */
  77. long pc2lsf( /* ret 1 on succ, 0 on failure */
  78. float lsf[], /* output: the np line spectral freq. */
  79. const float pc[], /* input : the np+1 predictor coeff. */
  80. long np /* input : order = # of freq to cal. */
  81. )
  82. {
  83.   float fa[(MAXORDER+1)/2+1], fb[(MAXORDER+1)/2+1];
  84.   float ta[(MAXORDER+1)/2+1], tb[(MAXORDER+1)/2+1];
  85.   float *t;
  86.   float xlow, xmid, xhigh;
  87.   float ylow, ymid, yhigh;
  88.   float xroot;
  89.   float dx;
  90.   long i, j, nf;
  91.   long odd;
  92.   long na, nb, n;
  93.   float ss, aa;
  94.   testbound(np, MAXORDER, "pc2lsf.c");
  95. /* Determine the number of coefficients in ta(.) and tb(.) */
  96.   odd = (np % 2 != 0);
  97.   if (odd) {
  98.     nb = (np + 1) / 2;
  99.     na = nb + 1;
  100.   }
  101.   else {
  102.     nb = np / 2 + 1;
  103.     na = nb;
  104.   }
  105. /*
  106. *   Let D=z**(-1), the unit delay; the predictor coefficients form the
  107. *
  108. *             N         n 
  109. *     P(D) = SUM pc[n] D  .   ( pc[0] = 1.0 )
  110. *            n=0
  111. *
  112. *   error filter polynomial, A(D)=P(D) with N+1 terms.  Two auxiliary
  113. *   polynomials are formed from the error filter polynomial,
  114. *     Fa(D) = A(D) + D**(N+1) A(D**(-1))  (N+2 terms, symmetric)
  115. *     Fb(D) = A(D) - D**(N+1) A(D**(-1))  (N+2 terms, anti-symmetric)
  116. */
  117.   fa[0] = 1.0;
  118.   for (i = 1, j = np; i < na; ++i, --j)
  119.     fa[i] = pc[i] + pc[j];
  120.   fb[0] = 1.0;
  121.   for (i = 1, j = np; i < nb; ++i, --j)
  122.     fb[i] = pc[i] - pc[j];
  123. /*
  124. * N even,  Fa(D)  includes a factor 1+D,
  125. *          Fb(D)  includes a factor 1-D
  126. * N odd,   Fb(D)  includes a factor 1-D**2
  127. * Divide out these factors, leaving even order symmetric polynomials, M is the
  128. * total number of terms and Nc is the number of unique terms,
  129. *
  130. *   N       polynomial            M         Nc=(M+1)/2
  131. * even,  Ga(D) = F1(D)/(1+D)     N+1          N/2+1
  132. *        Gb(D) = F2(D)/(1-D)     N+1          N/2+1
  133. * odd,   Ga(D) = F1(D)           N+2        (N+1)/2+1
  134. *        Gb(D) = F2(D)/(1-D**2)   N          (N+1)/2
  135. */
  136.   if (odd) {
  137.     for (i = 2; i < nb; ++i)
  138.       fb[i] = fb[i] + fb[i-2];
  139.   }
  140.   else {
  141.     for (i = 1; i < na; ++i) {
  142.       fa[i] = fa[i] - fa[i-1];
  143.       fb[i] = fb[i] + fb[i-1];
  144.     }
  145.   }
  146. /*
  147. *   To look for roots on the unit circle, Ga(D) and Gb(D) are evaluated for
  148. *   D=exp(jw).  Since Gz(D) and Gb(D) are symmetric, they can be expressed in
  149. *   terms of a series in cos(nw) for D on the unit circle.  Since M is odd and
  150. *   D=exp(jw)
  151. *
  152. *           M-1        n 
  153. *   Ga(D) = SUM fa(n) D             (symmetric, fa(n) = fa(M-1-n))
  154. *           n=0
  155. *                                    Mh-1
  156. *         = exp(j Mh w) [ f1(Mh) + 2 SUM fa(n) cos((Mh-n)w) ]
  157. *                                    n=0
  158. *                       Mh
  159. *         = exp(j Mh w) SUM ta(n) cos(nw) ,
  160. *                       n=0
  161. *
  162. *   where Mh=(M-1)/2=Nc-1.  The Nc=Mh+1 coefficients ta(n) are defined as
  163. *
  164. *   ta(n) =   fa(Nc-1) ,    n=0,
  165. *         = 2 fa(Nc-1-n) ,  n=1,...,Nc-1.
  166. *   The next step is to identify cos(nw) with the Chebyshev polynomial T(n,x).
  167. *   The Chebyshev polynomials satisfy the relationship T(n,cos(w)) = cos(nw).
  168. *   Omitting the exponential delay term, the series expansion in terms of
  169. *   Chebyshev polynomials is
  170. *
  171. *           Nc-1
  172. *   Ta(x) = SUM ta(n) T(n,x)
  173. *           n=0
  174. *
  175. *   The domain of Ta(x) is -1 < x < +1.  For a given root of Ta(x), say x0,
  176. *   the corresponding position of the root of Fa(D) on the unit circle is
  177. *   exp(j arccos(x0)).
  178. */
  179.   ta[0] = fa[na-1];
  180.   for (i = 1, j = na - 2; i < na; ++i, --j)
  181.     ta[i] = 2.0 * fa[j];
  182.   tb[0] = fb[nb-1];
  183.   for (i = 1, j = nb - 2; i < nb; ++i, --j)
  184.     tb[i] = 2.0 * fb[j];
  185. /*
  186. *   To find the roots, we sample the polynomials Ta(x) and Tb(x) looking for
  187. *   sign changes.  An interval containing a root is successively bisected to
  188. *   narrow the interval and then linear interpolation is used to estimate the
  189. *   root.  For a given root at x0, the line spectral frequency is w0=acos(x0).
  190. *
  191. *   Since the roots of the two polynomials interlace, the search for roots
  192. *   alternates between the polynomials Ta(x) and Tb(x).  The sampling interval
  193. *   must be small enough to avoid having two cancelling sign changes in the
  194. *   same interval.  Consider specifying the resolution in the LSF domain.  For
  195. *   an interval [xl, xh], the corresponding interval in frequency is [w1, w2],
  196. *   with xh=cos(w1) and xl=cos(w2) (note the reversal in order).  Let dw=w2-w1,
  197. *     dx = xh-xl = xh [1-cos(dw)] + sqrt(1-xh*xh) sin(dw).
  198. *   However, the calculation of the square root is overly time-consuming.  If
  199. *   instead, we use equal steps in the x-domain, the resolution in the LSF
  200. *   domain is best at at pi/2 and worst at 0 and pi.  As a compromise we fit a
  201. *   quadratic to the step size relationship.  At x=1, dx=(1-cos(dw); at x=0,
  202. *   dx=sin(dw).  Then the approximation is
  203. *     dx' = (a(1-cos(dw))-sin(dw)) x**2 + sin(dw)).
  204. *   For a=1, this value underestimates the step size in the range of interest.
  205. *   However, the step size for just below x=1 and just above x=-1 fall well
  206. *   below the desired step sizes.  To compensate for this, we use a=4.  Then at
  207. *   x=+1 and x=-1, the step sizes are too large by a factor of 4, but rapidly
  208. *   fall to about 60% of the desired values and then rise slowly to become 
  209. *   equal to the desired step size at x=0.
  210. */
  211.   nf = 0;
  212.   t = ta;
  213.   n = na;
  214.   xroot = 2.0;
  215.   xlow = 1.0;
  216.   ylow = FNevChebP(xlow, t, n);
  217. /*
  218. *   Define the step-size function parameters
  219. *   The resolution in the LSF domain is at least DW/2**NBIS, not counting the
  220. *   increase in resolution due to the linear interpolation step.  For
  221. *   DW=0.02*Pi, and NBIS=4, and a sampling frequency of 8000, this corresponds
  222. *   to 5 Hz.
  223. */
  224.   ss = sin (DW);
  225.   aa = 4.0 - 4.0 * cos (DW)  - ss;
  226. /* Root search loop */
  227.   while (xlow > -1.0 && nf < np) {
  228.     /* New trial point */
  229.     xhigh = xlow;
  230.     yhigh = ylow;
  231.     dx = aa * xhigh * xhigh + ss;
  232.     xlow = xhigh - dx;
  233.     if (xlow < -1.0)
  234.       xlow = -1.0;
  235.     ylow = FNevChebP(xlow, t, n);
  236.     if (ylow * yhigh <= 0.0) {
  237.     /* Bisections of the interval containing a sign change */
  238.       dx = xhigh - xlow;
  239.       for (i = 1; i <= NBIS; ++i) {
  240. dx = 0.5 * dx;
  241. xmid = xlow + dx;
  242. ymid = FNevChebP(xmid, t, n);
  243. if (ylow * ymid <= 0.0) {
  244.   yhigh = ymid;
  245.   xhigh = xmid;
  246. }
  247. else {
  248.   ylow = ymid;
  249.   xlow = xmid;
  250. }
  251.       }
  252.       /*
  253.        * Linear interpolation in the subinterval with a sign change
  254.        * (take care if yhigh=ylow=0)
  255.        */
  256.       if (yhigh != ylow)
  257. xmid = xlow + dx * ylow / (ylow - yhigh);
  258.       else
  259. xmid = xlow + dx;
  260.       /* New root position */
  261.       lsf[nf] = acos((double) xmid);
  262.       ++nf;
  263.       /* Start the search for the roots of the next polynomial at the estimated
  264.        * location of the root just found.  We have to catch the case that the
  265.        * two polynomials have roots at the same place to avoid getting stuck at
  266.        * that root.
  267.        */
  268.       if (xmid >= xroot) {
  269. xmid = xlow - dx;
  270.       }
  271.       xroot = xmid;
  272.       if (t == ta) {
  273. t = tb;
  274. n = nb;
  275.       }
  276.       else {
  277. t = ta;
  278. n = na;
  279.       }
  280.       xlow = xmid;
  281.       ylow = FNevChebP(xlow, t, n);
  282.     }
  283.   }
  284. /* Error if np roots have not been found */
  285.   if (nf != np) {
  286.     printf("nWARNING: pc2lsf failed to find all lsf nf=%ld np=%ldn", nf, np);
  287.     return(0);
  288.   }
  289.   return(1);
  290. }