/* vq_mbest.c David Rowe Dec 2019 Utility to perform a mbest VQ search on vectors from stdin, sending quantised vectors to stdout. */ #include #include #include #include #include #include #include #include "mbest.h" #define MAX_K 20 #define MAX_ENTRIES 4096 #define MAX_STAGES 5 void quant_mbest(float vec_out[], int indexes[], float vec_in[], int num_stages, float vqw[], float vq[], int m[], int k, int mbest_survivors); int verbose = 0; int main(int argc, char *argv[]) { float vq[MAX_STAGES*MAX_K*MAX_ENTRIES]; float vqw[MAX_STAGES*MAX_K*MAX_ENTRIES]; int m[MAX_STAGES]; int k=0, mbest_survivors=1, num_stages=0; char fnames[256], fn[256], *comma, *p; FILE *fq; float lower = -1E32; int st = -1; int en = -1; int num = INT_MAX; int output_vec_usage = 0; int o = 0; int opt_idx = 0; while (o != -1) { static struct option long_opts[] = { {"k", required_argument, 0, 'k'}, {"quant", required_argument, 0, 'q'}, {"mbest", required_argument, 0, 'm'}, {"lower", required_argument, 0, 'l'}, {"verbose", required_argument, 0, 'v'}, {"st", required_argument, 0, 't'}, {"en", required_argument, 0, 'e'}, {"num", required_argument, 0, 'n'}, {"vec_usage", no_argument, 0, 'u'}, {0, 0, 0, 0} }; o = getopt_long(argc,argv,"hk:q:m:vt:e:n:u",long_opts,&opt_idx); switch (o) { case 'k': k = atoi(optarg); assert(k <= MAX_K); break; case 'q': /* load up list of comma delimited file names */ strcpy(fnames, optarg); p = fnames; num_stages = 0; do { assert(num_stages < MAX_STAGES); strcpy(fn, p); comma = strchr(fn, ','); if (comma) { *comma = 0; p = comma+1; } /* load quantiser file */ fprintf(stderr, "stage: %d loading %s ... ", num_stages, fn); fq=fopen(fn, "rb"); if (fq == NULL) { fprintf(stderr, "Couldn't open: %s\n", fn); exit(1); } /* count how many entries m of dimension k are in this VQ file */ m[num_stages] = 0; float dummy[k]; while (fread(dummy, sizeof(float), k, fq) == (size_t)k) m[num_stages]++; assert(m[num_stages] <= MAX_ENTRIES); fprintf(stderr, "%d entries of vectors width %d\n", m[num_stages], k); /* now load VQ into memory */ rewind(fq); int rd = fread(&vq[num_stages*k*MAX_ENTRIES], sizeof(float), m[num_stages]*k, fq); assert(rd == m[num_stages]*k); num_stages++; fclose(fq); } while(comma); break; case 'm': mbest_survivors = atoi(optarg); fprintf(stderr, "mbest_survivors = %d\n", mbest_survivors); break; case 'n': num = atoi(optarg); break; case 'l': lower = atof(optarg); break; case 't': st = atoi(optarg); break; case 'e': en = atoi(optarg); break; case 'u': output_vec_usage = 1; break; case 'v': verbose = 1; break; help: fprintf(stderr, "\n"); fprintf(stderr, "usage: %s -k dimension -q vq1.f32,vq2.f32,.... [Options]\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, "input vectors on stdin, output quantised vectors on stdout\n"); fprintf(stderr, "\n"); fprintf(stderr, "--lower lowermeanLimit Only count vectors with average above this level in distortion calculations\n"); fprintf(stderr, "--mbest N number of survivors at each stage, set to 0 for standard VQ search\n"); fprintf(stderr, "--st Kst start vector element for error calculation (default 0)\n"); fprintf(stderr, "--en Ken end vector element for error calculation (default K-1)\n"); fprintf(stderr, "--num numToProcess number of vectors to quantise (default to EOF)\n"); fprintf(stderr, "--vec_usage Output a record of how many times each vector is used\n"); fprintf(stderr, "-v Verbose\n"); exit(1); } } if ((num_stages == 0) || (k == 0)) goto help; /* default to measuring error on entire vector */ if (st == -1) st = 0; if (en == -1) en = k-1; float w[k]; for(int i=0; ilist[j].index[s1]; } /* target is residual err[] vector given path to this candidate */ for(i=0; ilist[0].index[num_stages-1-s]; } /* OK put it all back together using best survivor */ for(i=0; i