lspdata.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: metrics and quantization code for LSP VQ codebooks
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include "vqgen.h"
  19. #include "vqext.h"
  20. #include "codebook.h"
  21. char *vqext_booktype="LSPdata";
  22. quant_meta q={0,0,0,1}; /* set sequence data */
  23. int vqext_aux=1;
  24. float global_maxdel=M_PI;
  25. float global_mindel=M_PI;
  26. #if 0
  27. void vqext_quantize(vqgen *v,quant_meta *q){
  28. float delta,mindel;
  29. float maxquant=((1<<q->quant)-1);
  30. int j,k;
  31. /* first find the basic delta amount from the maximum span to be
  32. encoded. Loosen the delta slightly to allow for additional error
  33. during sequence quantization */
  34. delta=(global_maxdel-global_mindel)/((1<<q->quant)-1.5f);
  35. q->min=_float32_pack(global_mindel);
  36. q->delta=_float32_pack(delta);
  37. mindel=_float32_unpack(q->min);
  38. delta=_float32_unpack(q->delta);
  39. for(j=0;j<v->entries;j++){
  40. float last=0;
  41. for(k=0;k<v->elements;k++){
  42. float val=_now(v,j)[k];
  43. float now=rint((val-last-mindel)/delta);
  44. _now(v,j)[k]=now;
  45. if(now<0){
  46. /* be paranoid; this should be impossible */
  47. fprintf(stderr,"fault; quantized value<0\n");
  48. exit(1);
  49. }
  50. if(now>maxquant){
  51. /* be paranoid; this should be impossible */
  52. fprintf(stderr,"fault; quantized value>max\n");
  53. exit(1);
  54. }
  55. last=(now*delta)+mindel+last;
  56. }
  57. }
  58. }
  59. #else
  60. void vqext_quantize(vqgen *v,quant_meta *q){
  61. vqgen_quantize(v,q);
  62. }
  63. #endif
  64. float *weight=NULL;
  65. #if 0
  66. /* LSP training metric. We weight error proportional to distance
  67. *between* LSP vector values. The idea of this metric is not to set
  68. final cells, but get the midpoint spacing into a form conducive to
  69. what we want, which is weighting toward preserving narrower
  70. features. */
  71. #define FUDGE (global_maxdel-weight[i])
  72. float *vqext_weight(vqgen *v,float *p){
  73. int i;
  74. int el=v->elements;
  75. float lastp=0.f;
  76. for(i=0;i<el;i++){
  77. float predist=(p[i]-lastp);
  78. float postdist=(p[i+1]-p[i]);
  79. weight[i]=(predist<postdist?predist:postdist);
  80. lastp=p[i];
  81. }
  82. return p;
  83. }
  84. #else
  85. #define FUDGE 1.f
  86. float *vqext_weight(vqgen *v,float *p){
  87. return p;
  88. }
  89. #endif
  90. /* candidate,actual */
  91. float vqext_metric(vqgen *v,float *e, float *p){
  92. int i;
  93. int el=v->elements;
  94. float acc=0.f;
  95. for(i=0;i<el;i++){
  96. float val=(p[i]-e[i])*FUDGE;
  97. acc+=val*val;
  98. }
  99. return sqrt(acc/v->elements);
  100. }
  101. /* Data files are line-vectors, now just deltas. The codebook entries
  102. want to be monotonically increasing, so we adjust */
  103. /* assume vqext_aux==1 */
  104. void vqext_addpoint_adj(vqgen *v,float *b,int start,int dim,int cols,int num){
  105. float *a=alloca(sizeof(float)*(dim+1)); /* +aux */
  106. float base=0;
  107. int i;
  108. for(i=0;i<dim;i++)
  109. base=a[i]=b[i+start]+base;
  110. if(start+dim+1>cols) /* +aux */
  111. a[i]=M_PI;
  112. else
  113. a[i]=b[i+start]+base;
  114. vqgen_addpoint(v,a,a+dim);
  115. }
  116. /* we just need to calc the global_maxdel from the training set */
  117. void vqext_preprocess(vqgen *v){
  118. long j,k;
  119. global_maxdel=0.f;
  120. global_mindel=M_PI;
  121. for(j=0;j<v->points;j++){
  122. float last=0.;
  123. for(k=0;k<v->elements+v->aux;k++){
  124. float p=_point(v,j)[k];
  125. if(p-last>global_maxdel)global_maxdel=p-last;
  126. if(p-last<global_mindel)global_mindel=p-last;
  127. last=p;
  128. }
  129. }
  130. weight=_ogg_malloc(sizeof(float)*v->elements);
  131. }