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 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-2001 *
  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.34 2001/12/20 01:00:27 segher 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 *lpc,int n,int m){
  49. float *aut=alloca(sizeof(*aut)*(m+1));
  50. float error;
  51. int i,j;
  52. /* autocorrelation, p+1 lag coefficients */
  53. j=m+1;
  54. while(j--){
  55. double d=0; /* double needed for accumulator depth */
  56. for(i=j;i<n;i++)d+=data[i]*data[i-j];
  57. aut[j]=d;
  58. }
  59. /* Generate lpc coefficients from autocorr values */
  60. error=aut[0];
  61. for(i=0;i<m;i++){
  62. float r= -aut[i+1];
  63. if(error==0){
  64. memset(lpc,0,m*sizeof(*lpc));
  65. return 0;
  66. }
  67. /* Sum up this iteration's reflection coefficient; note that in
  68. Vorbis we don't save it. If anyone wants to recycle this code
  69. and needs reflection coefficients, save the results of 'r' from
  70. each iteration. */
  71. for(j=0;j<i;j++)r-=lpc[j]*aut[i-j];
  72. r/=error;
  73. /* Update LPC coefficients and total error */
  74. lpc[i]=r;
  75. for(j=0;j<i/2;j++){
  76. float tmp=lpc[j];
  77. lpc[j]+=r*lpc[i-1-j];
  78. lpc[i-1-j]+=r*tmp;
  79. }
  80. if(i%2)lpc[j]+=lpc[j]*r;
  81. error*=1.f-r*r;
  82. }
  83. /* we need the error value to know how big an impulse to hit the
  84. filter with later */
  85. return error;
  86. }
  87. /* Input : n element envelope spectral curve
  88. Output: m lpc coefficients, excitation energy */
  89. float vorbis_lpc_from_curve(float *curve,float *lpc,lpc_lookup *l){
  90. int n=l->ln;
  91. int m=l->m;
  92. float *work=alloca(sizeof(*work)*(n+n));
  93. float fscale=.5f/n;
  94. int i,j;
  95. /* input is a real curve. make it complex-real */
  96. /* This mixes phase, but the LPC generation doesn't care. */
  97. for(i=0;i<n;i++){
  98. work[i*2]=curve[i]*fscale;
  99. work[i*2+1]=0;
  100. }
  101. work[n*2-1]=curve[n-1]*fscale;
  102. n*=2;
  103. drft_backward(&l->fft,work);
  104. /* The autocorrelation will not be circular. Shift, else we lose
  105. most of the power in the edges. */
  106. for(i=0,j=n/2;i<n/2;){
  107. float temp=work[i];
  108. work[i++]=work[j];
  109. work[j++]=temp;
  110. }
  111. /* we *could* shave speed here by skimping on the edges (thus
  112. speeding up the autocorrelation in vorbis_lpc_from_data) but we
  113. don't right now. */
  114. return(vorbis_lpc_from_data(work,lpc,n,m));
  115. }
  116. void lpc_init(lpc_lookup *l,long mapped, int m){
  117. memset(l,0,sizeof(*l));
  118. l->ln=mapped;
  119. l->m=m;
  120. /* we cheat decoding the LPC spectrum via FFTs */
  121. drft_init(&l->fft,mapped*2);
  122. }
  123. void lpc_clear(lpc_lookup *l){
  124. if(l){
  125. drft_clear(&l->fft);
  126. }
  127. }
  128. void vorbis_lpc_predict(float *coeff,float *prime,int m,
  129. float *data,long n){
  130. /* in: coeff[0...m-1] LPC coefficients
  131. prime[0...m-1] initial values (allocated size of n+m-1)
  132. out: data[0...n-1] data samples */
  133. long i,j,o,p;
  134. float y;
  135. float *work=alloca(sizeof(*work)*(m+n));
  136. if(!prime)
  137. for(i=0;i<m;i++)
  138. work[i]=0.f;
  139. else
  140. for(i=0;i<m;i++)
  141. work[i]=prime[i];
  142. for(i=0;i<n;i++){
  143. y=0;
  144. o=i;
  145. p=m;
  146. for(j=0;j<m;j++)
  147. y-=work[o++]*coeff[--p];
  148. data[i]=work[o]=y;
  149. }
  150. }