From 3532e024e2412048b172cdd4ffab79bd7cd1e87b Mon Sep 17 00:00:00 2001 From: drowe67 Date: Thu, 13 Jul 2023 09:58:21 +0930 Subject: files previously in misc, needed for ctests --- unittest/mksine.c | 54 ++++++++++ unittest/vq_mbest.c | 302 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 356 insertions(+) create mode 100644 unittest/mksine.c create mode 100644 unittest/vq_mbest.c (limited to 'unittest') diff --git a/unittest/mksine.c b/unittest/mksine.c new file mode 100644 index 0000000..fb5f822 --- /dev/null +++ b/unittest/mksine.c @@ -0,0 +1,54 @@ +/* + mksine.c + David Rowe + 10 Nov 2010 + + Creates a file of sine wave samples. +*/ + +#include +#include +#include +#include +#include +#include + +#define TWO_PI 6.283185307 +#define FS 8000.0 + +int main(int argc, char *argv[]) { + FILE *f; + int i,n; + float freq, length; + short *buf; + float amp = 1E4; + + if (argc < 4) { + printf("usage: %s outputFile frequencyHz lengthSecs [PeakAmp]\n", argv[0]); + exit(1); + } + + if (strcmp(argv[1], "-") == 0) { + f = stdout; + } else if ( (f = fopen(argv[1],"wb")) == NULL ) { + fprintf(stderr, "Error opening output file: %s: %s.\n", argv[3], strerror(errno)); + exit(1); + } + freq = atof(argv[2]); + length = atof(argv[3]); + if (argc == 5) amp = atof(argv[4]); + + n = length*FS; + buf = (short*)malloc(sizeof(short)*n); + assert(buf != NULL); + + for(i=0; i +#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