lpc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
  9. * by the Xiph.Org Foundation https://xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: LPC low level routines
  13. ********************************************************************/
  14. /* Some of these routines (autocorrelator, LPC coefficient estimator)
  15. are derived from code written by Jutta Degener and Carsten Bormann;
  16. thus we include their copyright below. The entirety of this file
  17. is freely redistributable on the condition that both of these
  18. copyright notices are preserved without modification. */
  19. /* Preserved Copyright: *********************************************/
  20. /* Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
  21. Technische Universita"t Berlin
  22. Any use of this software is permitted provided that this notice is not
  23. removed and that neither the authors nor the Technische Universita"t
  24. Berlin are deemed to have made any representations as to the
  25. suitability of this software for any purpose nor are held responsible
  26. for any defects of this software. THERE IS ABSOLUTELY NO WARRANTY FOR
  27. THIS SOFTWARE.
  28. As a matter of courtesy, the authors request to be informed about uses
  29. this software has found, about bugs in this software, and about any
  30. improvements that may be of general interest.
  31. Berlin, 28.11.1994
  32. Jutta Degener
  33. Carsten Bormann
  34. *********************************************************************/
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <math.h>
  38. #include "os.h"
  39. #include "smallft.h"
  40. #include "lpc.h"
  41. #include "scales.h"
  42. #include "misc.h"
  43. /* Autocorrelation LPC coeff generation algorithm invented by
  44. N. Levinson in 1947, modified by J. Durbin in 1959. */
  45. /* Input : n elements of time doamin data
  46. Output: m lpc coefficients, excitation energy */
  47. float vorbis_lpc_from_data(float *data,float *lpci,int n,int m){
  48. double *aut=alloca(sizeof(*aut)*(m+1));
  49. double *lpc=alloca(sizeof(*lpc)*(m));
  50. double error;
  51. double epsilon;
  52. int i,j;
  53. /* autocorrelation, p+1 lag coefficients */
  54. j=m+1;
  55. while(j--){
  56. double d=0; /* double needed for accumulator depth */
  57. for(i=j;i<n;i++)d+=(double)data[i]*data[i-j];
  58. aut[j]=d;
  59. }
  60. /* Generate lpc coefficients from autocorr values */
  61. /* set our noise floor to about -100dB */
  62. error=aut[0] * (1. + 1e-10);
  63. epsilon=1e-9*aut[0]+1e-10;
  64. for(i=0;i<m;i++){
  65. double r= -aut[i+1];
  66. if(error<epsilon){
  67. memset(lpc+i,0,(m-i)*sizeof(*lpc));
  68. goto done;
  69. }
  70. /* Sum up this iteration's reflection coefficient; note that in
  71. Vorbis we don't save it. If anyone wants to recycle this code
  72. and needs reflection coefficients, save the results of 'r' from
  73. each iteration. */
  74. for(j=0;j<i;j++)r-=lpc[j]*aut[i-j];
  75. r/=error;
  76. /* Update LPC coefficients and total error */
  77. lpc[i]=r;
  78. for(j=0;j<i/2;j++){
  79. double tmp=lpc[j];
  80. lpc[j]+=r*lpc[i-1-j];
  81. lpc[i-1-j]+=r*tmp;
  82. }
  83. if(i&1)lpc[j]+=lpc[j]*r;
  84. error*=1.-r*r;
  85. }
  86. done:
  87. /* slightly damp the filter */
  88. {
  89. double g = .99;
  90. double damp = g;
  91. for(j=0;j<m;j++){
  92. lpc[j]*=damp;
  93. damp*=g;
  94. }
  95. }
  96. for(j=0;j<m;j++)lpci[j]=(float)lpc[j];
  97. /* we need the error value to know how big an impulse to hit the
  98. filter with later */
  99. return error;
  100. }
  101. void vorbis_lpc_predict(float *coeff,float *prime,int m,
  102. float *data,long n){
  103. /* in: coeff[0...m-1] LPC coefficients
  104. prime[0...m-1] initial values (allocated size of n+m-1)
  105. out: data[0...n-1] data samples */
  106. long i,j,o,p;
  107. float y;
  108. float *work=alloca(sizeof(*work)*(m+n));
  109. if(!prime)
  110. for(i=0;i<m;i++)
  111. work[i]=0.f;
  112. else
  113. for(i=0;i<m;i++)
  114. work[i]=prime[i];
  115. for(i=0;i<n;i++){
  116. y=0;
  117. o=i;
  118. p=m;
  119. for(j=0;j<m;j++)
  120. y-=work[o++]*coeff[--p];
  121. data[i]=work[o]=y;
  122. }
  123. }