diff --git a/libs/libcodec2/unittest/vqtrainjnd.c b/libs/libcodec2/unittest/vqtrainjnd.c new file mode 100644 index 0000000000..f0fb18a329 --- /dev/null +++ b/libs/libcodec2/unittest/vqtrainjnd.c @@ -0,0 +1,254 @@ +/*--------------------------------------------------------------------------*\ + + FILE........: vqtrainjnd.c + AUTHOR......: David Rowe + DATE CREATED: 10 Nov 2011 + + This program trains vector quantisers for LSPs using an + experimental, but very simple Just Noticable Difference (JND) + algorithm: + + - we quantise each training vector to JND steps (say 100Hz for LSPs + 5-10) + - we then use the most popular training vectors as our VQ codebook + +\*--------------------------------------------------------------------------*/ + +/* + Copyright (C) 2011 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +/*-----------------------------------------------------------------------*\ + + INCLUDES + +\*-----------------------------------------------------------------------*/ + +#include +#include +#include +#include +#include + +/*-----------------------------------------------------------------------*\ + + DEFINES + +\*-----------------------------------------------------------------------*/ + +#define PI 3.141592654 /* mathematical constant */ +#define MAX_POP 10 + +/*-----------------------------------------------------------------------*\ + + FUNCTION PROTOTYPES + +\*-----------------------------------------------------------------------*/ + +void zero(float v[], int k); +void acc(float v1[], float v2[], int k); +void norm(float v[], int k, long n); +void locate_lsps_jnd_steps(float lsps[], float step, int k); + +/*-----------------------------------------------------------------------* \ + + MAIN + +\*-----------------------------------------------------------------------*/ + +int main(int argc, char *argv[]) { + int k; /* dimension and codebook size */ + float *vec; /* current vector */ + int *n; /* number of vectors in this interval */ + int J; /* number of vectors in training set */ + int i,j; + FILE *ftrain; /* file containing training set */ + float *train; /* training database */ + //float *pend_train; /* last entry */ + float *pt; + int ntrain, match, vec_exists, vec_index=0, entry; + int popular[MAX_POP], pop_thresh; + FILE *fvq; + float jnd; + + /* Interpret command line arguments */ + + if (argc != 6) { + printf("usage: %s TrainFile K(dimension) JND popThresh VQFile\n", + argv[0]); + exit(1); + } + + /* Open training file */ + + ftrain = fopen(argv[1],"rb"); + if (ftrain == NULL) { + printf("Error opening training database file: %s\n",argv[1]); + exit(1); + } + + /* determine k and m, and allocate arrays */ + + k = atol(argv[2]); + jnd = atof(argv[3]); + pop_thresh = atol(argv[4]); + printf("dimension K=%d popThresh=%d JND=%3.1f Hz\n", + k, pop_thresh, jnd); + vec = (float*)malloc(sizeof(float)*k); + if (vec == NULL) { + printf("Error in malloc.\n"); + exit(1); + } + + /* determine size of training set */ + + J = 0; + while(fread(vec, sizeof(float), k, ftrain) == (size_t)k) + J++; + printf("J=%d entries in training set\n", J); + train = (float*)malloc(sizeof(float)*k*J); + if (train == NULL) { + printf("Error in malloc.\n"); + exit(1); + } + printf("training array is %d bytes\n", sizeof(float)*k*J); + + n = (int*)malloc(sizeof(int)*J); + if (n == NULL) { + printf("Error in malloc.\n"); + exit(1); + } + for(i=0; i pop_thresh) { + for(j=0; j