1 /********************************************************************
3 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
6 * PLEASE READ THESE TERMS DISTRIBUTING. *
8 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
9 * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
10 * http://www.xiph.org/ *
12 ********************************************************************
14 function: LPC low level routines
15 last mod: $Id: lpc.c,v 1.10 1999/12/30 07:26:40 xiphmont Exp $
17 ********************************************************************/
19 /* Some of these routines (autocorrelator, LPC coefficient estimator)
20 are derived from code written by Jutta Degener and Carsten Bormann;
21 thus we include their copyright below. The entirety of this file
22 is freely redistributable on the condition that both of these
23 copyright notices are preserved without modification. */
25 /* Preserved Copyright: *********************************************/
27 /* Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
28 Technische Universita"t Berlin
30 Any use of this software is permitted provided that this notice is not
31 removed and that neither the authors nor the Technische Universita"t
32 Berlin are deemed to have made any representations as to the
33 suitability of this software for any purpose nor are held responsible
34 for any defects of this software. THERE IS ABSOLUTELY NO WARRANTY FOR
37 As a matter of courtesy, the authors request to be informed about uses
38 this software has found, about bugs in this software, and about any
39 improvements that may be of general interest.
45 *********************************************************************/
56 /* This is pared down for Vorbis. Autocorrelation LPC coeff generation
57 algorithm invented by N. Levinson in 1947, modified by J. Durbin in
60 /* Input : n elements of time doamin data
61 Output: m lpc coefficients, excitation energy */
64 double vorbis_lpc_from_data(double *data,double *lpc,int n,int m){
65 double *aut=alloca(sizeof(double)*(m+1));
69 /* autocorrelation, p+1 lag coefficients */
74 for(i=j;i<n;i++)d+=data[i]*data[i-j];
78 /* Generate lpc coefficients from autocorr values */
82 memset(lpc,0,m*sizeof(double));
89 /* Sum up this iteration's reflection coefficient; note that in
90 Vorbis we don't save it. If anyone wants to recycle this code
91 and needs reflection coefficients, save the results of 'r' from
94 for(j=0;j<i;j++)r-=lpc[j]*aut[i-j];
97 /* Update LPC coefficients and total error */
102 lpc[j]+=r*lpc[i-1-j];
105 if(i%2)lpc[j]+=lpc[j]*r;
110 /* we need the error value to know how big an impulse to hit the
116 /* Input : n element envelope spectral curve
117 Output: m lpc coefficients, excitation energy */
119 double vorbis_lpc_from_spectrum(double *curve,double *lpc,lpc_lookup *l){
122 double *work=alloca(sizeof(double)*(n+n));
126 /* input is a real curve. make it complex-real */
127 /* This mixes phase, but the LPC generation doesn't care. */
129 work[i*2]=curve[i]*fscale;
134 drft_backward(&l->fft,work);
136 /* The autocorrelation will not be circular. Shift, else we lose
137 most of the power in the edges. */
139 for(i=0,j=n/2;i<n/2;){
145 return(vorbis_lpc_from_data(work,lpc,n,m));
148 /* On top of this basic LPC infrastructure we introduce two modifications:
150 1) Filter generation is limited in the resolution of features it
151 can represent (this is more obvious when the filter is looked at as
152 a set of LSP coefficients). Human perception of the audio spectrum
153 is logarithmic not only in amplitude, but also frequency. Because
154 the high frequency features we'll need to encode will be broader
155 than the low frequency features, filter generation will be
156 dominated by higher frequencies (when most of the energy is in the
157 lowest frequencies, and greatest perceived resolution is in the
158 midrange). To avoid this effect, Vorbis encodes the frequency
159 spectrum with a biased log frequency scale. The intent is to
160 roughly equalize the sizes of the octaves (see xlogmap.h)
162 2) When we change the frequency scale, we also change the
163 (apparent) relative energies of the bands; that is, on a log scale
164 covering 5 octaves, the highest octave goes from being represented
165 in half the bins, to only 1/32 of the bins. If the amplitudes
166 remain the same, we have divided the energy represented by the
167 highest octave by 16 (as far as Levinson-Durbin is concerned).
168 This will seriously skew filter generation, which bases calculation
169 on the mean square error with respect to energy. Thus, Vorbis
170 normalizes the amplitudes of the log spectrum frequencies to keep
171 the relative octave energies correct. */
173 /* n == size of vector to be used for filter, m == order of filter,
174 oct == octaves in normalized scale, encode_p == encode (1) or
177 void lpc_init(lpc_lookup *l,int n, int mapped, int m, int oct, int encode_p){
178 double bias=LOG_BIAS(n,oct);
179 double scale=(float)mapped/(float)oct; /* where n==mapped */
182 memset(l,0,sizeof(lpc_lookup));
187 l->iscale=malloc(n*sizeof(int));
188 l->ifrac=malloc(n*sizeof(double));
189 l->norm=malloc(n*sizeof(double));
192 /* how much 'real estate' in the log domain does the bin in the
193 linear domain represent? */
194 double logA=LOG_X(i,bias);
195 double logB=LOG_X(i+1.,bias);
196 l->norm[i]=logB-logA; /* this much */
199 /* the scale is encode/decode specific for algebraic simplicity */
203 l->escale=malloc(mapped*sizeof(double));
204 l->uscale=malloc(n*sizeof(int));
206 /* undersample guard */
208 l->uscale[i]=rint(LOG_X(i,bias)/oct*mapped);
211 for(i=0;i<mapped;i++){
212 l->escale[i]=LINEAR_X(i/scale,bias);
213 l->uscale[(int)(floor(l->escale[i]))]=-1;
214 l->uscale[(int)(ceil(l->escale[i]))]=-1;
219 /* decode; encode may use this too */
221 drft_init(&l->fft,mapped*2);
223 double is=LOG_X(i,bias)/oct*mapped;
226 l->iscale[i]=floor(is);
227 if(l->iscale[i]>=l->ln-1)l->iscale[i]=l->ln-2;
229 l->ifrac[i]=is-floor(is);
230 if(l->ifrac[i]>1.)l->ifrac[i]=1.;
235 void lpc_clear(lpc_lookup *l){
237 if(l->escale)free(l->escale);
246 /* less efficient than the decode side (written for clarity). We're
247 not bottlenecked here anyway */
249 double vorbis_curve_to_lpc(double *curve,double *lpc,lpc_lookup *l){
250 /* map the input curve to a log curve for encoding */
252 /* for clarity, mapped and n are both represented although setting
253 'em equal is a decent rule of thumb. The below must be reworked
254 slightly if mapped != n */
257 double *work=alloca(sizeof(double)*mapped);
260 /* fairly correct for low frequencies, naieve for high frequencies
261 (suffers from undersampling) */
262 for(i=0;i<mapped;i++){
263 double lin=l->escale[i];
266 double del=lin-floor(lin);
268 work[i]=(curve[a]/l->norm[a]*(1.-del)+
269 curve[b]/l->norm[b]*del);
273 /* for(i=0;i<l->n;i++)
275 if(work[l->uscale[i]]<curve[i])work[l->uscale[i]]=curve[i];*/
282 static int frameno=0;
284 sprintf(buffer,"preloglpc%d.m",frameno++);
285 out=fopen(buffer,"w+");
287 fprintf(out,"%g\n",work[j]);
292 return vorbis_lpc_from_spectrum(work,lpc,l);
296 /* One can do this the long way by generating the transfer function in
297 the time domain and taking the forward FFT of the result. The
298 results from direct calculation are cleaner and faster.
300 This version does a linear curve generation and then later
301 interpolates the log curve from the linear curve. This could stand
302 optimization; it could both be more precise as well as not compute
303 quite a few unused values */
305 void _vlpc_de_helper(double *curve,double *lpc,double amp,
308 memset(curve,0,sizeof(double)*l->ln*2);
312 curve[i*2+1]=lpc[i]/4/amp;
313 curve[i*2+2]=-lpc[i]/4/amp;
316 drft_backward(&l->fft,curve); /* reappropriated ;-) */
321 curve[0]=(1./(curve[0]*2+unit));
322 for(i=1;i<l->ln;i++){
323 double real=(curve[i]+curve[l2-i]);
324 double imag=(curve[i]-curve[l2-i]);
325 curve[i]=(1./hypot(real+unit,imag));
331 /* generate the whole freq response curve on an LPC IIR filter */
333 void vorbis_lpc_to_curve(double *curve,double *lpc,double amp,lpc_lookup *l){
334 double *lcurve=alloca(sizeof(double)*(l->ln*2));
337 _vlpc_de_helper(lcurve,lpc,amp,l);
344 static int frameno=0;
346 sprintf(buffer,"loglpc%d.m",frameno++);
347 out=fopen(buffer,"w+");
349 fprintf(out,"%g\n",lcurve[j]);
358 curve[i]=((1.-l->ifrac[i])*lcurve[ii]+
359 l->ifrac[i]*lcurve[ii+1])*l->norm[i];
364 void vorbis_lpc_apply(double *residue,double *lpc,double amp,lpc_lookup *l){
365 double *lcurve=alloca(sizeof(double)*((l->ln+l->n)*2));
369 memset(residue,0,l->n*sizeof(double));
372 _vlpc_de_helper(lcurve,lpc,amp,l);
377 residue[i]*=((1.-l->ifrac[i])*lcurve[ii]+
378 l->ifrac[i]*lcurve[ii+1])*l->norm[i];
384 /* subtract or add an lpc filter to data. Vorbis doesn't actually use this. */
386 void vorbis_lpc_residue(double *coeff,double *prime,int m,
387 double *data,long n){
389 /* in: coeff[0...m-1] LPC coefficients
390 prime[0...m-1] initial values
391 data[0...n-1] data samples
392 out: data[0...n-1] residuals from LPC prediction */
395 double *work=alloca(sizeof(double)*(m+n));
408 y-=work[i+j]*coeff[m-j-1];
416 void vorbis_lpc_predict(double *coeff,double *prime,int m,
417 double *data,long n){
419 /* in: coeff[0...m-1] LPC coefficients
420 prime[0...m-1] initial values (allocated size of n+m-1)
421 data[0...n-1] residuals from LPC prediction
422 out: data[0...n-1] data samples */
426 double *work=alloca(sizeof(double)*(m+n));
440 y-=work[o++]*coeff[--p];