metrics.c 8.1 KB

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