lpc.c 4.4 KB

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