lsp.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.4.4.3 2000/04/21 16:35:39 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-double-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. void vorbis_lsp_to_lpc(double *lsp,double *lpc,int m){
  35. int i,j,m2=m/2;
  36. double *O=alloca(sizeof(double)*m2);
  37. double *E=alloca(sizeof(double)*m2);
  38. double A;
  39. double *Ae=alloca(sizeof(double)*(m2+1));
  40. double *Ao=alloca(sizeof(double)*(m2+1));
  41. double B;
  42. double *Be=alloca(sizeof(double)*(m2));
  43. double *Bo=alloca(sizeof(double)*(m2));
  44. double temp;
  45. /* even/odd roots setup */
  46. for(i=0;i<m2;i++){
  47. O[i]=-2.*cos(lsp[i*2]);
  48. E[i]=-2.*cos(lsp[i*2+1]);
  49. }
  50. /* set up impulse response */
  51. for(j=0;j<m2;j++){
  52. Ae[j]=0.;
  53. Ao[j]=1.;
  54. Be[j]=0.;
  55. Bo[j]=1.;
  56. }
  57. Ao[j]=1.;
  58. Ae[j]=1.;
  59. /* run impulse response */
  60. for(i=1;i<m+1;i++){
  61. A=B=0.;
  62. for(j=0;j<m2;j++){
  63. temp=O[j]*Ao[j]+Ae[j];
  64. Ae[j]=Ao[j];
  65. Ao[j]=A;
  66. A+=temp;
  67. temp=E[j]*Bo[j]+Be[j];
  68. Be[j]=Bo[j];
  69. Bo[j]=B;
  70. B+=temp;
  71. }
  72. lpc[i-1]=(A+Ao[j]+B-Ae[j])/2;
  73. Ao[j]=A;
  74. Ae[j]=B;
  75. }
  76. }
  77. static void cheby(double *g, int ord) {
  78. int i, j;
  79. g[0] *= 0.5;
  80. for(i=2; i<= ord; i++) {
  81. for(j=ord; j >= i; j--) {
  82. g[j-2] -= g[j];
  83. g[j] += g[j];
  84. }
  85. }
  86. }
  87. static int comp(const void *a,const void *b){
  88. if(*(double *)a<*(double *)b)
  89. return(1);
  90. else
  91. return(-1);
  92. }
  93. /* CACM algorithm 283. */
  94. static void cacm283(double *a,int ord,double *r){
  95. int i, k;
  96. double val, p, delta, error;
  97. double rooti;
  98. for(i=0; i<ord;i++) r[i] = 2.0 * (i+0.5) / ord - 1.0;
  99. for(error=1 ; error > 1.e-12; ) {
  100. error = 0;
  101. for( i=0; i<ord; i++) { /* Update each point. */
  102. rooti = r[i];
  103. val = a[ord];
  104. p = a[ord];
  105. for(k=ord-1; k>= 0; k--) {
  106. val = val * rooti + a[k];
  107. if (k != i) p *= rooti - r[k];
  108. }
  109. delta = val/p;
  110. r[i] -= delta;
  111. error += delta*delta;
  112. }
  113. }
  114. /* Replaced the original bubble sort with a real sort. With your
  115. help, we can eliminate the bubble sort in our lifetime. --Monty */
  116. qsort(r,ord,sizeof(double),comp);
  117. }
  118. /* Convert lpc coefficients to lsp coefficients */
  119. void vorbis_lpc_to_lsp(double *lpc,double *lsp,int m){
  120. int order2=m/2;
  121. double *g1=alloca(sizeof(double)*(order2+1));
  122. double *g2=alloca(sizeof(double)*(order2+1));
  123. double *g1r=alloca(sizeof(double)*(order2+1));
  124. double *g2r=alloca(sizeof(double)*(order2+1));
  125. int i;
  126. /* Compute the lengths of the x polynomials. */
  127. /* Compute the first half of K & R F1 & F2 polynomials. */
  128. /* Compute half of the symmetric and antisymmetric polynomials. */
  129. /* Remove the roots at +1 and -1. */
  130. g1[order2] = 1.0;
  131. for(i=0;i<order2;i++) g1[order2-i-1] = lpc[i]+lpc[m-i-1];
  132. g2[order2] = 1.0;
  133. for(i=0;i<order2;i++) g2[order2-i-1] = lpc[i]-lpc[m-i-1];
  134. for(i=0; i<order2;i++) g1[order2-i-1] -= g1[order2-i];
  135. for(i=0; i<order2;i++) g2[order2-i-1] += g2[order2-i];
  136. /* Convert into polynomials in cos(alpha) */
  137. cheby(g1,order2);
  138. cheby(g2,order2);
  139. /* Find the roots of the 2 even polynomials.*/
  140. cacm283(g1,order2,g1r);
  141. cacm283(g2,order2,g2r);
  142. for(i=0;i<m;i+=2){
  143. lsp[i] = acos(g1r[i/2]);
  144. lsp[i+1] = acos(g2r[i/2]);
  145. }
  146. }