lsp.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: LSP (also called LSF) conversion routines
  14. last mod: $Id: lsp.c,v 1.9.2.3 2000/09/02 09:39:19 xiphmont Exp $
  15. The LSP generation code is taken (with minimal modification) from
  16. "On the Computation of the LSP Frequencies" by Joseph Rothweiler
  17. <rothwlr@altavista.net>, available at:
  18. http://www2.xtdl.com/~rothwlr/lsfpaper/lsfpage.html
  19. ********************************************************************/
  20. /* Note that the lpc-lsp conversion finds the roots of polynomial with
  21. an iterative root polisher (CACM algorithm 283). It *is* possible
  22. to confuse this algorithm into not converging; that should only
  23. happen with absurdly closely spaced roots (very sharp peaks in the
  24. LPC f response) which in turn should be impossible in our use of
  25. the code. If this *does* happen anyway, it's a bug in the floor
  26. finder; find the cause of the confusion (probably a single bin
  27. spike or accidental near-float-limit resolution problems) and
  28. correct it. */
  29. #include <math.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include "lsp.h"
  33. #include "os.h"
  34. #include "misc.h"
  35. void vorbis_lsp_to_curve(float *curve,int n,float *lsp,int m,float amp,
  36. float *w){
  37. int i,j,k;
  38. float *coslsp=alloca(m*sizeof(float));
  39. for(i=0;i<m;i++)coslsp[i]=2*cos(lsp[i]);
  40. for(k=0;k<n;k++){
  41. double p=.5;
  42. double q=.5;
  43. for(j=0;j<m;j+=2){
  44. p*= w[k]-coslsp[j];
  45. q*= w[k]-coslsp[j+1];
  46. }
  47. curve[k]=amp/sqrt(p*p*(2.+ w[k])+q*q*(2.- w[k]));
  48. }
  49. }
  50. static void cheby(float *g, int ord) {
  51. int i, j;
  52. g[0] *= 0.5;
  53. for(i=2; i<= ord; i++) {
  54. for(j=ord; j >= i; j--) {
  55. g[j-2] -= g[j];
  56. g[j] += g[j];
  57. }
  58. }
  59. }
  60. static int comp(const void *a,const void *b){
  61. if(*(float *)a<*(float *)b)
  62. return(1);
  63. else
  64. return(-1);
  65. }
  66. /* CACM algorithm 283. */
  67. /* we require doubles here due to the huge spread between val/p and
  68. the required max error of 1.e-12, which is beyond the capabilities
  69. of floats */
  70. static void cacm283(float *a,int ord,float *r){
  71. int i, k;
  72. double val, p, delta, error;
  73. double rooti;
  74. for(i=0; i<ord;i++) r[i] = 2.0 * (i+0.5) / ord - 1.0;
  75. for(error=1 ; error > 1.e-12; ) {
  76. error = 0;
  77. for( i=0; i<ord; i++) { /* Update each point. */
  78. rooti = r[i];
  79. val = a[ord];
  80. p = a[ord];
  81. for(k=ord-1; k>= 0; k--) {
  82. val = val * rooti + a[k];
  83. if (k != i) p *= rooti - r[k];
  84. }
  85. delta = val/p;
  86. r[i] -= delta;
  87. error += delta*delta;
  88. }
  89. }
  90. /* Replaced the original bubble sort with a real sort. With your
  91. help, we can eliminate the bubble sort in our lifetime. --Monty */
  92. qsort(r,ord,sizeof(float),comp);
  93. }
  94. /* Convert lpc coefficients to lsp coefficients */
  95. void vorbis_lpc_to_lsp(float *lpc,float *lsp,int m){
  96. int order2=m/2;
  97. float *g1=alloca(sizeof(float)*(order2+1));
  98. float *g2=alloca(sizeof(float)*(order2+1));
  99. float *g1r=alloca(sizeof(float)*(order2+1));
  100. float *g2r=alloca(sizeof(float)*(order2+1));
  101. int i;
  102. /* Compute the lengths of the x polynomials. */
  103. /* Compute the first half of K & R F1 & F2 polynomials. */
  104. /* Compute half of the symmetric and antisymmetric polynomials. */
  105. /* Remove the roots at +1 and -1. */
  106. g1[order2] = 1.0;
  107. for(i=0;i<order2;i++) g1[order2-i-1] = lpc[i]+lpc[m-i-1];
  108. g2[order2] = 1.0;
  109. for(i=0;i<order2;i++) g2[order2-i-1] = lpc[i]-lpc[m-i-1];
  110. for(i=0; i<order2;i++) g1[order2-i-1] -= g1[order2-i];
  111. for(i=0; i<order2;i++) g2[order2-i-1] += g2[order2-i];
  112. /* Convert into polynomials in cos(alpha) */
  113. cheby(g1,order2);
  114. cheby(g2,order2);
  115. /* Find the roots of the 2 even polynomials.*/
  116. cacm283(g1,order2,g1r);
  117. cacm283(g2,order2,g2r);
  118. for(i=0;i<m;i+=2){
  119. lsp[i] = acos(g1r[i/2]);
  120. lsp[i+1] = acos(g2r[i/2]);
  121. }
  122. }