lsp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. ********************************************************************
  11. function: LSP (also called LSF) conversion routines
  12. last mod: $Id: lsp.c,v 1.18 2001/06/15 21:15:39 xiphmont Exp $
  13. The LSP generation code is taken (with minimal modification and a
  14. few bugfixes) from "On the Computation of the LSP Frequencies" by
  15. Joseph Rothweiler <rothwlr@altavista.net>, available at:
  16. http://www2.xtdl.com/~rothwlr/lsfpaper/lsfpage.html
  17. ********************************************************************/
  18. /* Note that the lpc-lsp conversion finds the roots of polynomial with
  19. an iterative root polisher (CACM algorithm 283). It *is* possible
  20. to confuse this algorithm into not converging; that should only
  21. happen with absurdly closely spaced roots (very sharp peaks in the
  22. LPC f response) which in turn should be impossible in our use of
  23. the code. If this *does* happen anyway, it's a bug in the floor
  24. finder; find the cause of the confusion (probably a single bin
  25. spike or accidental near-float-limit resolution problems) and
  26. correct it. */
  27. #include <math.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include "lsp.h"
  31. #include "os.h"
  32. #include "misc.h"
  33. #include "lookup.h"
  34. #include "scales.h"
  35. /* three possible LSP to f curve functions; the exact computation
  36. (float), a lookup based float implementation, and an integer
  37. implementation. The float lookup is likely the optimal choice on
  38. any machine with an FPU. The integer implementation is *not* fixed
  39. point (due to the need for a large dynamic range and thus a
  40. seperately tracked exponent) and thus much more complex than the
  41. relatively simple float implementations. It's mostly for future
  42. work on a fully fixed point implementation for processors like the
  43. ARM family. */
  44. /* undefine both for the 'old' but more precise implementation */
  45. #undef FLOAT_LOOKUP
  46. #undef INT_LOOKUP
  47. #ifdef FLOAT_LOOKUP
  48. #include "lookup.c" /* catch this in the build system; we #include for
  49. compilers (like gcc) that can't inline across
  50. modules */
  51. /* side effect: changes *lsp to cosines of lsp */
  52. void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
  53. float amp,float ampoffset){
  54. int i;
  55. float wdel=M_PI/ln;
  56. vorbis_fpu_control fpu;
  57. vorbis_fpu_setround(&fpu);
  58. for(i=0;i<m;i++)lsp[i]=vorbis_coslook(lsp[i]);
  59. i=0;
  60. while(i<n){
  61. int k=map[i];
  62. int qexp;
  63. float p=.7071067812f;
  64. float q=.7071067812f;
  65. float w=vorbis_coslook(wdel*k);
  66. float *ftmp=lsp;
  67. int c=m>>1;
  68. do{
  69. q*=ftmp[0]-w;
  70. p*=ftmp[1]-w;
  71. ftmp+=2;
  72. }while(--c);
  73. if(m&1){
  74. /* odd order filter; slightly assymetric */
  75. /* the last coefficient */
  76. q*=ftmp[0]-w;
  77. q*=q;
  78. p*=p*(1.f-w*w);
  79. }else{
  80. /* even order filter; still symmetric */
  81. q*=q*(1.f+w);
  82. p*=p*(1.f-w);
  83. }
  84. q=frexp(p+q,&qexp);
  85. q=vorbis_fromdBlook(amp*
  86. vorbis_invsqlook(q)*
  87. vorbis_invsq2explook(qexp+m)-
  88. ampoffset);
  89. do{
  90. curve[i++]*=q;
  91. }while(map[i]==k);
  92. }
  93. vorbis_fpu_restore(fpu);
  94. }
  95. #else
  96. #ifdef INT_LOOKUP
  97. #include "lookup.c" /* catch this in the build system; we #include for
  98. compilers (like gcc) that can't inline across
  99. modules */
  100. static int MLOOP_1[64]={
  101. 0,10,11,11, 12,12,12,12, 13,13,13,13, 13,13,13,13,
  102. 14,14,14,14, 14,14,14,14, 14,14,14,14, 14,14,14,14,
  103. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  104. 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
  105. };
  106. static int MLOOP_2[64]={
  107. 0,4,5,5, 6,6,6,6, 7,7,7,7, 7,7,7,7,
  108. 8,8,8,8, 8,8,8,8, 8,8,8,8, 8,8,8,8,
  109. 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,
  110. 9,9,9,9, 9,9,9,9, 9,9,9,9, 9,9,9,9,
  111. };
  112. static int MLOOP_3[8]={0,1,2,2,3,3,3,3};
  113. /* side effect: changes *lsp to cosines of lsp */
  114. void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
  115. float amp,float ampoffset){
  116. /* 0 <= m < 256 */
  117. /* set up for using all int later */
  118. int i;
  119. int ampoffseti=rint(ampoffset*4096.f);
  120. int ampi=rint(amp*16.f);
  121. long *ilsp=alloca(m*sizeof(long));
  122. for(i=0;i<m;i++)ilsp[i]=vorbis_coslook_i(lsp[i]/M_PI*65536.f+.5f);
  123. i=0;
  124. while(i<n){
  125. int j,k=map[i];
  126. unsigned long pi=46341; /* 2**-.5 in 0.16 */
  127. unsigned long qi=46341;
  128. int qexp=0,shift;
  129. long wi=vorbis_coslook_i(k*65536/ln);
  130. qi*=labs(ilsp[0]-wi);
  131. pi*=labs(ilsp[1]-wi);
  132. for(j=3;j<m;j+=2){
  133. if(!(shift=MLOOP_1[(pi|qi)>>25]))
  134. if(!(shift=MLOOP_2[(pi|qi)>>19]))
  135. shift=MLOOP_3[(pi|qi)>>16];
  136. qi=(qi>>shift)*labs(ilsp[j-1]-wi);
  137. pi=(pi>>shift)*labs(ilsp[j]-wi);
  138. qexp+=shift;
  139. }
  140. if(!(shift=MLOOP_1[(pi|qi)>>25]))
  141. if(!(shift=MLOOP_2[(pi|qi)>>19]))
  142. shift=MLOOP_3[(pi|qi)>>16];
  143. /* pi,qi normalized collectively, both tracked using qexp */
  144. if(m&1){
  145. /* odd order filter; slightly assymetric */
  146. /* the last coefficient */
  147. qi=(qi>>shift)*labs(ilsp[j-1]-wi);
  148. pi=(pi>>shift)<<14;
  149. qexp+=shift;
  150. if(!(shift=MLOOP_1[(pi|qi)>>25]))
  151. if(!(shift=MLOOP_2[(pi|qi)>>19]))
  152. shift=MLOOP_3[(pi|qi)>>16];
  153. pi>>=shift;
  154. qi>>=shift;
  155. qexp+=shift-14*((m+1)>>1);
  156. pi=((pi*pi)>>16);
  157. qi=((qi*qi)>>16);
  158. qexp=qexp*2+m;
  159. pi*=(1<<14)-((wi*wi)>>14);
  160. qi+=pi>>14;
  161. }else{
  162. /* even order filter; still symmetric */
  163. /* p*=p(1-w), q*=q(1+w), let normalization drift because it isn't
  164. worth tracking step by step */
  165. pi>>=shift;
  166. qi>>=shift;
  167. qexp+=shift-7*m;
  168. pi=((pi*pi)>>16);
  169. qi=((qi*qi)>>16);
  170. qexp=qexp*2+m;
  171. pi*=(1<<14)-wi;
  172. qi*=(1<<14)+wi;
  173. qi=(qi+pi)>>14;
  174. }
  175. /* we've let the normalization drift because it wasn't important;
  176. however, for the lookup, things must be normalized again. We
  177. need at most one right shift or a number of left shifts */
  178. if(qi&0xffff0000){ /* checks for 1.xxxxxxxxxxxxxxxx */
  179. qi>>=1; qexp++;
  180. }else
  181. while(qi && !(qi&0x8000)){ /* checks for 0.0xxxxxxxxxxxxxxx or less*/
  182. qi<<=1; qexp--;
  183. }
  184. amp=vorbis_fromdBlook_i(ampi* /* n.4 */
  185. vorbis_invsqlook_i(qi,qexp)-
  186. /* m.8, m+n<=8 */
  187. ampoffseti); /* 8.12[0] */
  188. curve[i]*=amp;
  189. while(map[++i]==k)curve[i]*=amp;
  190. }
  191. }
  192. #else
  193. /* old, nonoptimized but simple version for any poor sap who needs to
  194. figure out what the hell this code does, or wants the other
  195. fraction of a dB precision */
  196. /* side effect: changes *lsp to cosines of lsp */
  197. void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m,
  198. float amp,float ampoffset){
  199. int i;
  200. float wdel=M_PI/ln;
  201. for(i=0;i<m;i++)lsp[i]=2.f*cos(lsp[i]);
  202. i=0;
  203. while(i<n){
  204. int j,k=map[i];
  205. float p=.5f;
  206. float q=.5f;
  207. float w=2.f*cos(wdel*k);
  208. for(j=1;j<m;j+=2){
  209. q *= w-lsp[j-1];
  210. p *= w-lsp[j];
  211. }
  212. if(j==m){
  213. /* odd order filter; slightly assymetric */
  214. /* the last coefficient */
  215. q*=w-lsp[j-1];
  216. p*=p*(4.f-w*w);
  217. q*=q;
  218. }else{
  219. /* even order filter; still symmetric */
  220. p*=p*(2.f-w);
  221. q*=q*(2.f+w);
  222. }
  223. q=fromdB(amp/sqrt(p+q)-ampoffset);
  224. curve[i]*=q;
  225. while(map[++i]==k)curve[i]*=q;
  226. }
  227. }
  228. #endif
  229. #endif
  230. static void cheby(float *g, int ord) {
  231. int i, j;
  232. g[0] *= .5f;
  233. for(i=2; i<= ord; i++) {
  234. for(j=ord; j >= i; j--) {
  235. g[j-2] -= g[j];
  236. g[j] += g[j];
  237. }
  238. }
  239. }
  240. static int comp(const void *a,const void *b){
  241. if(*(float *)a<*(float *)b)
  242. return(1);
  243. else
  244. return(-1);
  245. }
  246. /* Newton-Raphson-Maehly actually functioned as a decent root finder,
  247. but there are root sets for which it gets into limit cycles
  248. (exacerbated by zero suppression) and fails. We can't afford to
  249. fail, even if the failure is 1 in 100,000,000, so we now use
  250. Laguerre and later polish with Newton-Raphson (which can then
  251. afford to fail) */
  252. #define EPSILON 10e-7
  253. static int Laguerre_With_Deflation(float *a,int ord,float *r){
  254. int i,m;
  255. double lastdelta=0.f;
  256. double *defl=alloca(sizeof(double)*(ord+1));
  257. for(i=0;i<=ord;i++)defl[i]=a[i];
  258. for(m=ord;m>0;m--){
  259. double new=0.f,delta;
  260. /* iterate a root */
  261. while(1){
  262. double p=defl[m],pp=0.f,ppp=0.f,denom;
  263. /* eval the polynomial and its first two derivatives */
  264. for(i=m;i>0;i--){
  265. ppp = new*ppp + pp;
  266. pp = new*pp + p;
  267. p = new*p + defl[i-1];
  268. }
  269. /* Laguerre's method */
  270. denom=(m-1) * ((m-1)*pp*pp - m*p*ppp);
  271. if(denom<0)
  272. return(-1); /* complex root! The LPC generator handed us a bad filter */
  273. if(pp>0){
  274. denom = pp + sqrt(denom);
  275. if(denom<EPSILON)denom=EPSILON;
  276. }else{
  277. denom = pp - sqrt(denom);
  278. if(denom>-(EPSILON))denom=-(EPSILON);
  279. }
  280. delta = m*p/denom;
  281. new -= delta;
  282. if(delta<0.f)delta*=-1;
  283. if(fabs(delta/new)<10e-12)break;
  284. lastdelta=delta;
  285. }
  286. r[m-1]=new;
  287. /* forward deflation */
  288. for(i=m;i>0;i--)
  289. defl[i-1]+=new*defl[i];
  290. defl++;
  291. }
  292. return(0);
  293. }
  294. /* for spit-and-polish only */
  295. static int Newton_Raphson(float *a,int ord,float *r){
  296. int i, k, count=0;
  297. double error=1.f;
  298. double *root=alloca(ord*sizeof(double));
  299. for(i=0; i<ord;i++) root[i] = r[i];
  300. while(error>1e-20){
  301. error=0;
  302. for(i=0; i<ord; i++) { /* Update each point. */
  303. double pp=0.,delta;
  304. double rooti=root[i];
  305. double p=a[ord];
  306. for(k=ord-1; k>= 0; k--) {
  307. pp= pp* rooti + p;
  308. p = p * rooti + a[k];
  309. }
  310. delta = p/pp;
  311. root[i] -= delta;
  312. error+= delta*delta;
  313. }
  314. if(count>40)return(-1);
  315. count++;
  316. }
  317. /* Replaced the original bubble sort with a real sort. With your
  318. help, we can eliminate the bubble sort in our lifetime. --Monty */
  319. for(i=0; i<ord;i++) r[i] = root[i];
  320. return(0);
  321. }
  322. /* Convert lpc coefficients to lsp coefficients */
  323. int vorbis_lpc_to_lsp(float *lpc,float *lsp,int m){
  324. int order2=(m+1)>>1;
  325. int g1_order,g2_order;
  326. float *g1=alloca(sizeof(float)*(order2+1));
  327. float *g2=alloca(sizeof(float)*(order2+1));
  328. float *g1r=alloca(sizeof(float)*(order2+1));
  329. float *g2r=alloca(sizeof(float)*(order2+1));
  330. int i;
  331. /* even and odd are slightly different base cases */
  332. g1_order=(m+1)>>1;
  333. g2_order=(m) >>1;
  334. /* Compute the lengths of the x polynomials. */
  335. /* Compute the first half of K & R F1 & F2 polynomials. */
  336. /* Compute half of the symmetric and antisymmetric polynomials. */
  337. /* Remove the roots at +1 and -1. */
  338. g1[g1_order] = 1.f;
  339. for(i=1;i<=g1_order;i++) g1[g1_order-i] = lpc[i-1]+lpc[m-i];
  340. g2[g2_order] = 1.f;
  341. for(i=1;i<=g2_order;i++) g2[g2_order-i] = lpc[i-1]-lpc[m-i];
  342. if(g1_order>g2_order){
  343. for(i=2; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+2];
  344. }else{
  345. for(i=1; i<=g1_order;i++) g1[g1_order-i] -= g1[g1_order-i+1];
  346. for(i=1; i<=g2_order;i++) g2[g2_order-i] += g2[g2_order-i+1];
  347. }
  348. /* Convert into polynomials in cos(alpha) */
  349. cheby(g1,g1_order);
  350. cheby(g2,g2_order);
  351. /* Find the roots of the 2 even polynomials.*/
  352. if(Laguerre_With_Deflation(g1,g1_order,g1r) ||
  353. Laguerre_With_Deflation(g2,g2_order,g2r))
  354. return(-1);
  355. Newton_Raphson(g1,g1_order,g1r); /* if it fails, it leaves g1r alone */
  356. Newton_Raphson(g2,g2_order,g2r); /* if it fails, it leaves g2r alone */
  357. qsort(g1r,g1_order,sizeof(float),comp);
  358. qsort(g2r,g2_order,sizeof(float),comp);
  359. for(i=0;i<g1_order;i++)
  360. lsp[i*2] = acos(g1r[i]);
  361. for(i=0;i<g2_order;i++)
  362. lsp[i*2+1] = acos(g2r[i]);
  363. return(0);
  364. }