1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5 * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
6 * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: LPC low level routines
14 last mod: $Id: lpc.c,v 1.29 2001/02/02 03:51:56 xiphmont Exp $
16 ********************************************************************/
18 /* Some of these routines (autocorrelator, LPC coefficient estimator)
19 are derived from code written by Jutta Degener and Carsten Bormann;
20 thus we include their copyright below. The entirety of this file
21 is freely redistributable on the condition that both of these
22 copyright notices are preserved without modification. */
24 /* Preserved Copyright: *********************************************/
26 /* Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
27 Technische Universita"t Berlin
29 Any use of this software is permitted provided that this notice is not
30 removed and that neither the authors nor the Technische Universita"t
31 Berlin are deemed to have made any representations as to the
32 suitability of this software for any purpose nor are held responsible
33 for any defects of this software. THERE IS ABSOLUTELY NO WARRANTY FOR
36 As a matter of courtesy, the authors request to be informed about uses
37 this software has found, about bugs in this software, and about any
38 improvements that may be of general interest.
44 *********************************************************************/
55 /* Autocorrelation LPC coeff generation algorithm invented by
56 N. Levinson in 1947, modified by J. Durbin in 1959. */
58 /* Input : n elements of time doamin data
59 Output: m lpc coefficients, excitation energy */
61 float vorbis_lpc_from_data(float *data,float *lpc,int n,int m){
62 float *aut=alloca(sizeof(float)*(m+1));
66 /* autocorrelation, p+1 lag coefficients */
71 for(i=j;i<n;i++)d+=data[i]*data[i-j];
75 /* Generate lpc coefficients from autocorr values */
79 memset(lpc,0,m*sizeof(float));
86 /* Sum up this iteration's reflection coefficient; note that in
87 Vorbis we don't save it. If anyone wants to recycle this code
88 and needs reflection coefficients, save the results of 'r' from
91 for(j=0;j<i;j++)r-=lpc[j]*aut[i-j];
94 /* Update LPC coefficients and total error */
102 if(i%2)lpc[j]+=lpc[j]*r;
107 /* we need the error value to know how big an impulse to hit the
113 /* Input : n element envelope spectral curve
114 Output: m lpc coefficients, excitation energy */
116 float vorbis_lpc_from_curve(float *curve,float *lpc,lpc_lookup *l){
119 float *work=alloca(sizeof(float)*(n+n));
123 /* input is a real curve. make it complex-real */
124 /* This mixes phase, but the LPC generation doesn't care. */
126 work[i*2]=curve[i]*fscale;
129 work[n*2-1]=curve[n-1]*fscale;
132 drft_backward(&l->fft,work);
134 /* The autocorrelation will not be circular. Shift, else we lose
135 most of the power in the edges. */
137 for(i=0,j=n/2;i<n/2;){
143 /* we *could* shave speed here by skimping on the edges (thus
144 speeding up the autocorrelation in vorbis_lpc_from_data) but we
147 return(vorbis_lpc_from_data(work,lpc,n,m));
150 void lpc_init(lpc_lookup *l,long mapped, int m){
151 memset(l,0,sizeof(lpc_lookup));
156 /* we cheat decoding the LPC spectrum via FFTs */
157 drft_init(&l->fft,mapped*2);
161 void lpc_clear(lpc_lookup *l){
167 void vorbis_lpc_predict(float *coeff,float *prime,int m,
170 /* in: coeff[0...m-1] LPC coefficients
171 prime[0...m-1] initial values (allocated size of n+m-1)
172 out: data[0...n-1] data samples */
176 float *work=alloca(sizeof(float)*(m+n));
190 y-=work[o++]*coeff[--p];