Remove svn $Id$ header.
[platform/upstream/libvorbis.git] / lib / lpc.c
index c94b9ee..798f4cf 100644 (file)
--- a/lib/lpc.c
+++ b/lib/lpc.c
@@ -1,18 +1,16 @@
 /********************************************************************
  *                                                                  *
- * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE.  *
- * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
- * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
- * PLEASE READ THESE TERMS DISTRIBUTING.                            *
+ * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
+ * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
+ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
+ * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
  *                                                                  *
- * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
- * by Monty <monty@xiph.org> and The XIPHOPHORUS Company            *
- * http://www.xiph.org/                                             *
+ * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009             *
+ * by the Xiph.Org Foundation http://www.xiph.org/                  *
  *                                                                  *
  ********************************************************************
 
   function: LPC low level routines
-  last mod: $Id: lpc.c,v 1.23 2000/08/15 09:09:43 xiphmont Exp $
 
  ********************************************************************/
 
@@ -59,30 +57,34 @@ Carsten Bormann
 /* Input : n elements of time doamin data
    Output: m lpc coefficients, excitation energy */
 
-double vorbis_lpc_from_data(double *data,double *lpc,int n,int m){
-  double *aut=alloca(sizeof(double)*(m+1));
+float vorbis_lpc_from_data(float *data,float *lpci,int n,int m){
+  double *aut=alloca(sizeof(*aut)*(m+1));
+  double *lpc=alloca(sizeof(*lpc)*(m));
   double error;
+  double epsilon;
   int i,j;
 
   /* autocorrelation, p+1 lag coefficients */
-
   j=m+1;
   while(j--){
-    double d=0;
-    for(i=j;i<n;i++)d+=data[i]*data[i-j];
+    double d=0; /* double needed for accumulator depth */
+    for(i=j;i<n;i++)d+=(double)data[i]*data[i-j];
     aut[j]=d;
   }
-  
+
   /* Generate lpc coefficients from autocorr values */
 
-  error=aut[0];
-  if(error==0){
-    memset(lpc,0,m*sizeof(double));
-    return 0;
-  }
-  
+  /* set our noise floor to about -100dB */
+  error=aut[0] * (1. + 1e-10);
+  epsilon=1e-9*aut[0]+1e-10;
+
   for(i=0;i<m;i++){
-    double r=-aut[i+1];
+    double r= -aut[i+1];
+
+    if(error<epsilon){
+      memset(lpc+i,0,(m-i)*sizeof(*lpc));
+      goto done;
+    }
 
     /* Sum up this iteration's reflection coefficient; note that in
        Vorbis we don't save it.  If anyone wants to recycle this code
@@ -90,202 +92,68 @@ double vorbis_lpc_from_data(double *data,double *lpc,int n,int m){
        each iteration. */
 
     for(j=0;j<i;j++)r-=lpc[j]*aut[i-j];
-    r/=error; 
+    r/=error;
 
     /* Update LPC coefficients and total error */
-    
+
     lpc[i]=r;
     for(j=0;j<i/2;j++){
       double tmp=lpc[j];
+
       lpc[j]+=r*lpc[i-1-j];
       lpc[i-1-j]+=r*tmp;
     }
-    if(i%2)lpc[j]+=lpc[j]*r;
-    
-    error*=1.0-r*r;
-  }
-  
-  /* we need the error value to know how big an impulse to hit the
-     filter with later */
-  
-  return error;
-}
+    if(i&1)lpc[j]+=lpc[j]*r;
 
-/* Input : n element envelope spectral curve
-   Output: m lpc coefficients, excitation energy */
+    error*=1.-r*r;
 
-double vorbis_lpc_from_curve(double *curve,double *lpc,lpc_lookup *l){
-  int n=l->ln;
-  int m=l->m;
-  double *work=alloca(sizeof(double)*(n+n));
-  double fscale=.5/n;
-  int i,j;
-  
-  /* input is a real curve. make it complex-real */
-  /* This mixes phase, but the LPC generation doesn't care. */
-  for(i=0;i<n;i++){
-    work[i*2]=curve[i]*fscale;
-    work[i*2+1]=0;
-  }
-  work[n*2-1]=curve[n-1]*fscale;
-  
-  n*=2;
-  drft_backward(&l->fft,work);
-
-  /* The autocorrelation will not be circular.  Shift, else we lose
-     most of the power in the edges. */
-  
-  for(i=0,j=n/2;i<n/2;){
-    double temp=work[i];
-    work[i++]=work[j];
-    work[j++]=temp;
   }
-  
-  /* we *could* shave speed here by skimping on the edges (thus
-     speeding up the autocorrelation in vorbis_lpc_from_data) but we
-     don't right now. */
 
-  return(vorbis_lpc_from_data(work,lpc,n,m));
-}
-
-void lpc_init(lpc_lookup *l,long mapped, int m){
-  memset(l,0,sizeof(lpc_lookup));
-
-  l->ln=mapped;
-  l->m=m;
-
-  /* we cheat decoding the LPC spectrum via FFTs */  
-  drft_init(&l->fft,mapped*2);
-
-}
-
-void lpc_clear(lpc_lookup *l){
-  if(l){
-    drft_clear(&l->fft);
-  }
-}
-
-/* One can do this the long way by generating the transfer function in
-   the time domain and taking the forward FFT of the result.  The
-   results from direct calculation are cleaner and faster. 
-
-   This version does a linear curve generation and then later
-   interpolates the log curve from the linear curve.  */
-
-void vorbis_lpc_to_curve(double *curve,double *lpc,double amp,
-                        lpc_lookup *l){
-  int i;
-  memset(curve,0,sizeof(double)*l->ln*2);
-  if(amp==0)return;
-
-  for(i=0;i<l->m;i++){
-    curve[i*2+1]=lpc[i]/(4*amp);
-    curve[i*2+2]=-lpc[i]/(4*amp);
-  }
-
-  drft_backward(&l->fft,curve); /* reappropriated ;-) */
+ done:
 
+  /* slightly damp the filter */
   {
-    int l2=l->ln*2;
-    double unit=1./amp;
-    curve[0]=(1./(curve[0]*2+unit));
-    for(i=1;i<l->ln;i++){
-      double real=(curve[i]+curve[l2-i]);
-      double imag=(curve[i]-curve[l2-i]);
-
-      double a = real + unit;
-      curve[i] = 1.0 / FAST_HYPOT(a, imag);
+    double g = .99;
+    double damp = g;
+    for(j=0;j<m;j++){
+      lpc[j]*=damp;
+      damp*=g;
     }
   }
-}  
-
-/* subtract or add an lpc filter to data. */
-
-void vorbis_lpc_filter(double *coeff,double *prime,int m,
-                       double *data,long n,double amp){
-
-  /* in: coeff[0...m-1] LPC coefficients 
-         prime[0...m-1] initial values 
-         data[0...n-1] data samples 
-    out: data[0...n-1] residuals from LPC prediction */
-
-  long i,j;
-  double *work=alloca(sizeof(double)*(m+n));
-  double y;
-
-  if(!prime)
-    for(i=0;i<m;i++)
-      work[i]=0;
-  else
-    for(i=0;i<m;i++)
-      work[i]=prime[i];
-
-  for(i=0;i<n;i++){
-    y=0;
-    for(j=0;j<m;j++)
-      y-=work[i+j]*coeff[m-j-1];
-    
-    data[i]=work[i+m]=data[i]+y;
-
-  }
-}
-
-void vorbis_lpc_residue(double *coeff,double *prime,int m,
-                       double *data,long n){
 
-  /* in: coeff[0...m-1] LPC coefficients 
-         prime[0...m-1] initial values 
-         data[0...n-1] data samples 
-    out: data[0...n-1] residuals from LPC prediction */
+  for(j=0;j<m;j++)lpci[j]=(float)lpc[j];
 
-  long i,j;
-  double *work=alloca(sizeof(double)*(m+n));
-  double y;
-
-  if(!prime)
-    for(i=0;i<m;i++)
-      work[i]=0;
-  else
-    for(i=0;i<m;i++)
-      work[i]=prime[i];
+  /* we need the error value to know how big an impulse to hit the
+     filter with later */
 
-  for(i=0;i<n;i++){
-    y=0;
-    for(j=0;j<m;j++)
-      y-=work[i+j]*coeff[m-j-1];
-    
-    work[i+m]=data[i];
-    data[i]-=y;
-  }
+  return error;
 }
 
-void vorbis_lpc_predict(double *coeff,double *prime,int m,
-                     double *data,long n){
+void vorbis_lpc_predict(float *coeff,float *prime,int m,
+                     float *data,long n){
 
-  /* in: coeff[0...m-1] LPC coefficients 
+  /* in: coeff[0...m-1] LPC coefficients
          prime[0...m-1] initial values (allocated size of n+m-1)
-         data[0...n-1] residuals from LPC prediction   
     out: data[0...n-1] data samples */
 
   long i,j,o,p;
-  double y;
-  double *work=alloca(sizeof(double)*(m+n));
+  float y;
+  float *work=alloca(sizeof(*work)*(m+n));
 
   if(!prime)
     for(i=0;i<m;i++)
-      work[i]=0.;
+      work[i]=0.f;
   else
     for(i=0;i<m;i++)
       work[i]=prime[i];
 
   for(i=0;i<n;i++){
-    y=data[i];
+    y=0;
     o=i;
     p=m;
     for(j=0;j<m;j++)
       y-=work[o++]*coeff[--p];
-    
+
     data[i]=work[o]=y;
   }
 }
-