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 XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: utility for finding the distribution in a data set
14 last mod: $Id: distribution.c,v 1.6 2001/06/15 21:15:43 xiphmont Exp $
16 ********************************************************************/
29 int ascend(const void *a,const void *b){
30 return(**((long **)a)-**((long **)b));
33 int main(int argc,char *argv[]){
45 fprintf(stderr,"Usage: distribution {data.vqd [bins]| book.vqh} \n\n");
51 in=fopen(argv[1],"r");
53 fprintf(stderr,"Could not open input file %s\n",argv[1]);
57 if(strrchr(argv[1],'.') && strcmp(strrchr(argv[1],'.'),".vqh")==0){
58 /* load/decode a book */
60 codebook *b=codebook_load(argv[1]);
61 static_codebook *c=(static_codebook *)(b->c);
68 printf("entropy codebook only; no mappings\n");
72 bins=_book_maptype1_quantvals(c);
75 bins=c->entries*c->dim;
79 max=min=_float32_unpack(c->q_min);
80 delta=_float32_unpack(c->q_delta);
83 float val=c->quantlist[i]*delta+min;
87 printf("Minimum scalar value: %f\n",min);
88 printf("Maximum scalar value: %f\n",max);
93 /* lattice codebook. dump it. */
96 long **sort=calloc(bins,sizeof(long *));
97 long base=c->lengthlist[0];
98 countarray=calloc(bins,sizeof(long));
100 for(i=0;i<bins;i++)sort[i]=c->quantlist+i;
101 qsort(sort,bins,sizeof(long *),ascend);
103 for(i=0;i<b->entries;i++)
104 if(c->lengthlist[i]>base)base=c->lengthlist[i];
106 /* dump a full, correlated count */
107 for(j=0;j<b->entries;j++){
108 if(c->lengthlist[j]){
111 for(k=0;k<b->dim;k++){
112 int index= (j/indexdiv)%bins;
113 printf("%+3.1f,", c->quantlist[index]*_float32_unpack(c->q_delta)+
114 _float32_unpack(c->q_min));
117 printf("\t|",(1<<(base-c->lengthlist[j])));
118 for(k=0;k<base-c->lengthlist[j];k++)printf("*");
123 /* do a rough count */
124 for(j=0;j<b->entries;j++){
126 for(k=0;k<b->dim;k++){
127 if(c->lengthlist[j]){
128 int index= (j/indexdiv)%bins;
129 countarray[index]+=(1<<(base-c->lengthlist[j]));
140 if(countarray[i]>maxcount)maxcount=countarray[i];
143 int ptr=sort[i]-c->quantlist;
144 int stars=rint(50./maxcount*countarray[ptr]);
145 printf("%+08f (%8ld) |",c->quantlist[ptr]*delta+min,countarray[ptr]);
146 for(j=0;j<stars;j++)printf("*");
154 /* trained, full mapping codebook. */
155 printf("Can't do probability dump of a trained [type 2] codebook (yet)\n");
160 /* load/count a data file */
162 /* do it the simple way; two pass. */
167 if(!(lines&0xff))spinnit("getting min/max. lines so far...",lines);
169 while(!flag && sscanf(line,"%f",&code)==1){
170 line=strchr(line,',');
175 while(line && sscanf(line,"%f",&code)==1){
176 line=strchr(line,',');
178 if(code<min)min=code;
179 if(code>max)max=code;
186 if((int)(max-min)==min-max){
194 printf("Minimum scalar value: %f\n",min);
195 printf("Maximum scalar value: %f\n",max);
197 printf("\n counting hits into %d bins...\n",bins+1);
198 countarray=calloc(bins+1,sizeof(long));
205 if(!(lines&0xff))spinnit("counting distribution. lines so far...",lines);
207 while(line && sscanf(line,"%f",&code)==1){
208 line=strchr(line,',');
214 countarray[(int)rint(code)]++;
223 /* make a pretty graph */
226 for(i=0;i<bins+1;i++)
227 if(countarray[i]>maxcount)maxcount=countarray[i];
230 printf("Total scalars: %ld\n",total);
231 for(i=0;i<bins+1;i++){
232 int stars=rint(50./maxcount*countarray[i]);
233 printf("%08f (%8ld) |",(max-min)/bins*i+min,countarray[i]);
234 for(j=0;j<stars;j++)printf("*");