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-1999 *
9 * by 1999 Monty <monty@xiph.org> and The XIPHOPHORUS Company *
10 * http://www.xiph.org/ *
12 ********************************************************************
14 function: LPC low level routines
15 author: Monty <monty@xiph.org>
16 modifications by: Monty
17 last modification date: Nov 16 1999
19 ********************************************************************/
21 /* Some of these routines (autocorrelator, LPC coefficient estimator)
22 are derived from code written by Jutta Degener and Carsten Bormann;
23 thus we include their copyright below. The entirety of this file
24 is freely redistributable on the condition that both of these
25 copyright notices are preserved without modification. */
27 /* Preserved Copyright: *********************************************/
29 /* Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
30 Technische Universita"t Berlin
32 Any use of this software is permitted provided that this notice is not
33 removed and that neither the authors nor the Technische Universita"t
34 Berlin are deemed to have made any representations as to the
35 suitability of this software for any purpose nor are held responsible
36 for any defects of this software. THERE IS ABSOLUTELY NO WARRANTY FOR
39 As a matter of courtesy, the authors request to be informed about uses
40 this software has found, about bugs in this software, and about any
41 improvements that may be of general interest.
47 *********************************************************************/
58 /* This is pared down for Vorbis. Autocorrelation LPC coeff generation
59 algorithm invented by N. Levinson in 1947, modified by J. Durbin in
62 /* Input : n elements of time doamin data
63 Output: m lpc coefficients, excitation energy */
66 double vorbis_lpc_from_data(double *data,double *lpc,int n,int m){
67 double *aut=alloca(sizeof(double)*(m+1));
71 /* autocorrelation, p+1 lag coefficients */
76 for(i=j;i<n;i++)d+=data[i]*data[i-j];
80 /* Generate lpc coefficients from autocorr values */
84 memset(lpc,0,m*sizeof(double));
91 /* Sum up this iteration's reflection coefficient; note that in
92 Vorbis we don't save it. If anyone wants to recycle this code
93 and needs reflection coefficients, save the results of 'r' from
96 for(j=0;j<i;j++)r-=lpc[j]*aut[i-j];
99 /* Update LPC coefficients and total error */
104 lpc[j]+=r*lpc[i-1-j];
107 if(i%2)lpc[j]+=lpc[j]*r;
112 /* we need the error value to know how big an impulse to hit the
118 /* Input : n element envelope spectral curve
119 Output: m lpc coefficients, excitation energy */
121 double vorbis_lpc_from_spectrum(double *curve,double *lpc,lpc_lookup *l){
124 double *work=alloca(sizeof(double)*(n+n));
128 /* input is a real curve. make it complex-real */
129 /* This mixes phase, but the LPC generation doesn't care. */
131 work[i*2]=curve[i]*fscale;
136 drft_backward(&l->fft,work);
138 /* The autocorrelation will not be circular. Shift, else we lose
139 most of the power in the edges. */
141 for(i=0,j=n/2;i<n/2;){
147 return(vorbis_lpc_from_data(work,lpc,n,m));
150 /* On top of this basic LPC infrastructure we introduce two modifications:
152 1) Filter generation is limited in the resolution of features it
153 can represent (this is more obvious when the filter is looked at as
154 a set of LSP coefficients). Human perception of the audio spectrum
155 is logarithmic not only in amplitude, but also frequency. Because
156 the high frequency features we'll need to encode will be broader
157 than the low frequency features, filter generation will be
158 dominated by higher frequencies (when most of the energy is in the
159 lowest frequencies, and greatest perceived resolution is in the
160 midrange). To avoid this effect, Vorbis encodes the frequency
161 spectrum with a biased log frequency scale. The intent is to
162 roughly equalize the sizes of the octaves (see xlogmap.h)
164 2) When we change the frequency scale, we also change the
165 (apparent) relative energies of the bands; that is, on a log scale
166 covering 5 octaves, the highest octave goes from being represented
167 in half the bins, to only 1/32 of the bins. If the amplitudes
168 remain the same, we have divided the energy represented by the
169 highest octave by 16 (as far as Levinson-Durbin is concerned).
170 This will seriously skew filter generation, which bases calculation
171 on the mean square error with respect to energy. Thus, Vorbis
172 normalizes the amplitudes of the log spectrum frequencies to keep
173 the relative octave energies correct. */
175 /* n == size of vector to be used for filter, m == order of filter,
176 oct == octaves in normalized scale, encode_p == encode (1) or
179 void lpc_init(lpc_lookup *l,int n, int mapped, int m, int oct, int encode_p){
180 double bias=LOG_BIAS(n,oct);
181 double scale=(float)mapped/(float)oct; /* where n==mapped */
184 memset(l,0,sizeof(lpc_lookup));
189 l->iscale=malloc(n*sizeof(int));
190 l->ifrac=malloc(n*sizeof(double));
191 l->norm=malloc(n*sizeof(double));
194 /* how much 'real estate' in the log domain does the bin in the
195 linear domain represent? */
196 double logA=LOG_X(i,bias);
197 double logB=LOG_X(i+1.,bias);
198 l->norm[i]=logB-logA; /* this much */
201 /* the scale is encode/decode specific for algebraic simplicity */
205 l->escale=malloc(mapped*sizeof(double));
206 l->uscale=malloc(n*sizeof(int));
208 /* undersample guard */
210 l->uscale[i]=rint(LOG_X(i,bias)/oct*mapped);
213 for(i=0;i<mapped;i++){
214 l->escale[i]=LINEAR_X(i/scale,bias);
215 l->uscale[(int)(floor(l->escale[i]))]=-1;
216 l->uscale[(int)(ceil(l->escale[i]))]=-1;
221 /* decode; encode may use this too */
223 drft_init(&l->fft,mapped*2);
225 double is=LOG_X(i,bias)/oct*mapped;
228 l->iscale[i]=floor(is);
229 if(l->iscale[i]>=l->ln-1)l->iscale[i]=l->ln-2;
231 l->ifrac[i]=is-floor(is);
232 if(l->ifrac[i]>1.)l->ifrac[i]=1.;
237 void lpc_clear(lpc_lookup *l){
239 if(l->escale)free(l->escale);
248 /* less efficient than the decode side (written for clarity). We're
249 not bottlenecked here anyway */
251 double vorbis_curve_to_lpc(double *curve,double *lpc,lpc_lookup *l){
252 /* map the input curve to a log curve for encoding */
254 /* for clarity, mapped and n are both represented although setting
255 'em equal is a decent rule of thumb. The below must be reworked
256 slightly if mapped != n */
259 double *work=alloca(sizeof(double)*mapped);
262 /* fairly correct for low frequencies, naieve for high frequencies
263 (suffers from undersampling) */
264 for(i=0;i<mapped;i++){
265 double lin=l->escale[i];
268 double del=lin-floor(lin);
270 work[i]=(curve[a]/l->norm[a]*(1.-del)+
271 curve[b]/l->norm[b]*del);
275 /* for(i=0;i<l->n;i++)
277 if(work[l->uscale[i]]<curve[i])work[l->uscale[i]]=curve[i];*/
284 static int frameno=0;
286 sprintf(buffer,"preloglpc%d.m",frameno++);
287 out=fopen(buffer,"w+");
289 fprintf(out,"%g\n",work[j]);
294 return vorbis_lpc_from_spectrum(work,lpc,l);
298 /* One can do this the long way by generating the transfer function in
299 the time domain and taking the forward FFT of the result. The
300 results from direct calculation are cleaner and faster.
302 This version does a linear curve generation and then later
303 interpolates the log curve from the linear curve. This could stand
304 optimization; it could both be more precise as well as not compute
305 quite a few unused values */
307 void _vlpc_de_helper(double *curve,double *lpc,double amp,
310 memset(curve,0,sizeof(double)*l->ln*2);
314 curve[i*2+1]=lpc[i]/4/amp;
315 curve[i*2+2]=-lpc[i]/4/amp;
318 drft_backward(&l->fft,curve); /* reappropriated ;-) */
323 curve[0]=(1./(curve[0]*2+unit));
324 for(i=1;i<l->ln;i++){
325 double real=(curve[i]+curve[l2-i]);
326 double imag=(curve[i]-curve[l2-i]);
327 curve[i]=(1./hypot(real+unit,imag));
333 /* generate the whole freq response curve on an LPC IIR filter */
335 void vorbis_lpc_to_curve(double *curve,double *lpc,double amp,lpc_lookup *l){
336 double *lcurve=alloca(sizeof(double)*(l->ln*2));
339 _vlpc_de_helper(lcurve,lpc,amp,l);
346 static int frameno=0;
348 sprintf(buffer,"loglpc%d.m",frameno++);
349 out=fopen(buffer,"w+");
351 fprintf(out,"%g\n",lcurve[j]);
360 curve[i]=((1.-l->ifrac[i])*lcurve[ii]+
361 l->ifrac[i]*lcurve[ii+1])*l->norm[i];
366 void vorbis_lpc_apply(double *residue,double *lpc,double amp,lpc_lookup *l){
367 double *lcurve=alloca(sizeof(double)*((l->ln+l->n)*2));
371 memset(residue,0,l->n*sizeof(double));
374 _vlpc_de_helper(lcurve,lpc,amp,l);
379 residue[i]*=((1.-l->ifrac[i])*lcurve[ii]+
380 l->ifrac[i]*lcurve[ii+1])*l->norm[i];
386 /* subtract or add an lpc filter to data. Vorbis doesn't actually use this. */
388 void vorbis_lpc_residue(double *coeff,double *prime,int m,
389 double *data,long n){
391 /* in: coeff[0...m-1] LPC coefficients
392 prime[0...m-1] initial values
393 data[0...n-1] data samples
394 out: data[0...n-1] residuals from LPC prediction */
397 double *work=alloca(sizeof(double)*(m+n));
410 y-=work[i+j]*coeff[m-j-1];
418 void vorbis_lpc_predict(double *coeff,double *prime,int m,
419 double *data,long n){
421 /* in: coeff[0...m-1] LPC coefficients
422 prime[0...m-1] initial values (allocated size of n+m-1)
423 data[0...n-1] residuals from LPC prediction
424 out: data[0...n-1] data samples */
428 double *work=alloca(sizeof(double)*(m+n));
442 y-=work[o++]*coeff[--p];