lpc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-2002 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: LPC low level routines
  13. last mod: $Id: lpc.c,v 1.37 2003/03/08 07:15:32 xiphmont Exp $
  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. 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. error=aut[0];
  62. for(i=0;i<m;i++){
  63. double r= -aut[i+1];
  64. if(error==0){
  65. memset(lpci,0,m*sizeof(*lpci));
  66. return 0;
  67. }
  68. /* Sum up this iteration's reflection coefficient; note that in
  69. Vorbis we don't save it. If anyone wants to recycle this code
  70. and needs reflection coefficients, save the results of 'r' from
  71. each iteration. */
  72. for(j=0;j<i;j++)r-=lpc[j]*aut[i-j];
  73. r/=error;
  74. /* Update LPC coefficients and total error */
  75. lpc[i]=r;
  76. for(j=0;j<i/2;j++){
  77. double tmp=lpc[j];
  78. lpc[j]+=r*lpc[i-1-j];
  79. lpc[i-1-j]+=r*tmp;
  80. }
  81. if(i%2)lpc[j]+=lpc[j]*r;
  82. error*=1.f-r*r;
  83. }
  84. for(j=0;j<m;j++)lpci[j]=(float)lpc[j];
  85. /* we need the error value to know how big an impulse to hit the
  86. filter with later */
  87. return error;
  88. }
  89. void vorbis_lpc_predict(float *coeff,float *prime,int m,
  90. float *data,long n){
  91. /* in: coeff[0...m-1] LPC coefficients
  92. prime[0...m-1] initial values (allocated size of n+m-1)
  93. out: data[0...n-1] data samples */
  94. long i,j,o,p;
  95. float y;
  96. float *work=alloca(sizeof(*work)*(m+n));
  97. if(!prime)
  98. for(i=0;i<m;i++)
  99. work[i]=0.f;
  100. else
  101. for(i=0;i<m;i++)
  102. work[i]=prime[i];
  103. for(i=0;i<n;i++){
  104. y=0;
  105. o=i;
  106. p=m;
  107. for(j=0;j<m;j++)
  108. y-=work[o++]*coeff[--p];
  109. data[i]=work[o]=y;
  110. }
  111. }