stabilize the generic metric some, bugfixes
authorMonty <xiphmont@xiph.org>
Wed, 5 Jan 2000 15:05:02 +0000 (15:05 +0000)
committerMonty <xiphmont@xiph.org>
Wed, 5 Jan 2000 15:05:02 +0000 (15:05 +0000)
svn path=/trunk/vorbis/; revision=225

vq/bookutil.c
vq/cascade.c
vq/genericdata.c
vq/lspdata.c
vq/run.c
vq/train.c
vq/vqgen.c
vq/vqsplit.c

index 9639ade..d4e25af 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: utility functions for loading .vqh and .vqd files
- last mod: $Id: bookutil.c,v 1.1 2000/01/05 10:14:54 xiphmont Exp $
+ last mod: $Id: bookutil.c,v 1.2 2000/01/05 15:04:55 xiphmont Exp $
 
  ********************************************************************/
 
@@ -53,7 +53,7 @@ char *get_line(FILE *in){
     int gotline=0;
 
     while(!gotline){
-      if(sofar>=lbufsize){
+      if(sofar+1>=lbufsize){
         if(!lbufsize){  
           lbufsize=1024;
           linebuffer=malloc(lbufsize);
index a198722..c7dcc5a 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: function call to do simple data cascading
- last mod: $Id: cascade.c,v 1.1 2000/01/05 10:14:55 xiphmont Exp $
+ last mod: $Id: cascade.c,v 1.2 2000/01/05 15:04:56 xiphmont Exp $
 
  ********************************************************************/
 
 
 #include "bookutil.h"
 
+void process_preprocess(codebook *b,char *basename){
+}
+void process_postprocess(codebook *b,char *basename){
+}
+
 void process_vector(codebook *b,double *a){
   int entry=codebook_entry(b,a);
   double *e=b->valuelist+b->dim*entry;
index 5279a2a..d6dbbed 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: generic euclidian distance metric for VQ codebooks
- last mod: $Id: genericdata.c,v 1.1 2000/01/05 10:14:55 xiphmont Exp $
+ last mod: $Id: genericdata.c,v 1.2 2000/01/05 15:04:57 xiphmont Exp $
 
  ********************************************************************/
 
@@ -40,7 +40,7 @@ double vqext_metric(vqgen *v,double *e, double *p){
     double val=p[i]-e[i];
     acc+=val*val;
   }
-  return acc;
+  return sqrt(acc/v->elements);
 }
 
 void vqext_addpoint_adj(vqgen *v,double *b,int start,int dim,int cols){
@@ -52,3 +52,7 @@ void vqext_preprocess(vqgen *v){
 }
 
 
+
+
+
+
index 75785ad..9ca2365 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: metrics and quantization code for LSP VQ codebooks
- last mod: $Id: lspdata.c,v 1.7 1999/12/30 07:27:00 xiphmont Exp $
+ last mod: $Id: lspdata.c,v 1.8 2000/01/05 15:04:58 xiphmont Exp $
 
  ********************************************************************/
 
@@ -58,7 +58,7 @@ double vqext_metric(vqgen *v,double *e, double *p){
     double val=(p[i]-e[i])*FUDGE;
     acc+=val*val;
   }
-  return acc;
+  return sqrt(acc/v->elements);
 }
 
 /* Data files are line-vectors, starting with zero.  If we want to
index 587231a..cacbbe9 100644 (file)
--- a/vq/run.c
+++ b/vq/run.c
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: utility main for loading and operating on codebooks
- last mod: $Id: run.c,v 1.4 2000/01/05 10:14:55 xiphmont Exp $
+ last mod: $Id: run.c,v 1.5 2000/01/05 15:04:59 xiphmont Exp $
 
  ********************************************************************/
 
@@ -36,7 +36,9 @@
 
  */
 
-extern void process_vector(codebook *b,double *a,char *basename);
+extern void process_preprocess(codebook *b,char *basename);
+extern void process_postprocess(codebook *b,char *basename);
+extern void process_vector(codebook *b,double *a);
 extern void process_usage(void);
 
 int main(int argc,char *argv[]){
@@ -66,6 +68,8 @@ int main(int argc,char *argv[]){
     dot=strchr(basename,'.');
     if(dot)*dot='\0';
   }
+
+  process_preprocess(b,basename);
   
   while(*argv){
     /* only input files */
@@ -74,7 +78,7 @@ int main(int argc,char *argv[]){
     reset_next_value();
 
     while(get_vector(b,in,a)!=-1)
-      process_vector(b,a,basename);
+      process_vector(b,a);
 
     fclose(in);
   }
@@ -89,8 +93,11 @@ int main(int argc,char *argv[]){
     if((S_IFIFO|S_IFREG|S_IFSOCK)&st.st_mode){
       reset_next_value();
       while(get_vector(b,stdin,a)!=-1)
-       process_vector(b,a,basename);
+       process_vector(b,a);
     }
   }
+
+  process_postprocess(b,basename);
+
   return 0;
 }
index b8d6845..6da07d3 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: utility main for training codebooks
- last mod: $Id: train.c,v 1.12 2000/01/05 10:14:56 xiphmont Exp $
+ last mod: $Id: train.c,v 1.13 2000/01/05 15:05:00 xiphmont Exp $
 
  ********************************************************************/
 
@@ -245,7 +245,7 @@ int main(int argc,char *argv[]){
        }
        {
          int i;
-         double *b=malloc(cols*sizeof(double));
+         double b[cols];
          if(start*num+dim>cols){
            fprintf(stderr,"ran out of columns reading %s\n",file);
            exit(1);
@@ -270,7 +270,6 @@ int main(int argc,char *argv[]){
          for(i=0;i<num;i++)
            vqext_addpoint_adj(&v,b,start+i*dim,dim,cols);
 
-         free(b);
        }
       }
       fclose(in);
index 024d589..7101531 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: train a VQ codebook 
- last mod: $Id: vqgen.c,v 1.26 2000/01/05 10:14:57 xiphmont Exp $
+ last mod: $Id: vqgen.c,v 1.27 2000/01/05 15:05:01 xiphmont Exp $
 
  ********************************************************************/
 
@@ -58,7 +58,7 @@
 #define vN(data,i) (data+v->elements*i)
 
 /* default metric; squared 'distance' from desired value. */
-double _dist_sq(vqgen *v,double *a, double *b){
+double _dist(vqgen *v,double *a, double *b){
   int i;
   int el=v->elements;
   double acc=0.;
@@ -66,7 +66,7 @@ double _dist_sq(vqgen *v,double *a, double *b){
     double val=(a[i]-b[i]);
     acc+=val*val;
   }
-  return acc;
+  return sqrt(acc);
 }
 
 double *_weight_null(vqgen *v,double *a){
@@ -215,7 +215,7 @@ void vqgen_init(vqgen *v,int elements,int aux,int entries,
   if(metric)
     v->metric_func=metric;
   else
-    v->metric_func=_dist_sq;
+    v->metric_func=_dist;
   if(weight)
     v->weight_func=weight;
   else
@@ -253,6 +253,19 @@ double vqgen_iterate(vqgen *v){
   double *new=malloc(sizeof(double)*v->entries*v->elements);
   long   *nearcount=malloc(v->entries*sizeof(long));
   double *nearbias=malloc(v->entries*desired2*sizeof(double));
+ #ifdef NOISY
+   char buff[80];
+   FILE *assig;
+   FILE *bias;
+   FILE *cells;
+   sprintf(buff,"cells%d.m",v->it);
+   cells=fopen(buff,"w");
+   sprintf(buff,"assig%d.m",v->it);
+   assig=fopen(buff,"w");
+   sprintf(buff,"bias%d.m",v->it);
+   bias=fopen(buff,"w");
+ #endif
 
   if(v->entries<2){
     fprintf(stderr,"generation requires at least two entries\n");
@@ -294,9 +307,17 @@ double vqgen_iterate(vqgen *v){
        }
       }
     }
+
+    j=firstentry;
       
+#ifdef NOISY
+    fprintf(cells,"%g %g\n%g %g\n\n",
+          _now(v,j)[0],_now(v,j)[1],
+          ppt[0],ppt[1]);
+#endif
+
     j=firstentry;
-    meterror+=sqrt(_dist_sq(v,_now(v,j),ppt)/v->elements);
+    meterror+=firstmetric-v->bias[firstentry];
     /* set up midpoints for next iter */
     if(v->assigned[j]++)
       for(k=0;k<v->elements;k++)
@@ -356,6 +377,10 @@ double vqgen_iterate(vqgen *v){
   /* assign midpoints */
 
   for(j=0;j<v->entries;j++){
+#ifdef NOISY
+    fprintf(assig,"%ld\n",v->assigned[j]);
+    fprintf(bias,"%g\n",v->bias[j]);
+#endif
     asserror+=fabs(v->assigned[j]-fdesired);
     if(v->assigned[j])
       for(k=0;k<v->elements;k++)
index aaf6525..5da1ea5 100644 (file)
@@ -12,7 +12,7 @@
  ********************************************************************
 
  function: build a VQ codebook and the encoding decision 'tree'
- last mod: $Id: vqsplit.c,v 1.9 2000/01/05 10:14:59 xiphmont Exp $
+ last mod: $Id: vqsplit.c,v 1.10 2000/01/05 15:05:02 xiphmont Exp $
 
  ********************************************************************/
 
@@ -68,7 +68,7 @@ int iascsort(const void *a,const void *b){
 }
 
 /* grab it from vqgen.c */
-extern double _dist_sq(vqgen *v,double *a, double *b);
+extern double _dist(vqgen *v,double *a, double *b);
 
 /* goes through the split, but just counts it and returns a metric*/
 void vqsp_count(vqgen *v,long *membership,
@@ -162,12 +162,12 @@ int lp_split(vqgen *v,codebook *b,
   for(i=0;i<points;i++){
     double *ppt=_point(v,pointindex[i]);
     long   firstentry=0;
-    double firstmetric=_dist_sq(v,_now(v,entryindex[0]),ppt);
+    double firstmetric=_dist(v,_now(v,entryindex[0]),ppt);
     
     if(points*entries>64*1024)spinnit(spinbuf,entries);
 
     for(j=1;j<entries;j++){
-      double thismetric=_dist_sq(v,_now(v,entryindex[j]),ppt);
+      double thismetric=_dist(v,_now(v,entryindex[j]),ppt);
       if(thismetric<firstmetric){
        firstmetric=thismetric;
        firstentry=j;
@@ -249,7 +249,7 @@ int lp_split(vqgen *v,codebook *b,
 
       for(j=0;j<entries;j++){
        if(j!=i){
-         double this=_dist_sq(v,q,_now(v,entryindex[j]));
+         double this=_dist(v,q,_now(v,entryindex[j]));
          if(ref_j==-1 || this<ref_best){
            ref_best=this;
            ref_j=entryindex[j];
@@ -416,10 +416,10 @@ void vqsp_book(vqgen *v, codebook *b, long *quantlist){
   for(i=0;i<v->points;i++){
     double *ppt=_point(v,i);
     long   firstentry=0;
-    double firstmetric=_dist_sq(v,_now(v,0),ppt);
+    double firstmetric=_dist(v,_now(v,0),ppt);
     
     for(j=1;j<v->entries;j++){
-      double thismetric=_dist_sq(v,_now(v,j),ppt);
+      double thismetric=_dist(v,_now(v,j),ppt);
       if(thismetric<firstmetric){
        firstmetric=thismetric;
        firstentry=j;