From: drowe67 Date: Mon, 30 Jul 2012 00:41:06 +0000 (+0000) Subject: working on phase VQ X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=311623eb4d1b6c8beaddf252e453f318959b8205;p=freetel-svn-tracking.git working on phase VQ git-svn-id: https://svn.code.sf.net/p/freetel/code@598 01035d8c-6547-0410-b346-abe4f91aad63 --- diff --git a/codec2-dev/unittest/Makefile.am b/codec2-dev/unittest/Makefile.am index 467c407f..ad2ca3ed 100644 --- a/codec2-dev/unittest/Makefile.am +++ b/codec2-dev/unittest/Makefile.am @@ -3,7 +3,7 @@ AUTOMAKE_OPTS = gnu NAME = libcodec2 AM_CPPFLAGS = $(AM_CFLAGS) -noinst_PROGRAMS = genres genlsp extract vqtrain vqtrainjnd tnlp tinterp tquant vq_train_jvm scalarlsptest tfdmdv t48_8 lspsync create_interleaver tlspsens tphinterp vqtrainph +noinst_PROGRAMS = genres genlsp extract vqtrain vqtrainjnd tnlp tinterp tquant vq_train_jvm scalarlsptest tfdmdv t48_8 lspsync create_interleaver tlspsens tphinterp vqtrainph genphdata polar2rect genres_SOURCES = genres.c ../src/lpc.c genres_LDADD = $(lib_LTLIBRARIES) @@ -29,6 +29,14 @@ vqtrainph_SOURCES = vqtrainph.c vqtrainph_LDADD = $(lib_LTLIBRARIES) vqtrainph_LDFLAGS = $(LIBS) +genphdata_SOURCES = genphdata.c +genphdata_LDADD = $(lib_LTLIBRARIES) +genphdata_LDFLAGS = $(LIBS) + +polar2rect_SOURCES = polar2rect.c +polar2rect_LDADD = $(lib_LTLIBRARIES) +polar2rect_LDFLAGS = $(LIBS) + vq_train_jvm_SOURCES = vq_train_jvm.c vq_train_jvm_LDADD = $(lib_LTLIBRARIES) vq_train_jvm_LDFLAGS = $(LIBS) diff --git a/codec2-dev/unittest/Makefile.in b/codec2-dev/unittest/Makefile.in index 15931523..f969a2e0 100644 --- a/codec2-dev/unittest/Makefile.in +++ b/codec2-dev/unittest/Makefile.in @@ -41,7 +41,8 @@ noinst_PROGRAMS = genres$(EXEEXT) genlsp$(EXEEXT) extract$(EXEEXT) \ tinterp$(EXEEXT) tquant$(EXEEXT) vq_train_jvm$(EXEEXT) \ scalarlsptest$(EXEEXT) tfdmdv$(EXEEXT) t48_8$(EXEEXT) \ lspsync$(EXEEXT) create_interleaver$(EXEEXT) tlspsens$(EXEEXT) \ - tphinterp$(EXEEXT) vqtrainph$(EXEEXT) + tphinterp$(EXEEXT) vqtrainph$(EXEEXT) genphdata$(EXEEXT) \ + polar2rect$(EXEEXT) subdir = unittest DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 @@ -60,6 +61,9 @@ extract_DEPENDENCIES = am_genlsp_OBJECTS = genlsp.$(OBJEXT) lpc.$(OBJEXT) lsp.$(OBJEXT) genlsp_OBJECTS = $(am_genlsp_OBJECTS) genlsp_DEPENDENCIES = +am_genphdata_OBJECTS = genphdata.$(OBJEXT) +genphdata_OBJECTS = $(am_genphdata_OBJECTS) +genphdata_DEPENDENCIES = am_genres_OBJECTS = genres.$(OBJEXT) lpc.$(OBJEXT) genres_OBJECTS = $(am_genres_OBJECTS) genres_DEPENDENCIES = @@ -74,6 +78,9 @@ am_lspsync_OBJECTS = lspsync.$(OBJEXT) quantise.$(OBJEXT) \ pack.$(OBJEXT) $(am__objects_1) lspsync_OBJECTS = $(am_lspsync_OBJECTS) lspsync_DEPENDENCIES = +am_polar2rect_OBJECTS = polar2rect.$(OBJEXT) +polar2rect_OBJECTS = $(am_polar2rect_OBJECTS) +polar2rect_DEPENDENCIES = am_scalarlsptest_OBJECTS = scalarlsptest.$(OBJEXT) quantise.$(OBJEXT) \ lpc.$(OBJEXT) lsp.$(OBJEXT) dump.$(OBJEXT) kiss_fft.$(OBJEXT) \ $(am__objects_1) @@ -136,13 +143,15 @@ CCLD = $(CC) LINK = $(LIBTOOL) --tag=CC --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ $(AM_LDFLAGS) $(LDFLAGS) -o $@ SOURCES = $(create_interleaver_SOURCES) $(extract_SOURCES) \ - $(genlsp_SOURCES) $(genres_SOURCES) $(lspsync_SOURCES) \ + $(genlsp_SOURCES) $(genphdata_SOURCES) $(genres_SOURCES) \ + $(lspsync_SOURCES) $(polar2rect_SOURCES) \ $(scalarlsptest_SOURCES) $(t48_8_SOURCES) $(tfdmdv_SOURCES) \ $(tinterp_SOURCES) $(tlspsens_SOURCES) $(tnlp_SOURCES) \ $(tphinterp_SOURCES) $(tquant_SOURCES) $(vq_train_jvm_SOURCES) \ $(vqtrain_SOURCES) $(vqtrainjnd_SOURCES) $(vqtrainph_SOURCES) DIST_SOURCES = $(create_interleaver_SOURCES) $(extract_SOURCES) \ - $(genlsp_SOURCES) $(genres_SOURCES) $(lspsync_SOURCES) \ + $(genlsp_SOURCES) $(genphdata_SOURCES) $(genres_SOURCES) \ + $(lspsync_SOURCES) $(polar2rect_SOURCES) \ $(scalarlsptest_SOURCES) $(t48_8_SOURCES) $(tfdmdv_SOURCES) \ $(tinterp_SOURCES) $(tlspsens_SOURCES) $(tnlp_SOURCES) \ $(tphinterp_SOURCES) $(tquant_SOURCES) $(vq_train_jvm_SOURCES) \ @@ -276,6 +285,12 @@ vqtrainjnd_LDFLAGS = $(LIBS) vqtrainph_SOURCES = vqtrainph.c vqtrainph_LDADD = $(lib_LTLIBRARIES) vqtrainph_LDFLAGS = $(LIBS) +genphdata_SOURCES = genphdata.c +genphdata_LDADD = $(lib_LTLIBRARIES) +genphdata_LDFLAGS = $(LIBS) +polar2rect_SOURCES = polar2rect.c +polar2rect_LDADD = $(lib_LTLIBRARIES) +polar2rect_LDFLAGS = $(LIBS) vq_train_jvm_SOURCES = vq_train_jvm.c vq_train_jvm_LDADD = $(lib_LTLIBRARIES) vq_train_jvm_LDFLAGS = $(LIBS) @@ -361,12 +376,18 @@ extract$(EXEEXT): $(extract_OBJECTS) $(extract_DEPENDENCIES) genlsp$(EXEEXT): $(genlsp_OBJECTS) $(genlsp_DEPENDENCIES) @rm -f genlsp$(EXEEXT) $(LINK) $(genlsp_LDFLAGS) $(genlsp_OBJECTS) $(genlsp_LDADD) $(LIBS) +genphdata$(EXEEXT): $(genphdata_OBJECTS) $(genphdata_DEPENDENCIES) + @rm -f genphdata$(EXEEXT) + $(LINK) $(genphdata_LDFLAGS) $(genphdata_OBJECTS) $(genphdata_LDADD) $(LIBS) genres$(EXEEXT): $(genres_OBJECTS) $(genres_DEPENDENCIES) @rm -f genres$(EXEEXT) $(LINK) $(genres_LDFLAGS) $(genres_OBJECTS) $(genres_LDADD) $(LIBS) lspsync$(EXEEXT): $(lspsync_OBJECTS) $(lspsync_DEPENDENCIES) @rm -f lspsync$(EXEEXT) $(LINK) $(lspsync_LDFLAGS) $(lspsync_OBJECTS) $(lspsync_LDADD) $(LIBS) +polar2rect$(EXEEXT): $(polar2rect_OBJECTS) $(polar2rect_DEPENDENCIES) + @rm -f polar2rect$(EXEEXT) + $(LINK) $(polar2rect_LDFLAGS) $(polar2rect_OBJECTS) $(polar2rect_LDADD) $(LIBS) scalarlsptest$(EXEEXT): $(scalarlsptest_OBJECTS) $(scalarlsptest_DEPENDENCIES) @rm -f scalarlsptest$(EXEEXT) $(LINK) $(scalarlsptest_LDFLAGS) $(scalarlsptest_OBJECTS) $(scalarlsptest_LDADD) $(LIBS) @@ -423,6 +444,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/extract.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fdmdv.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/genlsp.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/genphdata.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/genres.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/interp.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/kiss_fft.Po@am__quote@ @@ -433,6 +455,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/octave.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pack.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/phase.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/polar2rect.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/postfilter.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/quantise.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/scalarlsptest.Po@am__quote@ diff --git a/codec2-dev/unittest/extract.c b/codec2-dev/unittest/extract.c index 77196248..15eb9344 100644 --- a/codec2-dev/unittest/extract.c +++ b/codec2-dev/unittest/extract.c @@ -28,10 +28,11 @@ along with this program; if not, see . */ -#define MAX_STR 256 /* maximum string length */ +#define MAX_STR 2048 /* maximum string length */ #include #include +#include #include #include @@ -110,9 +111,11 @@ void scan_line(FILE *fp, float f[], int n) char s[MAX_STR]; char *ps,*pe; int i; - + + memset(s, 0, MAX_STR); ps = pe = fgets(s,MAX_STR,fp); - assert(ps); + if (ps == NULL) + return; for(i=0; i +#include +#include +#include +#include +#include + +typedef struct { + float real; + float imag; +} COMP; + +#define N 100000 +#define K 2 +#define M 8 +#define PI 3.141592654 /* mathematical constant */ +#define TWO_PI 6.283185307 /* mathematical constant */ + +int main(void) { + FILE *f=fopen("testph.flt", "wb"); + int i; + float angle; + COMP c; + + #ifdef TEST1 + for(i=0; i +#include +#include +#include + +typedef struct { + float real; + float imag; +} COMP; + +int main(int argc, char *argv[]) { + FILE *fpolar; + FILE *frect; + float polar; + COMP rect; + + if (argc != 3) { + printf("usage: %s polarFile rectFile\n", argv[0]); + exit(0); + } + + fpolar = fopen(argv[1], "rb"); + assert(fpolar != NULL); + frect = fopen(argv[2], "wb"); + assert(frect != NULL); + + while (fread(&polar, sizeof(float), 1, fpolar) != 0) { + if (polar == 0.0) { + /* this values indicates the VQ training should ignore + this vector element. It's not a valid phase as it + doesn't have mangitude of 1.0 */ + rect.real = 0.0; + rect.imag = 0.0; + } + else { + rect.real = cos(polar); + rect.imag = sin(polar); + } + fwrite(&rect, sizeof(COMP), 1, frect); + } + + fclose(fpolar); + fclose(frect); + + return 0; +} diff --git a/codec2-dev/unittest/vqtrainph.c b/codec2-dev/unittest/vqtrainph.c index fc7e639c..9a882d09 100644 --- a/codec2-dev/unittest/vqtrainph.c +++ b/codec2-dev/unittest/vqtrainph.c @@ -52,6 +52,7 @@ typedef struct { #define DELTAQ 0.01 /* quiting distortion */ #define MAX_STR 80 /* maximum string length */ +#define PI 3.141592654 /*-----------------------------------------------------------------------*\ @@ -59,11 +60,11 @@ typedef struct { \*-----------------------------------------------------------------------*/ -void zero(COMP v[], int k); -void acc(COMP v1[], COMP v2[], int k); -void norm(COMP v[], int k, int n); -int quantise(COMP cb[], COMP vec[], int k, int m, float *se); -void print_vec(COMP cb[], int k, int m); +void zero(COMP v[], int d); +void acc(COMP v1[], COMP v2[], int d); +void norm(COMP v[], int k); +int quantise(COMP cb[], COMP vec[], int d, int e, float *se); +void print_vec(COMP cb[], int d, int e); /*-----------------------------------------------------------------------* \ @@ -72,25 +73,29 @@ void print_vec(COMP cb[], int k, int m); \*-----------------------------------------------------------------------*/ int main(int argc, char *argv[]) { - int k,m; /* dimension and codebook size */ + int d,e; /* dimension and codebook size */ COMP *vec; /* current vector */ COMP *cb; /* vector codebook */ COMP *cent; /* centroids for each codebook entry */ int *n; /* number of vectors in this interval */ int J; /* number of vectors in training set */ int ind; /* index of current vector */ - float se; /* squared error for this iteration */ - float Dn,Dn_1; /* current and previous iterations distortion */ + float se; /* total squared error for this iteration */ + float var; /* variance */ + float var_1; /* previous variance */ float delta; /* improvement in distortion */ FILE *ftrain; /* file containing training set */ FILE *fvq; /* file containing vector quantiser */ int ret; - int i,j; + int i,j, finished, iterations; + float b; /* equivalent number of bits */ + float improvement; + float sd_vec, sd_element, sd_theory, bits_theory; /* Interpret command line arguments */ if (argc != 5) { - printf("usage: %s TrainFile K(dimension) M(codebook size) VQFile\n", argv[0]); + printf("usage: %s TrainFile D(dimension) E(number of entries) VQFile\n", argv[0]); exit(1); } @@ -104,13 +109,13 @@ int main(int argc, char *argv[]) { /* determine k and m, and allocate arrays */ - k = atol(argv[2]); - m = atol(argv[3]); - printf("dimension K=%d number of entries M=%d\n", k, m); - vec = (COMP*)malloc(sizeof(COMP)*k); - cb = (COMP*)malloc(sizeof(COMP)*k*m); - cent = (COMP*)malloc(sizeof(COMP)*k*m); - n = (int*)malloc(sizeof(int)*m); + d = atoi(argv[2]); + e = atoi(argv[3]); + printf("dimension D=%d number of entries E=%d\n", d, e); + vec = (COMP*)malloc(sizeof(COMP)*d); + cb = (COMP*)malloc(sizeof(COMP)*d*e); + cent = (COMP*)malloc(sizeof(COMP)*d*e); + n = (int*)malloc(sizeof(int)*e); if (cb == NULL || cb == NULL || cent == NULL || vec == NULL) { printf("Error in malloc.\n"); exit(1); @@ -119,27 +124,33 @@ int main(int argc, char *argv[]) { /* determine size of training set */ J = 0; - while(fread(vec, sizeof(COMP), k, ftrain) == (size_t)k) + while(fread(vec, sizeof(COMP), d, ftrain) == (size_t)d) J++; printf("J=%d entries in training set\n", J); /* set up initial codebook state from samples of training set */ rewind(ftrain); - ret = fread(cb, sizeof(COMP), k*m, ftrain); - //print_vec(cb, k, m); + ret = fread(cb, sizeof(COMP), d*e, ftrain); /* main loop */ - Dn = 1E32; - j = 1; - do { - Dn_1 = Dn; + printf("Iteration delta std dev std dev std dev (theory) improvement\n"); + printf(" (per vec) (per element) (per element) (bits)\n"); + + b = log10((float)e)/log10(2.0); + sd_theory = (PI/sqrt(3.0))*pow(2.0, -b/(float)d); + + iterations = 0; + finished = 0; + delta = 0; + var_1 = 0.0; + do { /* zero centroids */ - for(i=0; i DELTAQ); + iterations++; + if (iterations > 1) { + delta = (var_1 - var)/var; + if (delta < DELTAQ) + finished = 1; + } + + printf("%2d %4.3f %4.3f %4.3f %4.3f % 4.3f\n",iterations, delta, sd_vec, sd_element, sd_theory, improvement); + + var_1 = var; + + } while (!finished); + + /* TODO: measure variance per element to ensure sd's about the same */ /* save codebook to disk */ @@ -177,10 +203,10 @@ int main(int argc, char *argv[]) { exit(1); } - fprintf(fvq,"%d %d\n",k,m); - for(j=0; j