lsp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /********************************************************************
  2. * *
  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. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and the XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: LSP (also called LSF) conversion routines
  14. last mod: $Id: lsp.c,v 1.10.2.4 2000/11/04 06:21:44 xiphmont Exp $
  15. The LSP generation code is taken (with minimal modification) from
  16. "On the Computation of the LSP Frequencies" by Joseph Rothweiler
  17. <rothwlr@altavista.net>, available at:
  18. http://www2.xtdl.com/~rothwlr/lsfpaper/lsfpage.html
  19. ********************************************************************/
  20. /* Note that the lpc-lsp conversion finds the roots of polynomial with
  21. an iterative root polisher (CACM algorithm 283). It *is* possible
  22. to confuse this algorithm into not converging; that should only
  23. happen with absurdly closely spaced roots (very sharp peaks in the
  24. LPC f response) which in turn should be impossible in our use of
  25. the code. If this *does* happen anyway, it's a bug in the floor
  26. finder; find the cause of the confusion (probably a single bin
  27. spike or accidental near-float-limit resolution problems) and
  28. correct it. */
  29. #include <math.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include "lsp.h"
  33. #include "os.h"
  34. #include "misc.h"
  35. #include "lookup.h"
  36. #include "scales.h"
  37. /* three possible LSP to f curve functions; the exact computation
  38. (float), a lookup based float implementation, and an integer
  39. implementation. The float lookup is likely the optimal choice on
  40. any machine with an FPU. The integer implementation is *not* fixed
  41. point (due to the need for a large dynamic range and thus a
  42. seperately tracked exponent) and thus much more complex than the
  43. relatively simple float implementations. It's mostly for future
  44. work on a fully fixed point implementation for processors like the
  45. ARM family. */
  46. /* undefine both for the 'old' but more precise implementation */
  47. #define FLOAT_LOOKUP
  48. #undef INT_LOOKUP
  49. #ifdef FLOAT_LOOKUP
  50. #include "lookup.c" /* catch this in the build system; we #include for
  51. compilers (like gcc) that can't inline across
  52. modules */
  53. /* side effect: changes *lsp to cosines of lsp */
  54. void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
  55. float amp,float ampoffset){
  56. int i;
  57. float wdel=M_PI/ln;
  58. vorbis_fpu_control fpu;
  59. vorbis_fpu_setround(&fpu);
  60. for(i=0;i<m;i++)lsp[i]=vorbis_coslook(lsp[i]);
  61. i=0;
  62. while(i<n){
  63. int k=map[i];
  64. int qexp;
  65. float p=.7071067812;
  66. float q=.7071067812;
  67. float w=vorbis_coslook(wdel*k);
  68. float *ftmp=lsp;
  69. int c=m>>1;
  70. do{
  71. p*=ftmp[0]-w;
  72. q*=ftmp[1]-w;
  73. ftmp+=2;
  74. }while(--c);
  75. q=frexp(p*p*(1.+w)+q*q*(1.-w),&qexp);
  76. q=vorbis_fromdBlook(amp*
  77. vorbis_invsqlook(q)*
  78. vorbis_invsq2explook(qexp+m)-
  79. ampoffset);
  80. do{
  81. curve[i++]=q;
  82. }while(map[i]==k);
  83. }
  84. vorbis_fpu_restore(fpu);
  85. }
  86. #else
  87. #ifdef INT_LOOKUP
  88. #include "lookup.c" /* catch this in the build system; we #include for
  89. compilers (like gcc) that can't inline across
  90. modules */
  91. static int MLOOP_1[64]={
  92. 0,10,11,11, 12,12,12,12, 13,13,13,13, 13,13,13,13,
  93. 14,14,14,14, 14,14,14,14, 14,14,14,14, 14,14,14,14,
  94. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  95. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  96. };
  97. static int MLOOP_2[64]={
  98. 0,4,5,5, 6,6,6,6, 7,7,7,7, 7,7,7,7,
  99. 8,8,8,8, 8,8,8,8, 8,8,8,8, 8,8,8,8,
  100. 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,
  101. 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,
  102. };
  103. static int MLOOP_3[8]={0,1,2,2,3,3,3,3};
  104. /* side effect: changes *lsp to cosines of lsp */
  105. void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
  106. float amp,float ampoffset){
  107. /* 0 <= m < 256 */
  108. /* set up for using all int later */
  109. int i;
  110. int ampoffseti=rint(ampoffset*4096.);
  111. int ampi=rint(amp*16.);
  112. long *ilsp=alloca(m*sizeof(long));
  113. for(i=0;i<m;i++)ilsp[i]=vorbis_coslook_i(lsp[i]/M_PI*65536.+.5);
  114. i=0;
  115. while(i<n){
  116. int j,k=map[i];
  117. unsigned long pi=46341; /* 2**-.5 in 0.16 */
  118. unsigned long qi=46341;
  119. int qexp=0,shift;
  120. long wi=vorbis_coslook_i(k*65536/ln);
  121. pi*=labs(ilsp[0]-wi);
  122. qi*=labs(ilsp[1]-wi);
  123. for(j=2;j<m;j+=2){
  124. if(!(shift=MLOOP_1[(pi|qi)>>25]))
  125. if(!(shift=MLOOP_2[(pi|qi)>>19]))
  126. shift=MLOOP_3[(pi|qi)>>16];
  127. pi=(pi>>shift)*labs(ilsp[j]-wi);
  128. qi=(qi>>shift)*labs(ilsp[j+1]-wi);
  129. qexp+=shift;
  130. }
  131. if(!(shift=MLOOP_1[(pi|qi)>>25]))
  132. if(!(shift=MLOOP_2[(pi|qi)>>19]))
  133. shift=MLOOP_3[(pi|qi)>>16];
  134. pi>>=shift;
  135. qi>>=shift;
  136. qexp+=shift-7*m;
  137. /* pi,qi normalized collectively, both tracked using qexp */
  138. /* p*=p(1-w), q*=q(1+w), let normalization drift because it isn't
  139. worth tracking step by step */
  140. pi=((pi*pi)>>16);
  141. qi=((qi*qi)>>16);
  142. qexp=qexp*2+m;
  143. qi*=(1<<14)-wi;
  144. pi*=(1<<14)+wi;
  145. qi=(qi+pi)>>14;
  146. /* we've let the normalization drift because it wasn't important;
  147. however, for the lookup, things must be normalized again. We
  148. need at most one right shift or a number of left shifts */
  149. if(qi&0xffff0000){ /* checks for 1.xxxxxxxxxxxxxxxx */
  150. qi>>=1; qexp++;
  151. }else
  152. while(qi && !(qi&0x8000)){ /* checks for 0.0xxxxxxxxxxxxxxx or less*/
  153. qi<<=1; qexp--;
  154. }
  155. amp=vorbis_fromdBlook_i(ampi* /* n.4 */
  156. vorbis_invsqlook_i(qi,qexp)-
  157. /* m.8, m+n<=8 */
  158. ampoffseti); /* 8.12[0] */
  159. curve[i]=amp;
  160. while(map[++i]==k)curve[i]=amp;
  161. }
  162. }
  163. #else
  164. /* old, nonoptimized but simple version for any poor sap who needs to
  165. figure out what the hell this code does, or wants the other tiny
  166. fraction of a dB precision */
  167. /* side effect: changes *lsp to cosines of lsp */
  168. void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
  169. float amp,float ampoffset){
  170. int i;
  171. float wdel=M_PI/ln;
  172. for(i=0;i<m;i++)lsp[i]=2*cos(lsp[i]);
  173. i=0;
  174. while(i<n){
  175. int j,k=map[i];
  176. float p=.5;
  177. float q=.5;
  178. float w=2*cos(wdel*k);
  179. for(j=0;j<m;j+=2){
  180. p *= w-lsp[j];
  181. q *= w-lsp[j+1];
  182. }
  183. p*=p*(2.+w);
  184. q*=q*(2.-w);
  185. q=fromdB(amp/sqrt(p+q)-ampoffset);
  186. curve[i]=q;
  187. while(map[++i]==k)curve[i]=q;
  188. }
  189. }
  190. #endif
  191. #endif
  192. static void cheby(float *g, int ord) {
  193. int i, j;
  194. g[0] *= 0.5;
  195. for(i=2; i<= ord; i++) {
  196. for(j=ord; j >= i; j--) {
  197. g[j-2] -= g[j];
  198. g[j] += g[j];
  199. }
  200. }
  201. }
  202. static int comp(const void *a,const void *b){
  203. if(*(float *)a<*(float *)b)
  204. return(1);
  205. else
  206. return(-1);
  207. }
  208. /* This is one of those 'mathemeticians should not write code' kind of
  209. cases. Newton's method of polishing roots is straightforward
  210. enough... except in those cases where it just fails in the real
  211. world. In our case below, we're worried about a local mini/maxima
  212. shooting a root estimation off to infinity, or the new estimation
  213. chaotically oscillating about convergence (shouldn't actually be a
  214. problem in our usage.
  215. Maehly's modification (zero suppression, to prevent two tenative
  216. roots from collapsing to the same actual root) similarly can
  217. temporarily shoot a root off toward infinity. It would come
  218. back... if it were not for the fact that machine representation has
  219. limited dynamic range and resolution. This too is guarded by
  220. limiting delta.
  221. Last problem is convergence criteria; we don't know what a 'double'
  222. is on our hardware/compiler, and the convergence limit is bounded
  223. by roundoff noise. So, we hack convergence:
  224. Require at most 1e-6 mean squared error for all zeroes. When
  225. converging, start the clock ticking at 1e-6; limit our polishing to
  226. as many more iterations as took us to get this far, 100 max.
  227. Past max iters, quit when MSE is no longer decreasing *or* we go
  228. below ~1e-20 MSE, whichever happens first. */
  229. static void Newton_Raphson_Maehly(float *a,int ord,float *r){
  230. int i, k, count=0, maxiter=0;
  231. double error=1.,besterror=1.;
  232. double *root=alloca(ord*sizeof(double));
  233. for(i=0; i<ord;i++) root[i] = 2.0 * (i+0.5) / ord - 1.0;
  234. while(error>1.e-20){
  235. error=0;
  236. for(i=0; i<ord; i++) { /* Update each point. */
  237. double ac=0.,pp=0.,delta;
  238. double rooti=root[i];
  239. double p=a[ord];
  240. for(k=ord-1; k>= 0; k--) {
  241. pp= pp* rooti + p;
  242. p = p * rooti+ a[k];
  243. if (k != i) ac += 1./(rooti - root[k]);
  244. }
  245. ac=p*ac;
  246. delta = p/(pp-ac);
  247. /* don't allow the correction to scream off into infinity if we
  248. happened to polish right at a local mini/maximum */
  249. if(delta<-3)delta=-3;
  250. if(delta>3.)delta=3.; /* 3 is not a random choice; it's large
  251. enough to make sure the first pass
  252. can't accidentally limit two poles to
  253. the same value in a fatal nonelastic
  254. collision. */
  255. root[i] -= delta;
  256. error += delta*delta;
  257. }
  258. if(maxiter && count>maxiter && error>=besterror)break;
  259. /* anything to help out the polisher; converge using doubles */
  260. if(!count || error<besterror){
  261. for(i=0; i<ord; i++) r[i]=root[i];
  262. besterror=error;
  263. if(error<1.e-6){ /* rough minimum criteria */
  264. maxiter=count*2+10;
  265. if(maxiter>100)maxiter=100;
  266. }
  267. }
  268. count++;
  269. }
  270. /* Replaced the original bubble sort with a real sort. With your
  271. help, we can eliminate the bubble sort in our lifetime. --Monty */
  272. qsort(r,ord,sizeof(float),comp);
  273. }
  274. /* Convert lpc coefficients to lsp coefficients */
  275. void vorbis_lpc_to_lsp(float *lpc,float *lsp,int m){
  276. int order2=m/2;
  277. float *g1=alloca(sizeof(float)*(order2+1));
  278. float *g2=alloca(sizeof(float)*(order2+1));
  279. float *g1r=alloca(sizeof(float)*(order2+1));
  280. float *g2r=alloca(sizeof(float)*(order2+1));
  281. int i;
  282. /* Compute the lengths of the x polynomials. */
  283. /* Compute the first half of K & R F1 & F2 polynomials. */
  284. /* Compute half of the symmetric and antisymmetric polynomials. */
  285. /* Remove the roots at +1 and -1. */
  286. g1[order2] = 1.0;
  287. for(i=0;i<order2;i++) g1[order2-i-1] = lpc[i]+lpc[m-i-1];
  288. g2[order2] = 1.0;
  289. for(i=0;i<order2;i++) g2[order2-i-1] = lpc[i]-lpc[m-i-1];
  290. for(i=0; i<order2;i++) g1[order2-i-1] -= g1[order2-i];
  291. for(i=0; i<order2;i++) g2[order2-i-1] += g2[order2-i];
  292. /* Convert into polynomials in cos(alpha) */
  293. cheby(g1,order2);
  294. cheby(g2,order2);
  295. /* Find the roots of the 2 even polynomials.*/
  296. Newton_Raphson_Maehly(g1,order2,g1r);
  297. Newton_Raphson_Maehly(g2,order2,g2r);
  298. for(i=0;i<m;i+=2){
  299. lsp[i] = acos(g1r[i/2]);
  300. lsp[i+1] = acos(g2r[i/2]);
  301. }
  302. }