lpc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
  6. * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis 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: LPC low level routines
  14. last mod: $Id: lpc.c,v 1.26.2.1 2000/11/04 06:21:44 xiphmont Exp $
  15. ********************************************************************/
  16. /* Some of these routines (autocorrelator, LPC coefficient estimator)
  17. are derived from code written by Jutta Degener and Carsten Bormann;
  18. thus we include their copyright below. The entirety of this file
  19. is freely redistributable on the condition that both of these
  20. copyright notices are preserved without modification. */
  21. /* Preserved Copyright: *********************************************/
  22. /* Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
  23. Technische Universita"t Berlin
  24. Any use of this software is permitted provided that this notice is not
  25. removed and that neither the authors nor the Technische Universita"t
  26. Berlin are deemed to have made any representations as to the
  27. suitability of this software for any purpose nor are held responsible
  28. for any defects of this software. THERE IS ABSOLUTELY NO WARRANTY FOR
  29. THIS SOFTWARE.
  30. As a matter of courtesy, the authors request to be informed about uses
  31. this software has found, about bugs in this software, and about any
  32. improvements that may be of general interest.
  33. Berlin, 28.11.1994
  34. Jutta Degener
  35. Carsten Bormann
  36. *********************************************************************/
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <math.h>
  40. #include "os.h"
  41. #include "smallft.h"
  42. #include "lpc.h"
  43. #include "scales.h"
  44. #include "misc.h"
  45. /* Autocorrelation LPC coeff generation algorithm invented by
  46. N. Levinson in 1947, modified by J. Durbin in 1959. */
  47. /* Input : n elements of time doamin data
  48. Output: m lpc coefficients, excitation energy */
  49. float vorbis_lpc_from_data(float *data,float *lpc,int n,int m){
  50. float *aut=alloca(sizeof(float)*(m+1));
  51. float error;
  52. int i,j;
  53. /* autocorrelation, p+1 lag coefficients */
  54. j=m+1;
  55. while(j--){
  56. float d=0;
  57. for(i=j;i<n;i++)d+=data[i]*data[i-j];
  58. aut[j]=d;
  59. }
  60. /* Generate lpc coefficients from autocorr values */
  61. error=aut[0];
  62. if(error==0){
  63. memset(lpc,0,m*sizeof(float));
  64. return 0;
  65. }
  66. for(i=0;i<m;i++){
  67. float r=-aut[i+1];
  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. float 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.0-r*r;
  83. }
  84. /* we need the error value to know how big an impulse to hit the
  85. filter with later */
  86. return error;
  87. }
  88. /* Input : n element envelope spectral curve
  89. Output: m lpc coefficients, excitation energy */
  90. float vorbis_lpc_from_curve(float *curve,float *lpc,lpc_lookup *l){
  91. int n=l->ln;
  92. int m=l->m;
  93. float *work=alloca(sizeof(float)*(n+n));
  94. float fscale=.5/n;
  95. int i,j;
  96. /* input is a real curve. make it complex-real */
  97. /* This mixes phase, but the LPC generation doesn't care. */
  98. for(i=0;i<n;i++){
  99. work[i*2]=curve[i]*fscale;
  100. work[i*2+1]=0;
  101. }
  102. work[n*2-1]=curve[n-1]*fscale;
  103. n*=2;
  104. drft_backward(&l->fft,work);
  105. /* The autocorrelation will not be circular. Shift, else we lose
  106. most of the power in the edges. */
  107. for(i=0,j=n/2;i<n/2;){
  108. float temp=work[i];
  109. work[i++]=work[j];
  110. work[j++]=temp;
  111. }
  112. /* we *could* shave speed here by skimping on the edges (thus
  113. speeding up the autocorrelation in vorbis_lpc_from_data) but we
  114. don't right now. */
  115. return(vorbis_lpc_from_data(work,lpc,n,m));
  116. }
  117. void lpc_init(lpc_lookup *l,long mapped, int m){
  118. memset(l,0,sizeof(lpc_lookup));
  119. l->ln=mapped;
  120. l->m=m;
  121. /* we cheat decoding the LPC spectrum via FFTs */
  122. drft_init(&l->fft,mapped*2);
  123. }
  124. void lpc_clear(lpc_lookup *l){
  125. if(l){
  126. drft_clear(&l->fft);
  127. }
  128. }
  129. void vorbis_lpc_predict(float *coeff,float *prime,int m,
  130. float *data,long n){
  131. /* in: coeff[0...m-1] LPC coefficients
  132. prime[0...m-1] initial values (allocated size of n+m-1)
  133. out: data[0...n-1] data samples */
  134. long i,j,o,p;
  135. float y;
  136. float *work=alloca(sizeof(float)*(m+n));
  137. if(!prime)
  138. for(i=0;i<m;i++)
  139. work[i]=0.;
  140. else
  141. for(i=0;i<m;i++)
  142. work[i]=prime[i];
  143. for(i=0;i<n;i++){
  144. y=0;
  145. o=i;
  146. p=m;
  147. for(j=0;j<m;j++)
  148. y-=work[o++]*coeff[--p];
  149. data[i]=work[o]=y;
  150. }
  151. }