metrics.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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: function calls to collect codebook metrics
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <math.h>
  18. #include "bookutil.h"
  19. /* collect the following metrics:
  20. mean and mean squared amplitude
  21. mean and mean squared error
  22. mean and mean squared error (per sample) by entry
  23. worst case fit by entry
  24. entry cell size
  25. hits by entry
  26. total bits
  27. total samples
  28. (average bits per sample)*/
  29. /* set up metrics */
  30. float meanamplitude_acc=0.f;
  31. float meanamplitudesq_acc=0.f;
  32. float meanerror_acc=0.f;
  33. float meanerrorsq_acc=0.f;
  34. float **histogram=NULL;
  35. float **histogram_error=NULL;
  36. float **histogram_errorsq=NULL;
  37. float **histogram_hi=NULL;
  38. float **histogram_lo=NULL;
  39. float bits=0.f;
  40. float count=0.f;
  41. static float *_now(codebook *c, int i){
  42. return c->valuelist+i*c->c->dim;
  43. }
  44. int books=0;
  45. void process_preprocess(codebook **bs,char *basename){
  46. int i;
  47. while(bs[books])books++;
  48. if(books){
  49. histogram=_ogg_calloc(books,sizeof(float *));
  50. histogram_error=_ogg_calloc(books,sizeof(float *));
  51. histogram_errorsq=_ogg_calloc(books,sizeof(float *));
  52. histogram_hi=_ogg_calloc(books,sizeof(float *));
  53. histogram_lo=_ogg_calloc(books,sizeof(float *));
  54. }else{
  55. fprintf(stderr,"Specify at least one codebook\n");
  56. exit(1);
  57. }
  58. for(i=0;i<books;i++){
  59. codebook *b=bs[i];
  60. histogram[i]=_ogg_calloc(b->entries,sizeof(float));
  61. histogram_error[i]=_ogg_calloc(b->entries*b->dim,sizeof(float));
  62. histogram_errorsq[i]=_ogg_calloc(b->entries*b->dim,sizeof(float));
  63. histogram_hi[i]=_ogg_calloc(b->entries*b->dim,sizeof(float));
  64. histogram_lo[i]=_ogg_calloc(b->entries*b->dim,sizeof(float));
  65. }
  66. }
  67. static float _dist(int el,float *a, float *b){
  68. int i;
  69. float acc=0.f;
  70. for(i=0;i<el;i++){
  71. float val=(a[i]-b[i]);
  72. acc+=val*val;
  73. }
  74. return acc;
  75. }
  76. void cell_spacing(codebook *c){
  77. int j,k;
  78. float min=-1.f,max=-1.f,mean=0.f,meansq=0.f;
  79. long total=0;
  80. /* minimum, maximum, mean, ms cell spacing */
  81. for(j=0;j<c->c->entries;j++){
  82. if(c->c->lengthlist[j]>0){
  83. float localmin=-1.;
  84. for(k=0;k<c->c->entries;k++){
  85. if(c->c->lengthlist[k]>0){
  86. float this=_dist(c->c->dim,_now(c,j),_now(c,k));
  87. if(j!=k &&
  88. (localmin==-1 || this<localmin))
  89. localmin=this;
  90. }
  91. }
  92. if(min==-1 || localmin<min)min=localmin;
  93. if(max==-1 || localmin>max)max=localmin;
  94. mean+=sqrt(localmin);
  95. meansq+=localmin;
  96. total++;
  97. }
  98. }
  99. fprintf(stderr,"\tminimum cell spacing (closest side): %g\n",sqrt(min));
  100. fprintf(stderr,"\tmaximum cell spacing (closest side): %g\n",sqrt(max));
  101. fprintf(stderr,"\tmean closest side spacing: %g\n",mean/total);
  102. fprintf(stderr,"\tmean sq closest side spacing: %g\n",sqrt(meansq/total));
  103. }
  104. void process_postprocess(codebook **bs,char *basename){
  105. int i,k,book;
  106. char *buffer=alloca(strlen(basename)+80);
  107. fprintf(stderr,"Done. Processed %ld data points:\n\n",
  108. (long)count);
  109. fprintf(stderr,"Global statistics:******************\n\n");
  110. fprintf(stderr,"\ttotal samples: %ld\n",(long)count);
  111. fprintf(stderr,"\ttotal bits required to code: %ld\n",(long)bits);
  112. fprintf(stderr,"\taverage bits per sample: %g\n\n",bits/count);
  113. fprintf(stderr,"\tmean sample amplitude: %g\n",
  114. meanamplitude_acc/count);
  115. fprintf(stderr,"\tmean squared sample amplitude: %g\n\n",
  116. sqrt(meanamplitudesq_acc/count));
  117. fprintf(stderr,"\tmean code error: %g\n",
  118. meanerror_acc/count);
  119. fprintf(stderr,"\tmean squared code error: %g\n\n",
  120. sqrt(meanerrorsq_acc/count));
  121. for(book=0;book<books;book++){
  122. FILE *out;
  123. codebook *b=bs[book];
  124. int n=b->c->entries;
  125. int dim=b->c->dim;
  126. fprintf(stderr,"Book %d statistics:------------------\n",book);
  127. cell_spacing(b);
  128. sprintf(buffer,"%s-%d-mse.m",basename,book);
  129. out=fopen(buffer,"w");
  130. if(!out){
  131. fprintf(stderr,"Could not open file %s for writing\n",buffer);
  132. exit(1);
  133. }
  134. for(i=0;i<n;i++){
  135. for(k=0;k<dim;k++){
  136. fprintf(out,"%d, %g, %g\n",
  137. i*dim+k,(b->valuelist+i*dim)[k],
  138. sqrt((histogram_errorsq[book]+i*dim)[k]/histogram[book][i]));
  139. }
  140. }
  141. fclose(out);
  142. sprintf(buffer,"%s-%d-me.m",basename,book);
  143. out=fopen(buffer,"w");
  144. if(!out){
  145. fprintf(stderr,"Could not open file %s for writing\n",buffer);
  146. exit(1);
  147. }
  148. for(i=0;i<n;i++){
  149. for(k=0;k<dim;k++){
  150. fprintf(out,"%d, %g, %g\n",
  151. i*dim+k,(b->valuelist+i*dim)[k],
  152. (histogram_error[book]+i*dim)[k]/histogram[book][i]);
  153. }
  154. }
  155. fclose(out);
  156. sprintf(buffer,"%s-%d-worst.m",basename,book);
  157. out=fopen(buffer,"w");
  158. if(!out){
  159. fprintf(stderr,"Could not open file %s for writing\n",buffer);
  160. exit(1);
  161. }
  162. for(i=0;i<n;i++){
  163. for(k=0;k<dim;k++){
  164. fprintf(out,"%d, %g, %g, %g\n",
  165. i*dim+k,(b->valuelist+i*dim)[k],
  166. (b->valuelist+i*dim)[k]+(histogram_lo[book]+i*dim)[k],
  167. (b->valuelist+i*dim)[k]+(histogram_hi[book]+i*dim)[k]);
  168. }
  169. }
  170. fclose(out);
  171. }
  172. }
  173. float process_one(codebook *b,int book,float *a,int dim,int step,int addmul,
  174. float base){
  175. int j,entry;
  176. float amplitude=0.f;
  177. if(book==0){
  178. float last=base;
  179. for(j=0;j<dim;j++){
  180. amplitude=a[j*step]-(b->c->q_sequencep?last:0);
  181. meanamplitude_acc+=fabs(amplitude);
  182. meanamplitudesq_acc+=amplitude*amplitude;
  183. count++;
  184. last=a[j*step];
  185. }
  186. }
  187. if(b->c->q_sequencep){
  188. float temp;
  189. for(j=0;j<dim;j++){
  190. temp=a[j*step];
  191. a[j*step]-=base;
  192. }
  193. base=temp;
  194. }
  195. entry=vorbis_book_besterror(b,a,step,addmul);
  196. if(entry==-1){
  197. fprintf(stderr,"Internal error: _best returned -1.\n");
  198. exit(1);
  199. }
  200. histogram[book][entry]++;
  201. bits+=vorbis_book_codelen(b,entry);
  202. for(j=0;j<dim;j++){
  203. float error=a[j*step];
  204. if(book==books-1){
  205. meanerror_acc+=fabs(error);
  206. meanerrorsq_acc+=error*error;
  207. }
  208. histogram_errorsq[book][entry*dim+j]+=error*error;
  209. histogram_error[book][entry*dim+j]+=fabs(error);
  210. if(histogram[book][entry]==0 || histogram_hi[book][entry*dim+j]<error)
  211. histogram_hi[book][entry*dim+j]=error;
  212. if(histogram[book][entry]==0 || histogram_lo[book][entry*dim+j]>error)
  213. histogram_lo[book][entry*dim+j]=error;
  214. }
  215. return base;
  216. }
  217. void process_vector(codebook **bs,int *addmul,int inter,float *a,int n){
  218. int bi;
  219. int i;
  220. for(bi=0;bi<books;bi++){
  221. codebook *b=bs[bi];
  222. int dim=b->dim;
  223. float base=0.f;
  224. if(inter){
  225. for(i=0;i<n/dim;i++)
  226. base=process_one(b,bi,a+i,dim,n/dim,addmul[bi],base);
  227. }else{
  228. for(i=0;i<=n-dim;i+=dim)
  229. base=process_one(b,bi,a+i,dim,1,addmul[bi],base);
  230. }
  231. }
  232. if((long)(count)%100)spinnit("working.... samples: ",count);
  233. }
  234. void process_usage(void){
  235. fprintf(stderr,
  236. "usage: vqmetrics [-i] +|*<codebook>.vqh [ +|*<codebook.vqh> ]... \n"
  237. " datafile.vqd [datafile.vqd]...\n\n"
  238. " data can be taken on stdin. -i indicates interleaved coding.\n"
  239. " Output goes to output files:\n"
  240. " basename-me.m: gnuplot: mean error by entry value\n"
  241. " basename-mse.m: gnuplot: mean square error by entry value\n"
  242. " basename-worst.m: gnuplot: worst error by entry value\n"
  243. " basename-distance.m: gnuplot file showing distance probability\n"
  244. "\n");
  245. }