Debian Codegraph @

external/vorbis/vorbis_lpc.cpp

raw
1 /********************************************************************
2 * *
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
7 * *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
9 * by the Xiph.Org Foundation https://xiph.org/ *
10 * *
11 ********************************************************************
12
13 function: LPC low level routines
14
15 ********************************************************************/
16
17 /* Some of these routines (autocorrelator, LPC coefficient estimator)
18 are derived from code written by Jutta Degener and Carsten Bormann;
19 thus we include their copyright below. The entirety of this file
20 is freely redistributable on the condition that both of these
21 copyright notices are preserved without modification. */
22
23 /* Preserved Copyright: *********************************************/
24
25 /* Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
26 Technische Universita"t Berlin
27
28 Any use of this software is permitted provided that this notice is not
29 removed and that neither the authors nor the Technische Universita"t
30 Berlin are deemed to have made any representations as to the
31 suitability of this software for any purpose nor are held responsible
32 for any defects of this software. THERE IS ABSOLUTELY NO WARRANTY FOR
33 THIS SOFTWARE.
34
35 As a matter of courtesy, the authors request to be informed about uses
36 this software has found, about bugs in this software, and about any
37 improvements that may be of general interest.
38
39 Berlin, 28.11.1994
40 Jutta Degener
41 Carsten Bormann
42
43 *********************************************************************/
44
45 #include "vorbis_os.h"
46 #include "vorbis_smallft.h"
47 #include "vorbis_lpc.h"
48 #include "vorbis_scales.h"
49 #include "vorbis_misc.h"
50
51 /* Autocorrelation LPC coeff generation algorithm invented by
52 N. Levinson in 1947, modified by J. Durbin in 1959. */
53
54 /* Input : n elements of time doamin data
55 Output: m lpc coefficients, excitation energy */
56
57 float vorbis_lpc_from_data(float *data,float *lpci,int n,int m){
58 double *aut = (double *) alloca(sizeof(*aut)*(m+1));
59 double *lpc = (double *) alloca(sizeof(*lpc)*(m));
60 double error;
61 double epsilon;
62 int i,j;
63
64 /* autocorrelation, p+1 lag coefficients */
65 j=m+1;
66 while(j--){
67 double d=0; /* double needed for accumulator depth */
68 for(i=j;i<n;i++)d+=(double)data[i]*data[i-j];
69 aut[j]=d;
70 }
71
72 /* Generate lpc coefficients from autocorr values */
73
74 /* set our noise floor to about -100dB */
75 error=aut[0] * (1. + 1e-10);
76 epsilon=1e-9*aut[0]+1e-10;
77
78 for(i=0;i<m;i++){
79 double r= -aut[i+1];
80
81 if(error<epsilon){
82 memset(lpc+i,0,(m-i)*sizeof(*lpc));
83 goto done;
84 }
85
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
89 each iteration. */
90
91 for(j=0;j<i;j++)r-=lpc[j]*aut[i-j];
92 r/=error;
93
94 /* Update LPC coefficients and total error */
95
96 lpc[i]=r;
97 for(j=0;j<i/2;j++){
98 double tmp=lpc[j];
99
100 lpc[j]+=r*lpc[i-1-j];
101 lpc[i-1-j]+=r*tmp;
102 }
103 if(i&1)lpc[j]+=lpc[j]*r;
104
105 error*=1.-r*r;
106
107 }
108
109 done:
110
111 /* slightly damp the filter */
112 {
113 double g = .99;
114 double damp = g;
115 for(j=0;j<m;j++){
116 lpc[j]*=damp;
117 damp*=g;
118 }
119 }
120
121 for(j=0;j<m;j++)lpci[j]=(float)lpc[j];
122
123 /* we need the error value to know how big an impulse to hit the
124 filter with later */
125
126 return error;
127 }
128
129 void vorbis_lpc_predict(float *coeff,float *prime,int m,
130 float *data,long n){
131
132 /* in: coeff[0...m-1] LPC coefficients
133 prime[0...m-1] initial values (allocated size of n+m-1)
134 out: data[0...n-1] data samples */
135
136 long i,j,o,p;
137 float y;
138 float *work = (float *) alloca(sizeof(*work)*(m+n));
139
140 if(!prime)
141 for(i=0;i<m;i++)
142 work[i]=0.f;
143 else
144 for(i=0;i<m;i++)
145 work[i]=prime[i];
146
147 for(i=0;i<n;i++){
148 y=0;
149 o=i;
150 p=m;
151 for(j=0;j<m;j++)
152 y-=work[o++]*coeff[--p];
153
154 data[i]=work[o]=y;
155 }
156 }