examples/SFExamples/oggvorbiscodec/src/libvorbis/vq/distribution.c

00001 /********************************************************************
00002  *                                                                  *
00003  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
00004  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
00005  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
00006  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
00007  *                                                                  *
00008  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
00009  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
00010  *                                                                  *
00011  ********************************************************************
00012 
00013  function: utility for finding the distribution in a data set
00014  last mod: $Id: distribution.c 7187 2004-07-20 07:24:27Z xiphmont $
00015 
00016  ********************************************************************/
00017 
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 #include <math.h>
00021 #include <string.h>
00022 #include <errno.h>
00023 #include "bookutil.h"
00024 
00025 /* command line:
00026    distribution file.vqd
00027 */
00028 
00029 int ascend(const void *a,const void *b){
00030   return(**((long **)a)-**((long **)b));
00031 }
00032 
00033 int main(int argc,char *argv[]){
00034   FILE *in;
00035   long lines=0;
00036   float min;
00037   float max;
00038   long bins=-1;
00039   int flag=0;
00040   long *countarray;
00041   long total=0;
00042   char *line;
00043 
00044   if(argv[1]==NULL){
00045     fprintf(stderr,"Usage: distribution {data.vqd [bins]| book.vqh} \n\n");
00046     exit(1);
00047   }
00048   if(argv[2]!=NULL)
00049     bins=atoi(argv[2])-1;
00050 
00051   in=fopen(argv[1],"r");
00052   if(!in){
00053     fprintf(stderr,"Could not open input file %s\n",argv[1]);
00054     exit(1);
00055   }
00056 
00057   if(strrchr(argv[1],'.') && strcmp(strrchr(argv[1],'.'),".vqh")==0){
00058     /* load/decode a book */
00059 
00060     codebook *b=codebook_load(argv[1]);
00061     static_codebook *c=(static_codebook *)(b->c);
00062     float delta;
00063     int i;
00064     fclose(in);
00065 
00066     switch(c->maptype){
00067     case 0:
00068       printf("entropy codebook only; no mappings\n");
00069       exit(0);
00070       break;
00071     case 1:
00072       bins=_book_maptype1_quantvals(c);
00073       break;
00074     case 2:
00075       bins=c->entries*c->dim;
00076       break;
00077     }
00078 
00079     max=min=_float32_unpack(c->q_min);
00080     delta=_float32_unpack(c->q_delta);
00081 
00082     for(i=0;i<bins;i++){
00083       float val=c->quantlist[i]*delta+min;
00084       if(val>max)max=val;
00085     }
00086 
00087     printf("Minimum scalar value: %f\n",min);
00088     printf("Maximum scalar value: %f\n",max);
00089 
00090     switch(c->maptype){
00091     case 1:
00092       {
00093         /* lattice codebook.  dump it. */
00094         int j,k;
00095         long maxcount=0;
00096         long **sort=calloc(bins,sizeof(long *));
00097         long base=c->lengthlist[0];
00098         countarray=calloc(bins,sizeof(long));
00099 
00100         for(i=0;i<bins;i++)sort[i]=c->quantlist+i;
00101         qsort(sort,bins,sizeof(long *),ascend);
00102 
00103         for(i=0;i<b->entries;i++)
00104           if(c->lengthlist[i]>base)base=c->lengthlist[i];
00105 
00106         /* dump a full, correlated count */
00107         for(j=0;j<b->entries;j++){
00108           if(c->lengthlist[j]){
00109             int indexdiv=1;
00110             printf("%4d: ",j);
00111             for(k=0;k<b->dim;k++){      
00112               int index= (j/indexdiv)%bins;
00113               printf("%+3.1f,", c->quantlist[index]*_float32_unpack(c->q_delta)+
00114                      _float32_unpack(c->q_min));
00115               indexdiv*=bins;
00116             }
00117             printf("\t|");
00118             for(k=0;k<base-c->lengthlist[j];k++)printf("*");
00119             printf("\n");
00120           }
00121         }
00122 
00123         /* do a rough count */
00124         for(j=0;j<b->entries;j++){
00125           int indexdiv=1;
00126           for(k=0;k<b->dim;k++){
00127             if(c->lengthlist[j]){
00128               int index= (j/indexdiv)%bins;
00129               countarray[index]+=(1<<(base-c->lengthlist[j]));
00130               indexdiv*=bins;
00131             }
00132           }
00133         }
00134 
00135         /* dump the count */
00136 
00137         {
00138           long maxcount=0,i,j;
00139           for(i=0;i<bins;i++)
00140             if(countarray[i]>maxcount)maxcount=countarray[i];
00141       
00142           for(i=0;i<bins;i++){
00143             int ptr=sort[i]-c->quantlist;
00144             int stars=rint(50./maxcount*countarray[ptr]);
00145             printf("%+08f (%8ld) |",c->quantlist[ptr]*delta+min,countarray[ptr]);
00146             for(j=0;j<stars;j++)printf("*");
00147             printf("\n");
00148           }
00149         }
00150       }
00151       break;
00152     case 2:
00153       {
00154         /* trained, full mapping codebook. */
00155         printf("Can't do probability dump of a trained [type 2] codebook (yet)\n");
00156       }
00157       break;
00158     }
00159   }else{
00160     /* load/count a data file */
00161 
00162     /* do it the simple way; two pass. */
00163     line=setup_line(in);
00164     while(line){      
00165       float code;
00166       char buf[80];
00167       lines++;
00168 
00169       sprintf(buf,"getting min/max (%.2f::%.2f). lines...",min,max);
00170       if(!(lines&0xff))spinnit(buf,lines);
00171       
00172       while(!flag && sscanf(line,"%f",&code)==1){
00173         line=strchr(line,',');
00174         min=max=code;
00175         flag=1;
00176       }
00177       
00178       while(line && sscanf(line,"%f",&code)==1){
00179         line=strchr(line,',');
00180         if(line)line++;
00181         if(code<min)min=code;
00182         if(code>max)max=code;
00183       }
00184       
00185       line=setup_line(in);
00186     }
00187     
00188     if(bins<1){
00189       if((int)(max-min)==min-max){
00190         bins=max-min;
00191       }else{
00192         bins=25;
00193       }
00194     }
00195     
00196     printf("\r                                                     \r");
00197     printf("Minimum scalar value: %f\n",min);
00198     printf("Maximum scalar value: %f\n",max);
00199 
00200     if(argv[2]){
00201       
00202       printf("\n counting hits into %ld bins...\n",bins+1);
00203       countarray=calloc(bins+1,sizeof(long));
00204       
00205       rewind(in);
00206       line=setup_line(in);
00207       while(line){      
00208         float code;
00209         lines--;
00210         if(!(lines&0xff))spinnit("counting distribution. lines so far...",lines);
00211         
00212         while(line && sscanf(line,"%f",&code)==1){
00213           line=strchr(line,',');
00214           if(line)line++;
00215           
00216           code-=min;
00217           code/=(max-min);
00218           code*=bins;
00219           countarray[(int)rint(code)]++;
00220           total++;
00221         }
00222         
00223         line=setup_line(in);
00224       }
00225     
00226       /* make a pretty graph */
00227       {
00228         long maxcount=0,i,j;
00229         for(i=0;i<bins+1;i++)
00230           if(countarray[i]>maxcount)maxcount=countarray[i];
00231         
00232         printf("\r                                                     \r");
00233         printf("Total scalars: %ld\n",total);
00234         for(i=0;i<bins+1;i++){
00235           int stars=rint(50./maxcount*countarray[i]);
00236           printf("%08f (%8ld) |",(max-min)/bins*i+min,countarray[i]);
00237           for(j=0;j<stars;j++)printf("*");
00238           printf("\n");
00239         }
00240       }
00241     }
00242 
00243     fclose(in);
00244 
00245   }
00246   printf("\nDone.\n");
00247   exit(0);
00248 }

Generated by  doxygen 1.6.2