2 #define CODEBOOK _vq_book_lsp16
4 /********************************************************************
6 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
7 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
8 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
9 * PLEASE READ THESE TERMS DISTRIBUTING. *
11 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-1999 *
12 * by 1999 Monty <monty@xiph.org> and The XIPHOPHORUS Company *
13 * http://www.xiph.org/ *
15 ********************************************************************
17 function: utility main for loading/testing/running finished codebooks
18 author: Monty <xiphmont@mit.edu>
19 modifications by: Monty
20 last modification date: Dec 28 1999
22 ********************************************************************/
30 /* this is a bit silly; it's a C stub used by a Perl script to build a
31 quick executable against the chosen codebook and run tests */
33 static char *linebuffer=NULL;
34 static int lbufsize=0;
35 static char *rline(FILE *in){
37 if(feof(in))return NULL;
46 linebuffer=malloc(lbufsize);
49 linebuffer=realloc(linebuffer,lbufsize);
60 linebuffer[sofar++]=c;
61 linebuffer[sofar]='\0';
67 if(linebuffer[0]=='#'){
75 void vqbook_unquantize(vqbook *b){
77 double mindel=float24_unpack(b->min);
78 double delta=float24_unpack(b->delta);
79 if(!b->valuelist)b->valuelist=malloc(sizeof(double)*b->entries*b->dim);
81 for(j=0;j<b->entries;j++){
83 for(k=0;k<b->dim;k++){
84 double val=b->quantlist[j*b->dim+k]*delta+last+mindel;
85 b->valuelist[j*b->dim+k]=val;
86 if(b->sequencep)last=val;
94 run outbase [-m] [-s <start>,<n>] datafile [-s <start>,<n>] [datafile...]
96 produces: outbase-residue.m (error between input data and chosen codewords;
97 can be used to cascade)
98 outbase-cells.m (2d gnuplot file of cells if -m)
100 currently assumes a 'sequenced' codebook wants its test data to be
101 normalized to begin at zero... */
103 int main(int argc,char *argv[]){
112 double mean=0.,meansquare=0.,mean_count=0.;
117 fprintf(stderr,"Need a basename.\n");
122 vqbook_unquantize(b);
126 snprintf(buf,80,"%s-residue.vqd",name);
127 residue=fopen(buf,"w");
129 fprintf(stderr,"Unable to open output file %s\n",buf);
134 /* parse the command line; handle things as the come */
143 snprintf(buf,80,"%s-cells.m",name);
144 cells=fopen(buf,"w");
146 fprintf(stderr,"Unable to open output file %s\n",buf);
154 fprintf(stderr,"Option %s missing argument.\n",argv[0]);
157 if(sscanf(argv[1],"%d,%d",&start,&num)!=2){
159 if(sscanf(argv[1],"%d",&start)!=1){
160 fprintf(stderr,"Option %s syntax error.\n",argv[0]);
168 /* it's an input file */
169 char *file=strdup(*argv++);
175 fprintf(stderr,"Could not open input file %s\n",file);
179 while((line=rline(in))){
182 while(*temp==' ')temp++;
183 for(cols=0;*temp;cols++){
184 while(*temp>32)temp++;
185 while(*temp==' ')temp++;
190 double *p=malloc(cols*sizeof(double));
191 if(start*num+b->dim>cols){
192 fprintf(stderr,"ran out of columns reading %s\n",file);
195 while(*line==' ')line++;
198 /* static length buffer bug workaround */
201 while(*temp>32)temp++;
208 while(*line>32)line++;
209 while(*line==' ')line++;
211 if(num<=0)num=(cols-start)/b->dim;
212 for(i=num-1;i>=0;i--){
214 double *base=p+start+i*b->dim;
216 /* if this is a sequenced book and i||start,
217 normalize the beginning to zero */
218 if(b->sequencep && (i>0 || start>0)){
219 for(k=0;k<b->dim;k++)
223 /* assign the point */
224 entry=vqenc_entry(b,base);
226 /* accumulate metrics */
227 for(k=0;k<b->dim;k++){
228 double err=base[k]-b->valuelist[k+entry*b->dim];
234 /* brute force it... did that work better? */
237 /* paint the cell if -m */
239 fprintf(cells,"%g %g\n%g %g\n\n",
241 b->valuelist[0+entry*b->dim],
242 b->valuelist[1+entry*b->dim]);
245 /* write cascading data */
259 if(cells)fclose(cells);
261 /* print accumulated error statistics */
262 fprintf(stderr,"results:\n\tmean squared error:%g\n\tmean error:%g\n\n",
263 sqrt(meansquare/mean_count),mean/mean_count);