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: LSP (also called LSF) conversion routines
15 last mod: $Id: lsp.c,v 1.5 2000/03/10 13:21:18 xiphmont Exp $
17 The LSP generation code is taken (with minimal modification) from
18 "On the Computation of the LSP Frequencies" by Joseph Rothweiler
19 <rothwlr@altavista.net>, available at:
21 http://www2.xtdl.com/~rothwlr/lsfpaper/lsfpage.html
23 ********************************************************************/
31 void vorbis_lsp_to_lpc(double *lsp,double *lpc,int m){
33 double *O=alloca(sizeof(double)*m2);
34 double *E=alloca(sizeof(double)*m2);
36 double *Ae=alloca(sizeof(double)*(m2+1));
37 double *Ao=alloca(sizeof(double)*(m2+1));
39 double *Be=alloca(sizeof(double)*(m2));
40 double *Bo=alloca(sizeof(double)*(m2));
43 /* even/odd roots setup */
45 O[i]=-2.*cos(lsp[i*2]);
46 E[i]=-2.*cos(lsp[i*2+1]);
49 /* set up impulse response */
59 /* run impulse response */
63 temp=O[j]*Ao[j]+Ae[j];
68 temp=E[j]*Bo[j]+Be[j];
73 lpc[i-1]=(A+Ao[j]+B-Ae[j])/2;
79 static void kw(double *r,int n) {
80 double *s=alloca(sizeof(double)*(n/2+1));
81 double *c=alloca(sizeof(double)*(n+1));
87 for(i=3;i<=n/2;i++) s[i] = s[i-2];
92 for(i=k+2;i<=n;i+=2) {
98 for(k=0;k<=n;k++) r[k] = c[k];
102 static int comp(const void *a,const void *b){
103 return(*(double *)a<*(double *)b);
106 /* CACM algorithm 283. */
107 static void cacm283(double *a,int ord,double *r){
109 double val, p, delta, error;
112 for(i=0; i<ord;i++) r[i] = 2.0 * (i+0.5) / ord - 1.0;
114 for(error=1 ; error > 1.e-12; ) {
116 for( i=0; i<ord; i++) { /* Update each point. */
120 for(k=ord-1; k>= 0; k--) {
121 val = val * rooti + a[k];
122 if (k != i) p *= rooti - r[k];
126 error += delta*delta;
130 /* Replaced the original bubble sort with a real sort. With your
131 help, we can eliminate the bubble sort in our lifetime. --Monty */
133 qsort(r,ord,sizeof(double),comp);
137 /* Convert lpc coefficients to lsp coefficients */
138 void vorbis_lpc_to_lsp(double *lpc,double *lsp,int m){
140 double *g1=alloca(sizeof(double)*(order2+1));
141 double *g2=alloca(sizeof(double)*(order2+1));
142 double *g1r=alloca(sizeof(double)*(order2+1));
143 double *g2r=alloca(sizeof(double)*(order2+1));
146 /* Compute the lengths of the x polynomials. */
147 /* Compute the first half of K & R F1 & F2 polynomials. */
148 /* Compute half of the symmetric and antisymmetric polynomials. */
149 /* Remove the roots at +1 and -1. */
152 for(i=0;i<order2;i++) g1[order2-i-1] = lpc[i]+lpc[m-i-1];
154 for(i=0;i<order2;i++) g2[order2-i-1] = lpc[i]-lpc[m-i-1];
156 for(i=0; i<order2;i++) g1[order2-i-1] -= g1[order2-i];
157 for(i=0; i<order2;i++) g2[order2-i-1] += g2[order2-i];
159 /* Convert into polynomials in cos(alpha) */
163 /* Find the roots of the 2 even polynomials.*/
165 cacm283(g1,order2,g1r);
166 cacm283(g2,order2,g2r);
169 lsp[i] = acos(g1r[i/2]*.5);
170 lsp[i+1] = acos(g2r[i/2]*.5);