metrics.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH 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.6.4.5 2000/05/08 08:25:44 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. double meanamplitude_acc=0.;
  34. double meanamplitudesq_acc=0.;
  35. double meanerror_acc=0.;
  36. double meanerrorsq_acc=0.;
  37. double **histogram=NULL;
  38. double **histogram_error=NULL;
  39. double **histogram_errorsq=NULL;
  40. double **histogram_hi=NULL;
  41. double **histogram_lo=NULL;
  42. double bits=0.;
  43. double count=0.;
  44. static double *_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=calloc(books,sizeof(double *));
  53. histogram_error=calloc(books,sizeof(double *));
  54. histogram_errorsq=calloc(books,sizeof(double *));
  55. histogram_hi=calloc(books,sizeof(double *));
  56. histogram_lo=calloc(books,sizeof(double *));
  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]=calloc(b->entries,sizeof(double));
  64. histogram_error[i]=calloc(b->entries*b->dim,sizeof(double));
  65. histogram_errorsq[i]=calloc(b->entries*b->dim,sizeof(double));
  66. histogram_hi[i]=calloc(b->entries*b->dim,sizeof(double));
  67. histogram_lo[i]=calloc(b->entries*b->dim,sizeof(double));
  68. }
  69. }
  70. static double _dist(int el,double *a, double *b){
  71. int i;
  72. double acc=0.;
  73. for(i=0;i<el;i++){
  74. double 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. double 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. double localmin=-1.;
  87. for(k=0;k<c->c->entries;k++){
  88. if(c->c->lengthlist[k]>0){
  89. double 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. double process_one(codebook *b,int book,double *a,int dim,int step,int addmul,
  177. double base){
  178. int j,entry;
  179. double amplitude=0.;
  180. if(book==0){
  181. double last=base;
  182. for(j=0;j<dim;j++){
  183. amplitude=a[j*step]-last;
  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. double 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. histogram[book][entry]++;
  200. bits+=vorbis_book_codelen(b,entry);
  201. for(j=0;j<dim;j++){
  202. double error=a[j*step];
  203. if(book==books-1){
  204. meanerror_acc+=fabs(error);
  205. meanerrorsq_acc+=error*error;
  206. }
  207. histogram_errorsq[book][entry*dim+j]+=error*error;
  208. histogram_error[book][entry*dim+j]+=fabs(error);
  209. if(histogram[book][entry]==0 || histogram_hi[book][entry*dim+j]<error)
  210. histogram_hi[book][entry*dim+j]=error;
  211. if(histogram[book][entry]==0 || histogram_lo[book][entry*dim+j]>error)
  212. histogram_lo[book][entry*dim+j]=error;
  213. }
  214. return base;
  215. }
  216. void process_vector(codebook **bs,int *addmul,int inter,double *a,int n){
  217. int bi;
  218. int i;
  219. for(bi=0;bi<books;bi++){
  220. codebook *b=bs[bi];
  221. int dim=b->dim;
  222. double base=0.;
  223. if(inter){
  224. for(i=0;i<n/dim;i++)
  225. base=process_one(b,bi,a+i,dim,n/dim,addmul[bi],base);
  226. }else{
  227. for(i=0;i<=n-dim;i+=dim)
  228. base=process_one(b,bi,a+i,dim,1,addmul[bi],base);
  229. }
  230. }
  231. if((long)(count)%100)spinnit("working.... samples: ",count);
  232. }
  233. void process_usage(void){
  234. fprintf(stderr,
  235. "usage: vqmetrics [-i] +|*<codebook>.vqh [ +|*<codebook.vqh> ]... \n"
  236. " datafile.vqd [datafile.vqd]...\n\n"
  237. " data can be taken on stdin. -i indicates interleaved coding.\n"
  238. " Output goes to output files:\n"
  239. " basename-me.m: gnuplot: mean error by entry value\n"
  240. " basename-mse.m: gnuplot: mean square error by entry value\n"
  241. " basename-worst.m: gnuplot: worst error by entry value\n"
  242. " basename-distance.m: gnuplot file showing distance probability\n"
  243. "\n");
  244. }