1 /********************************************************************
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. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
13 function: utility for finding the distribution in a data set
15 ********************************************************************/
28 int ascend(const void *a,const void *b){
29 return(**((long **)a)-**((long **)b));
32 int main(int argc,char *argv[]){
44 fprintf(stderr,"Usage: distribution {data.vqd [bins]| book.vqh} \n\n");
50 in=fopen(argv[1],"r");
52 fprintf(stderr,"Could not open input file %s\n",argv[1]);
56 if(strrchr(argv[1],'.') && strcmp(strrchr(argv[1],'.'),".vqh")==0){
57 /* load/decode a book */
59 codebook *b=codebook_load(argv[1]);
60 static_codebook *c=(static_codebook *)(b->c);
67 printf("entropy codebook only; no mappings\n");
71 bins=_book_maptype1_quantvals(c);
74 bins=c->entries*c->dim;
78 max=min=_float32_unpack(c->q_min);
79 delta=_float32_unpack(c->q_delta);
82 float val=c->quantlist[i]*delta+min;
86 printf("Minimum scalar value: %f\n",min);
87 printf("Maximum scalar value: %f\n",max);
92 /* lattice codebook. dump it. */
95 long **sort=calloc(bins,sizeof(long *));
96 long base=c->lengthlist[0];
97 countarray=calloc(bins,sizeof(long));
99 for(i=0;i<bins;i++)sort[i]=c->quantlist+i;
100 qsort(sort,bins,sizeof(long *),ascend);
102 for(i=0;i<b->entries;i++)
103 if(c->lengthlist[i]>base)base=c->lengthlist[i];
105 /* dump a full, correlated count */
106 for(j=0;j<b->entries;j++){
107 if(c->lengthlist[j]){
110 for(k=0;k<b->dim;k++){
111 int index= (j/indexdiv)%bins;
112 printf("%+3.1f,", c->quantlist[index]*_float32_unpack(c->q_delta)+
113 _float32_unpack(c->q_min));
117 for(k=0;k<base-c->lengthlist[j];k++)printf("*");
122 /* do a rough count */
123 for(j=0;j<b->entries;j++){
125 for(k=0;k<b->dim;k++){
126 if(c->lengthlist[j]){
127 int index= (j/indexdiv)%bins;
128 countarray[index]+=(1<<(base-c->lengthlist[j]));
139 if(countarray[i]>maxcount)maxcount=countarray[i];
142 int ptr=sort[i]-c->quantlist;
143 int stars=rint(50./maxcount*countarray[ptr]);
144 printf("%+08f (%8ld) |",c->quantlist[ptr]*delta+min,countarray[ptr]);
145 for(j=0;j<stars;j++)printf("*");
153 /* trained, full mapping codebook. */
154 printf("Can't do probability dump of a trained [type 2] codebook (yet)\n");
159 /* load/count a data file */
161 /* do it the simple way; two pass. */
168 sprintf(buf,"getting min/max (%.2f::%.2f). lines...",min,max);
169 if(!(lines&0xff))spinnit(buf,lines);
171 while(!flag && sscanf(line,"%f",&code)==1){
172 line=strchr(line,',');
177 while(line && sscanf(line,"%f",&code)==1){
178 line=strchr(line,',');
180 if(code<min)min=code;
181 if(code>max)max=code;
188 if((int)(max-min)==min-max){
196 printf("Minimum scalar value: %f\n",min);
197 printf("Maximum scalar value: %f\n",max);
201 printf("\n counting hits into %ld bins...\n",bins+1);
202 countarray=calloc(bins+1,sizeof(long));
209 if(!(lines&0xff))spinnit("counting distribution. lines so far...",lines);
211 while(line && sscanf(line,"%f",&code)==1){
212 line=strchr(line,',');
218 countarray[(int)rint(code)]++;
225 /* make a pretty graph */
228 for(i=0;i<bins+1;i++)
229 if(countarray[i]>maxcount)maxcount=countarray[i];
232 printf("Total scalars: %ld\n",total);
233 for(i=0;i<bins+1;i++){
234 int stars=rint(50./maxcount*countarray[i]);
235 printf("%08f (%8ld) |",(max-min)/bins*i+min,countarray[i]);
236 for(j=0;j<stars;j++)printf("*");