summaryrefslogtreecommitdiff
path: root/lib/lpc.c
blob: ced6b298632a797d124c6f9e97a78f8591471b84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/********************************************************************
 *                                                                  *
 * 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.                            *
 *                                                                  *
 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
 * by Monty <monty@xiph.org> and The XIPHOPHORUS Company            *
 * http://www.xiph.org/                                             *
 *                                                                  *
 ********************************************************************

  function: LPC low level routines
  last mod: $Id: lpc.c,v 1.18.2.1 2000/03/29 03:49:28 xiphmont Exp $

 ********************************************************************/

/* Some of these routines (autocorrelator, LPC coefficient estimator)
   are derived from code written by Jutta Degener and Carsten Bormann;
   thus we include their copyright below.  The entirety of this file
   is freely redistributable on the condition that both of these
   copyright notices are preserved without modification.  */

/* Preserved Copyright: *********************************************/

/* Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
Technische Universita"t Berlin

Any use of this software is permitted provided that this notice is not
removed and that neither the authors nor the Technische Universita"t
Berlin are deemed to have made any representations as to the
suitability of this software for any purpose nor are held responsible
for any defects of this software. THERE IS ABSOLUTELY NO WARRANTY FOR
THIS SOFTWARE.

As a matter of courtesy, the authors request to be informed about uses
this software has found, about bugs in this software, and about any
improvements that may be of general interest.

Berlin, 28.11.1994
Jutta Degener
Carsten Bormann

*********************************************************************/

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "os.h"
#include "smallft.h"
#include "lpc.h"
#include "scales.h"
#include "misc.h"

/* Autocorrelation LPC coeff generation algorithm invented by
   N. Levinson in 1947, modified by J. Durbin in 1959. */

/* 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));
  double error;
  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];
    aut[j]=d;
  }
  
  /* Generate lpc coefficients from autocorr values */

  error=aut[0];
  if(error==0){
    memset(lpc,0,m*sizeof(double));
    return 0;
  }
  
  for(i=0;i<m;i++){
    double r=-aut[i+1];

    /* Sum up this iteration's reflection coefficient; note that in
       Vorbis we don't save it.  If anyone wants to recycle this code
       and needs reflection coefficients, save the results of 'r' from
       each iteration. */

    for(j=0;j<i;j++)r-=lpc[j]*aut[i-j];
    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;
}

/* Input : n element envelope spectral curve
   Output: m lpc coefficients, excitation energy */

double vorbis_lpc_from_spectrum(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;
  }
  
  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;
  }
  
  return(vorbis_lpc_from_data(work,lpc,n,m));
}

/* initialize Bark scale and normalization lookups.  We could do this
   with static tables, but Vorbis allows a number of possible
   combinations, so it's best to do it computationally.

   The below is authoritative in terms of defining scale mapping.
   Note that the scale depends on the sampling rate as well as the
   linear block and mapping sizes */

void lpc_init(lpc_lookup *l,int n, long mapped, long rate, int m){
  int i;
  double scale;
  memset(l,0,sizeof(lpc_lookup));

  l->n=n;
  l->ln=mapped;
  l->m=m;

  l->linearmap=malloc(n*sizeof(int));
  l->barknorm=malloc(mapped*sizeof(double));

  /* we choose a scaling constant so that:
     floor(bark(rate/2-1)*C)=mapped-1
     floor(bark(rate/2)*C)=mapped */

  scale=mapped/toBARK(rate/2.);

  /* the mapping from a linear scale to a smaller bark scale is
     straightforward.  We do *not* make sure that the linear mapping
     does not skip bark-scale bins; the decoder simply skips them and
     the encoder may do what it wishes in filling them.  They're
     necessary in some mapping combinations to keep the scale spacing
     accurate */
  {
    int last=-1;
    for(i=0;i<n;i++){
      int val=floor( toBARK((rate/2.)/n*i) *scale); /* bark numbers
							    represent
							    band edges */
      if(val>=mapped)val=mapped; /* guard against the approximation */
      l->linearmap[i]=val;
      last=val;
    }
  }

  /* 'Normalization' is just making sure that power isn't lost in the
     log scale by virtue of compressing the scale in higher
     frequencies.  We figure the weight of bands in proportion to
     their linear/bark width ratio below, again, authoritatively.  We
     use computed width (not the number of actual bins above) for
     smoothness in the scale; they should agree closely */

  /* keep it 0. to 1., else the dynamic range starts spreading through
     all the squaring... */

  for(i=0;i<mapped;i++)
    l->barknorm[i]=(fromBARK((i+1)/scale)-fromBARK(i/scale));
  for(i=0;i<mapped;i++)
    l->barknorm[i]/=l->barknorm[mapped-1];

  /* we cheat decoding the LPC spectrum via FFTs */
  
  drft_init(&l->fft,mapped*2);

}

void lpc_clear(lpc_lookup *l){
  if(l){
    if(l->barknorm)free(l->barknorm);
    if(l->linearmap)free(l->linearmap);
    drft_clear(&l->fft);
  }
}


/* less efficient than the decode side (written for clarity).  We're
   not bottlenecked here anyway */
double vorbis_curve_to_lpc(double *curve,double *lpc,lpc_lookup *l){
  /* map the input curve to a bark-scale curve for encoding */
  
  int mapped=l->ln;
  double *work=alloca(sizeof(double)*mapped);
  int i,j,last=0;

  memset(work,0,sizeof(double)*mapped);

  /* Only the decode side is behavior-specced; for now in the encoder,
     we select the maximum value of each band as representative (this
     helps make sure peaks don't go out of range.  In error terms,
     selecting min would make more sense, but the codebook is trained
     numerically, so we don't actually lose.  We'd still want to
     use the original curve for error and noise estimation */

  for(i=0;i<l->n;i++){
    int bark=l->linearmap[i];
    if(work[bark]<curve[i])work[bark]=curve[i];
    if(bark>last+1){
      /* If the bark scale is climbing rapidly, some bins may end up
         going unused.  This isn't a waste actually; it keeps the
         scale resolution even so that the LPC generator has an easy
         time.  However, if we leave the bins empty we lose energy.
         So, fill 'em in.  The decoder does not do anything with  he
         unused bins, so we can fill them anyway we like to end up
         with a better spectral curve */

      /* we'll always have a bin zero, so we don't need to guard init */
      long span=bark-last;
      for(j=1;j<span;j++){
	double del=(double)j/span;
	work[j+last]=work[bark]*del+work[last]*(1.-del);
      }
    }
    last=bark;
  }
  /*for(i=0;i<mapped;i++)work[i]*=l->barknorm[i];*/

  return vorbis_lpc_from_spectrum(work,lpc,l);
}


/* 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 _vlpc_de_helper(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 ;-) */

  {
    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]);
      curve[i]=(1./hypot(real+unit,imag));
    }
  }
}  

/* generate the whole freq response curve of an LPC IIR filter */

void vorbis_lpc_to_curve(double *curve,double *lpc,double amp,lpc_lookup *l){
  double *lcurve=alloca(sizeof(double)*(l->ln*2));
  int i;

  if(amp==0){
    memset(curve,0,sizeof(double)*l->n);
    return;
  }
  _vlpc_de_helper(lcurve,lpc,amp,l);

  /*for(i=0;i<l->ln;i++)lcurve[i]/=l->barknorm[i];*/
  for(i=0;i<l->n;i++)curve[i]=lcurve[l->linearmap[i]];

}

/* subtract or add an lpc filter to data.  Vorbis doesn't actually use this. */

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 */

  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];
    
    work[i+m]=data[i];
    data[i]-=y;
  }
}


void vorbis_lpc_predict(double *coeff,double *prime,int m,
                     double *data,long n){

  /* 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));

  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=data[i];
    o=i;
    p=m;
    for(j=0;j<m;j++)
      y-=work[o++]*coeff[--p];
    
    data[i]=work[o]=y;
  }
}