support for new ampl VQ experiments, aplogies if this breaks anything
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 1 Apr 2011 00:22:44 +0000 (00:22 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 1 Apr 2011 00:22:44 +0000 (00:22 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@284 01035d8c-6547-0410-b346-abe4f91aad63

codec2/unittest/Makefile.am
codec2/unittest/Makefile.in
codec2/unittest/dvdongle2.c
codec2/unittest/genbands.c [new file with mode: 0644]
codec2/unittest/tcodec2.c
codec2/unittest/vq_clip_test.c [new file with mode: 0644]
codec2/unittest/vqtrain_clip.c [new file with mode: 0644]

index 817e6a7891a62977474d90dcab5ceda48f67dd35..310436d20713da5c9c274d02c7b9d860b1add822 100644 (file)
@@ -3,7 +3,7 @@ AUTOMAKE_OPTS = gnu
 NAME = libcodec2
 AM_CPPFLAGS = $(AM_CFLAGS)
 
-bin_PROGRAMS = genres genlsp extract vqtrain tnlp tinterp tquant tcodec2
+bin_PROGRAMS = genres genlsp extract vqtrain vqtrain_clip tnlp tinterp tquant tcodec2 genbands vq_clip_test
 
 
 genres_SOURCES = genres.c ../src/lpc.c
@@ -22,6 +22,10 @@ vqtrain_SOURCES = vqtrain.c
 vqtrain_LDADD = $(lib_LTLIBRARIES) 
 vqtrain_LDFLAGS = $(LIBS)
 
+vqtrain_clip_SOURCES = vqtrain_clip.c
+vqtrain_clip_LDADD = $(lib_LTLIBRARIES) 
+vqtrain_clip_LDFLAGS = $(LIBS)
+
 tnlp_SOURCES = tnlp.c ../src/sine.c ../src/nlp.c ../src/fft.c ../src/kiss_fft.c ../src/dump.c
 tnlp_LDADD = $(lib_LTLIBRARIES) 
 tnlp_LDFLAGS = $(LIBS)
@@ -40,3 +44,15 @@ tcodec2_SOURCES = tcodec2.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/d
 tcodec2_LDADD = $(lib_LTLIBRARIES) 
 tcodec2_LDFLAGS = $(LIBS)
 
+genbands_SOURCES = genbands.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/fft.c ../src/kiss_fft.c \
+../src/codec2.c ../src/sine.c ../src/nlp.c ../src/postfilter.c ../src/phase.c ../src/interp.c \
+../src/codebook.c ../src/codebookd.c ../src/codebookdvq.c ../src/pack.c
+genbands_LDADD = $(lib_LTLIBRARIES) 
+genbands_LDFLAGS = $(LIBS)
+
+vq_clip_test_SOURCES = vq_clip_test.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/fft.c ../src/kiss_fft.c \
+../src/codec2.c ../src/sine.c ../src/nlp.c ../src/postfilter.c ../src/phase.c ../src/interp.c \
+../src/codebook.c ../src/codebookd.c ../src/codebookdvq.c ../src/pack.c
+vq_clip_test_LDADD = $(lib_LTLIBRARIES) 
+vq_clip_test_LDFLAGS = $(LIBS)
+
index 21d19fea21038fccfbb7510b160ae135782d853e..810b2ccd7ea4a154ad9faa909caf8802f477dd3f 100644 (file)
@@ -37,8 +37,9 @@ POST_UNINSTALL = :
 build_triplet = @build@
 host_triplet = @host@
 bin_PROGRAMS = genres$(EXEEXT) genlsp$(EXEEXT) extract$(EXEEXT) \
-       vqtrain$(EXEEXT) tnlp$(EXEEXT) tinterp$(EXEEXT) \
-       tquant$(EXEEXT) tcodec2$(EXEEXT)
+       vqtrain$(EXEEXT) vqtrain_clip$(EXEEXT) tnlp$(EXEEXT) \
+       tinterp$(EXEEXT) tquant$(EXEEXT) tcodec2$(EXEEXT) \
+       genbands$(EXEEXT) vq_clip_test$(EXEEXT)
 subdir = unittest
 DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@@ -53,6 +54,14 @@ PROGRAMS = $(bin_PROGRAMS)
 am_extract_OBJECTS = extract.$(OBJEXT)
 extract_OBJECTS = $(am_extract_OBJECTS)
 extract_DEPENDENCIES =
+am_genbands_OBJECTS = genbands.$(OBJEXT) quantise.$(OBJEXT) \
+       lpc.$(OBJEXT) lsp.$(OBJEXT) dump.$(OBJEXT) fft.$(OBJEXT) \
+       kiss_fft.$(OBJEXT) codec2.$(OBJEXT) sine.$(OBJEXT) \
+       nlp.$(OBJEXT) postfilter.$(OBJEXT) phase.$(OBJEXT) \
+       interp.$(OBJEXT) codebook.$(OBJEXT) codebookd.$(OBJEXT) \
+       codebookdvq.$(OBJEXT) pack.$(OBJEXT)
+genbands_OBJECTS = $(am_genbands_OBJECTS)
+genbands_DEPENDENCIES =
 am_genlsp_OBJECTS = genlsp.$(OBJEXT) lpc.$(OBJEXT) lsp.$(OBJEXT)
 genlsp_OBJECTS = $(am_genlsp_OBJECTS)
 genlsp_DEPENDENCIES =
@@ -82,9 +91,20 @@ am_tquant_OBJECTS = tquant.$(OBJEXT) quantise.$(OBJEXT) lpc.$(OBJEXT) \
        codebook.$(OBJEXT) codebookd.$(OBJEXT) codebookdvq.$(OBJEXT)
 tquant_OBJECTS = $(am_tquant_OBJECTS)
 tquant_DEPENDENCIES =
+am_vq_clip_test_OBJECTS = vq_clip_test.$(OBJEXT) quantise.$(OBJEXT) \
+       lpc.$(OBJEXT) lsp.$(OBJEXT) dump.$(OBJEXT) fft.$(OBJEXT) \
+       kiss_fft.$(OBJEXT) codec2.$(OBJEXT) sine.$(OBJEXT) \
+       nlp.$(OBJEXT) postfilter.$(OBJEXT) phase.$(OBJEXT) \
+       interp.$(OBJEXT) codebook.$(OBJEXT) codebookd.$(OBJEXT) \
+       codebookdvq.$(OBJEXT) pack.$(OBJEXT)
+vq_clip_test_OBJECTS = $(am_vq_clip_test_OBJECTS)
+vq_clip_test_DEPENDENCIES =
 am_vqtrain_OBJECTS = vqtrain.$(OBJEXT)
 vqtrain_OBJECTS = $(am_vqtrain_OBJECTS)
 vqtrain_DEPENDENCIES =
+am_vqtrain_clip_OBJECTS = vqtrain_clip.$(OBJEXT)
+vqtrain_clip_OBJECTS = $(am_vqtrain_clip_OBJECTS)
+vqtrain_clip_DEPENDENCIES =
 DEFAULT_INCLUDES = -I. -I$(srcdir)
 depcomp = $(SHELL) $(top_srcdir)/depcomp
 am__depfiles_maybe = depfiles
@@ -96,12 +116,15 @@ LTCOMPILE = $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) \
 CCLD = $(CC)
 LINK = $(LIBTOOL) --tag=CC --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
        $(AM_LDFLAGS) $(LDFLAGS) -o $@
-SOURCES = $(extract_SOURCES) $(genlsp_SOURCES) $(genres_SOURCES) \
-       $(tcodec2_SOURCES) $(tinterp_SOURCES) $(tnlp_SOURCES) \
-       $(tquant_SOURCES) $(vqtrain_SOURCES)
-DIST_SOURCES = $(extract_SOURCES) $(genlsp_SOURCES) $(genres_SOURCES) \
-       $(tcodec2_SOURCES) $(tinterp_SOURCES) $(tnlp_SOURCES) \
-       $(tquant_SOURCES) $(vqtrain_SOURCES)
+SOURCES = $(extract_SOURCES) $(genbands_SOURCES) $(genlsp_SOURCES) \
+       $(genres_SOURCES) $(tcodec2_SOURCES) $(tinterp_SOURCES) \
+       $(tnlp_SOURCES) $(tquant_SOURCES) $(vq_clip_test_SOURCES) \
+       $(vqtrain_SOURCES) $(vqtrain_clip_SOURCES)
+DIST_SOURCES = $(extract_SOURCES) $(genbands_SOURCES) \
+       $(genlsp_SOURCES) $(genres_SOURCES) $(tcodec2_SOURCES) \
+       $(tinterp_SOURCES) $(tnlp_SOURCES) $(tquant_SOURCES) \
+       $(vq_clip_test_SOURCES) $(vqtrain_SOURCES) \
+       $(vqtrain_clip_SOURCES)
 ETAGS = etags
 CTAGS = ctags
 DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@@ -225,6 +248,9 @@ extract_LDFLAGS = $(LIBS)
 vqtrain_SOURCES = vqtrain.c
 vqtrain_LDADD = $(lib_LTLIBRARIES) 
 vqtrain_LDFLAGS = $(LIBS)
+vqtrain_clip_SOURCES = vqtrain_clip.c
+vqtrain_clip_LDADD = $(lib_LTLIBRARIES) 
+vqtrain_clip_LDFLAGS = $(LIBS)
 tnlp_SOURCES = tnlp.c ../src/sine.c ../src/nlp.c ../src/fft.c ../src/kiss_fft.c ../src/dump.c
 tnlp_LDADD = $(lib_LTLIBRARIES) 
 tnlp_LDFLAGS = $(LIBS)
@@ -240,6 +266,18 @@ tcodec2_SOURCES = tcodec2.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/d
 
 tcodec2_LDADD = $(lib_LTLIBRARIES) 
 tcodec2_LDFLAGS = $(LIBS)
+genbands_SOURCES = genbands.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/fft.c ../src/kiss_fft.c \
+../src/codec2.c ../src/sine.c ../src/nlp.c ../src/postfilter.c ../src/phase.c ../src/interp.c \
+../src/codebook.c ../src/codebookd.c ../src/codebookdvq.c ../src/pack.c
+
+genbands_LDADD = $(lib_LTLIBRARIES) 
+genbands_LDFLAGS = $(LIBS)
+vq_clip_test_SOURCES = vq_clip_test.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/fft.c ../src/kiss_fft.c \
+../src/codec2.c ../src/sine.c ../src/nlp.c ../src/postfilter.c ../src/phase.c ../src/interp.c \
+../src/codebook.c ../src/codebookd.c ../src/codebookdvq.c ../src/pack.c
+
+vq_clip_test_LDADD = $(lib_LTLIBRARIES) 
+vq_clip_test_LDFLAGS = $(LIBS)
 all: all-am
 
 .SUFFIXES:
@@ -304,6 +342,9 @@ clean-binPROGRAMS:
 extract$(EXEEXT): $(extract_OBJECTS) $(extract_DEPENDENCIES) 
        @rm -f extract$(EXEEXT)
        $(LINK) $(extract_LDFLAGS) $(extract_OBJECTS) $(extract_LDADD) $(LIBS)
+genbands$(EXEEXT): $(genbands_OBJECTS) $(genbands_DEPENDENCIES) 
+       @rm -f genbands$(EXEEXT)
+       $(LINK) $(genbands_LDFLAGS) $(genbands_OBJECTS) $(genbands_LDADD) $(LIBS)
 genlsp$(EXEEXT): $(genlsp_OBJECTS) $(genlsp_DEPENDENCIES) 
        @rm -f genlsp$(EXEEXT)
        $(LINK) $(genlsp_LDFLAGS) $(genlsp_OBJECTS) $(genlsp_LDADD) $(LIBS)
@@ -322,9 +363,15 @@ tnlp$(EXEEXT): $(tnlp_OBJECTS) $(tnlp_DEPENDENCIES)
 tquant$(EXEEXT): $(tquant_OBJECTS) $(tquant_DEPENDENCIES) 
        @rm -f tquant$(EXEEXT)
        $(LINK) $(tquant_LDFLAGS) $(tquant_OBJECTS) $(tquant_LDADD) $(LIBS)
+vq_clip_test$(EXEEXT): $(vq_clip_test_OBJECTS) $(vq_clip_test_DEPENDENCIES) 
+       @rm -f vq_clip_test$(EXEEXT)
+       $(LINK) $(vq_clip_test_LDFLAGS) $(vq_clip_test_OBJECTS) $(vq_clip_test_LDADD) $(LIBS)
 vqtrain$(EXEEXT): $(vqtrain_OBJECTS) $(vqtrain_DEPENDENCIES) 
        @rm -f vqtrain$(EXEEXT)
        $(LINK) $(vqtrain_LDFLAGS) $(vqtrain_OBJECTS) $(vqtrain_LDADD) $(LIBS)
+vqtrain_clip$(EXEEXT): $(vqtrain_clip_OBJECTS) $(vqtrain_clip_DEPENDENCIES) 
+       @rm -f vqtrain_clip$(EXEEXT)
+       $(LINK) $(vqtrain_clip_LDFLAGS) $(vqtrain_clip_OBJECTS) $(vqtrain_clip_LDADD) $(LIBS)
 
 mostlyclean-compile:
        -rm -f *.$(OBJEXT)
@@ -339,6 +386,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dump.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/extract.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fft.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/genbands.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/genlsp.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@
@@ -355,7 +403,9 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tinterp.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tnlp.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tquant.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vq_clip_test.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vqtrain.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vqtrain_clip.Po@am__quote@
 
 .c.o:
 @am__fastdepCC_TRUE@   if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ $<; \
@@ -378,6 +428,20 @@ distclean-compile:
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(LTCOMPILE) -c -o $@ $<
 
+quantise.o: ../src/quantise.c
+@am__fastdepCC_TRUE@   if $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT quantise.o -MD -MP -MF "$(DEPDIR)/quantise.Tpo" -c -o quantise.o `test -f '../src/quantise.c' || echo '$(srcdir)/'`../src/quantise.c; \
+@am__fastdepCC_TRUE@   then mv -f "$(DEPDIR)/quantise.Tpo" "$(DEPDIR)/quantise.Po"; else rm -f "$(DEPDIR)/quantise.Tpo"; exit 1; fi
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='../src/quantise.c' object='quantise.o' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o quantise.o `test -f '../src/quantise.c' || echo '$(srcdir)/'`../src/quantise.c
+
+quantise.obj: ../src/quantise.c
+@am__fastdepCC_TRUE@   if $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT quantise.obj -MD -MP -MF "$(DEPDIR)/quantise.Tpo" -c -o quantise.obj `if test -f '../src/quantise.c'; then $(CYGPATH_W) '../src/quantise.c'; else $(CYGPATH_W) '$(srcdir)/../src/quantise.c'; fi`; \
+@am__fastdepCC_TRUE@   then mv -f "$(DEPDIR)/quantise.Tpo" "$(DEPDIR)/quantise.Po"; else rm -f "$(DEPDIR)/quantise.Tpo"; exit 1; fi
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='../src/quantise.c' object='quantise.obj' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@  $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o quantise.obj `if test -f '../src/quantise.c'; then $(CYGPATH_W) '../src/quantise.c'; else $(CYGPATH_W) '$(srcdir)/../src/quantise.c'; fi`
+
 lpc.o: ../src/lpc.c
 @am__fastdepCC_TRUE@   if $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT lpc.o -MD -MP -MF "$(DEPDIR)/lpc.Tpo" -c -o lpc.o `test -f '../src/lpc.c' || echo '$(srcdir)/'`../src/lpc.c; \
 @am__fastdepCC_TRUE@   then mv -f "$(DEPDIR)/lpc.Tpo" "$(DEPDIR)/lpc.Po"; else rm -f "$(DEPDIR)/lpc.Tpo"; exit 1; fi
@@ -406,20 +470,6 @@ lsp.obj: ../src/lsp.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@  $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o lsp.obj `if test -f '../src/lsp.c'; then $(CYGPATH_W) '../src/lsp.c'; else $(CYGPATH_W) '$(srcdir)/../src/lsp.c'; fi`
 
-quantise.o: ../src/quantise.c
-@am__fastdepCC_TRUE@   if $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT quantise.o -MD -MP -MF "$(DEPDIR)/quantise.Tpo" -c -o quantise.o `test -f '../src/quantise.c' || echo '$(srcdir)/'`../src/quantise.c; \
-@am__fastdepCC_TRUE@   then mv -f "$(DEPDIR)/quantise.Tpo" "$(DEPDIR)/quantise.Po"; else rm -f "$(DEPDIR)/quantise.Tpo"; exit 1; fi
-@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='../src/quantise.c' object='quantise.o' libtool=no @AMDEPBACKSLASH@
-@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
-@am__fastdepCC_FALSE@  $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o quantise.o `test -f '../src/quantise.c' || echo '$(srcdir)/'`../src/quantise.c
-
-quantise.obj: ../src/quantise.c
-@am__fastdepCC_TRUE@   if $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT quantise.obj -MD -MP -MF "$(DEPDIR)/quantise.Tpo" -c -o quantise.obj `if test -f '../src/quantise.c'; then $(CYGPATH_W) '../src/quantise.c'; else $(CYGPATH_W) '$(srcdir)/../src/quantise.c'; fi`; \
-@am__fastdepCC_TRUE@   then mv -f "$(DEPDIR)/quantise.Tpo" "$(DEPDIR)/quantise.Po"; else rm -f "$(DEPDIR)/quantise.Tpo"; exit 1; fi
-@AMDEP_TRUE@@am__fastdepCC_FALSE@      source='../src/quantise.c' object='quantise.obj' libtool=no @AMDEPBACKSLASH@
-@AMDEP_TRUE@@am__fastdepCC_FALSE@      DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
-@am__fastdepCC_FALSE@  $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o quantise.obj `if test -f '../src/quantise.c'; then $(CYGPATH_W) '../src/quantise.c'; else $(CYGPATH_W) '$(srcdir)/../src/quantise.c'; fi`
-
 dump.o: ../src/dump.c
 @am__fastdepCC_TRUE@   if $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT dump.o -MD -MP -MF "$(DEPDIR)/dump.Tpo" -c -o dump.o `test -f '../src/dump.c' || echo '$(srcdir)/'`../src/dump.c; \
 @am__fastdepCC_TRUE@   then mv -f "$(DEPDIR)/dump.Tpo" "$(DEPDIR)/dump.Po"; else rm -f "$(DEPDIR)/dump.Tpo"; exit 1; fi
index 792b3fc1062dc7358340b1786199d7fb3735df7a..f39ef53a0b957c82c341158ded205917050ccac7 100644 (file)
@@ -288,7 +288,7 @@ int main(int argc, char **argv) {
     c_in.power     = 0x0;
     c_in.control1  = 0x0;
 
-#define RATE2000
+    //#define RATE2000
 #ifdef RATE2000
     c_in.rate[0]   = 0x0028;  /* 2000 bit/s, no FEC */
     c_in.rate[1]   = 0x0000;
@@ -297,6 +297,7 @@ int main(int argc, char **argv) {
     c_in.rate[4]   = 0x6248;
 #endif
 
+#define RATE3600_1200
 #ifdef RATE3600_1200
     c_in.rate[0]   = 0x5048;  /* 3600 bit/s, 1200 bit/s FEC */
     c_in.rate[1]   = 0x0001;
diff --git a/codec2/unittest/genbands.c b/codec2/unittest/genbands.c
new file mode 100644 (file)
index 0000000..e57655e
--- /dev/null
@@ -0,0 +1,229 @@
+/*--------------------------------------------------------------------------*\
+
+       FILE........: genbands.c
+       AUTHOR......: David Rowe
+       DATE CREATED: 22 March 2011
+
+       This program generates a text file of resampled band data from
+       an input speech file.  Note: choose a better name and reword
+       this some time.
+
+\*--------------------------------------------------------------------------*/
+
+/*
+  Copyright (C) 2009 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 <http://www.gnu.org/licenses/>.
+*/
+
+#define P      10      /* LP order                                     */
+#define LSP_DELTA1 0.01 /* grid spacing for LSP root searches */
+#define NW     279     /* frame size in samples                        */
+#define        N       80      /* frame to frame shift                         */
+#define THRESH 40.0    /* threshold energy/sample for frame inclusion  */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include "defines.h"
+#include "comp.h"
+#include "codec2_internal.h"   
+#include "codec2.h"    
+#include "interp.h"    
+
+int switch_present(sw,argc,argv)
+  char sw[];     /* switch in string form */
+  int argc;      /* number of command line arguments */
+  char *argv[];  /* array of command line arguments in string form */
+{
+  int i;       /* loop variable */
+
+  for(i=1; i<argc; i++)
+    if (!strcmp(sw,argv[i]))
+      return(i);
+
+  return 0;
+}
+
+int main(int argc, char *argv[]) {
+    FILE   *fraw, *fdeltat;
+    short   buf[N];
+    void   *c2;
+    CODEC2 *c3;
+    MODEL   model;
+    float w[MAX_AMP], A[MAX_AMP];
+    float wres[MAX_AMP], Ares[MAX_AMP], AresdB[MAX_AMP],AresdB_prev[MAX_AMP];
+    float deltat[MAX_AMP];
+    float sam, E;
+    long  af;
+    int   i;
+
+    if (argc < 3) {
+       printf("usage: gentest RawFile DeltatBandsTextFile\n");
+       exit(0);
+    }
+
+    af = 0;
+
+    c2 = codec2_create();
+    c3 = (CODEC2*)c2;
+
+    for(i=0; i<RES_POINTS; i++)
+       AresdB_prev[i] = 0.0; 
+
+    /* Open files */
+
+    fraw = fopen(argv[1],"rb");
+    if (fraw == NULL) {
+       printf("Error opening input .raw file: %s",argv[1]);
+       exit(1);
+    }
+
+    fdeltat = fopen(argv[2],"wt");
+    if (fdeltat == NULL) {
+       printf("Error opening output text file: %s",argv[2]);
+       exit(1);
+    }
+
+    while(fread(buf, sizeof(short), N, fraw) == N) {
+       analyse_one_frame(c3, &model, buf);
+
+       E = 0.0;
+       for(i=0; i<N; i++) {
+           sam = (float)buf[i];
+           E += sam*sam;
+       }
+       E = 10.0*log10(E/N);
+
+       /* If energy high enough, include this frame */
+
+       if (E > THRESH) {
+           af++;
+           printf("Active Frame: %ld\n", af);
+
+           resample_amp_fixed(&model, w, A, wres, Ares,
+                       AresdB_prev, AresdB, deltat);
+           for(i=0; i<RES_POINTS; i++)
+               fprintf(fdeltat,"%f ", deltat[i]);
+           fprintf(fdeltat, "\n");
+
+           for(i=0; i<RES_POINTS; i++)
+               AresdB_prev[i] = AresdB[i];         
+       }
+    }
+
+    codec2_destroy(c2);
+
+    fclose(fraw);
+    fclose(fdeltat);
+
+    return 0;
+}
+
+#ifdef DELETE_ME
+
+    FILE   *fspc;      /* input file ptr for test database             */
+    FILE   *flsp;      /* output text file of LSPs                     */
+    short  buf[N];     /* input frame of speech samples                */
+    float  Sn[NW];     /* float input speech samples                   */
+    float  ak[P+1];    /* LPCs for current frame                       */
+    float  lsp[P];     /* LSPs for current frame                       */
+    float  E;          /* frame energy                                 */
+    long   af;         /* number frames with "active" speech           */
+    float  Eres;       /* LPC residual energy                          */
+    int    i;
+    int    roots;
+    int    unstables;
+    int    lspd;
+
+    /* Initialise ------------------------------------------------------*/
+
+    if (argc < 3) {
+       printf("usage: gentest RawFile LSPTextFile [--lspd]\n");
+       exit(0);
+    }
+
+    /* Open files */
+
+    fspc = fopen(argv[1],"rb");
+    if (fspc == NULL) {
+       printf("Error opening input SPC file: %s",argv[1]);
+       exit(1);
+    }
+
+    flsp = fopen(argv[2],"wt");
+    if (flsp == NULL) {
+       printf("Error opening output LSP file: %s",argv[2]);
+       exit(1);
+    }
+
+    lspd = switch_present("--lspd", argc, argv);
+
+    for(i=0; i<NW; i++)
+       Sn[i] = 0.0;
+
+    /* Read SPC file, and determine aks[] for each frame ------------------*/
+
+    af = 0;
+    unstables = 0;
+    while(fread(buf,sizeof(short),N,fspc) == N) {
+
+       for(i=0; i<NW-N; i++)
+           Sn[i] = Sn[i+N];
+       E = 0.0;
+       for(i=0; i<N; i++) {
+           Sn[i+NW-N] = buf[i];
+           E += Sn[i]*Sn[i];
+       }
+
+       E = 0.0;
+       for(i=0; i<NW; i++) {
+           E += Sn[i]*Sn[i];
+       }
+       E = 10.0*log10(E/NW);
+
+       /* If energy high enough, include this frame */
+
+       if (E > THRESH) {
+           af++;
+           printf("Active Frame: %ld  unstables: %d\n",af, unstables);
+
+           find_aks(Sn, ak, NW, P, &Eres);
+           roots = lpc_to_lsp(ak, P , lsp, 5, LSP_DELTA1);
+           if (roots == P) {
+               if (lspd) {
+                   fprintf(flsp,"%f ",lsp[0]);
+                   for(i=1; i<P; i++)
+                       fprintf(flsp,"%f ",lsp[i]-lsp[i-1]);
+                   fprintf(flsp,"\n");
+               }
+               else {
+                   for(i=0; i<P; i++)
+                       fprintf(flsp,"%f ",lsp[i]);
+                   fprintf(flsp,"\n");
+               }
+           }
+           else 
+               unstables++;
+       }
+    }
+
+    fclose(fspc);
+    fclose(flsp);
+
+    return 0;
+}
+
+#endif
index 4f057723e900653f5882f617b67c0c1c06ff488b..6788dfbbc4597b37a9ea78b4b8378306ced8eec1 100644 (file)
@@ -66,9 +66,9 @@ int test1()
     c2 = codec2_create();
     c3 = (CODEC2*)c2;
 
-    fin = fopen("../raw/hts1a.raw", "rb");
+    fin = fopen("../raw/hts2a.raw", "rb");
     assert(fin != NULL);
-    fout = fopen("hts1a_test.raw", "wb");
+    fout = fopen("hts2a_test.raw", "wb");
     assert(fout != NULL);
 
     while(fread(buf, sizeof(short), N, fin) == N) {
@@ -209,6 +209,6 @@ int test3()
 }
 
 int main() {
-    test3();
+    test1();
     return 0;
 }
diff --git a/codec2/unittest/vq_clip_test.c b/codec2/unittest/vq_clip_test.c
new file mode 100644 (file)
index 0000000..644d349
--- /dev/null
@@ -0,0 +1,450 @@
+/*--------------------------------------------------------------------------*\
+
+       FILE........: vq_clip_test.c
+       AUTHOR......: David Rowe
+       DATE CREATED: 22 March 2011
+
+       This program tests a vector quantiser against a test database 
+       using the centre cliped quantisation method.
+
+\*--------------------------------------------------------------------------*/
+
+/*
+  Copyright (C) 2009 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 <http://www.gnu.org/licenses/>.
+*/
+
+#define THRESH 40.0    /* threshold energy/sample for frame inclusion  */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include "defines.h"
+#include "comp.h"
+#include "codec2_internal.h"   
+#include "codec2.h"    
+#include "interp.h"    
+#include "lpc.h"       
+
+long quantise_clip(float cb[], float vec[], int k, int m, float clip, float *se,
+             int *less_than_clip);
+
+float sample_log_amp_quad_nl(
+                            float w[],     /* frequency points            */
+                            float A[],     /* for these amplitude samples */
+                            int np,        /* number of frequency points  */
+                            float w_sample /* frequency of new samples    */
+                            );
+
+int switch_present(sw,argc,argv)
+  char sw[];     /* switch in string form */
+  int argc;      /* number of command line arguments */
+  char *argv[];  /* array of command line arguments in string form */
+{
+  int i;       /* loop variable */
+
+  for(i=1; i<argc; i++)
+    if (!strcmp(sw,argv[i]))
+      return(i);
+
+  return 0;
+}
+
+struct codebook {
+  unsigned int k;
+  unsigned int log2m;
+  unsigned int m;
+  float * cb;
+};
+
+float
+get_float(FILE * in, const char * name, char * * cursor, char * buffer,
+ int size)
+{
+  for ( ; ; ) {
+    char *     s = *cursor;
+    char       c;
+
+    while ( (c = *s) != '\0' && !isdigit(c) && c != '-' && c != '.' )
+      s++;
+     
+    /* Comments start with "#" and continue to the end of the line. */
+    if ( c != '\0' && c != '#' ) {
+      char *   end = 0;
+      float    f = 0;
+
+      f = strtod(s, &end);
+
+      if ( end != s )
+        *cursor = end;
+        return f;
+    }
+
+    if ( fgets(buffer, size, in) == NULL ) {
+      fprintf(stderr, "%s: Format error.\n", name);
+      exit(1);
+    }
+    *cursor = buffer;
+  }
+}
+
+static struct codebook *
+load(FILE * file, const char * name)
+{
+  char                 line[1024];
+  char *               cursor = line;
+  struct codebook *    b = malloc(sizeof(struct codebook));
+  int                  i;
+  int                  size;
+
+  *cursor = '\0';
+
+  b->k = (int)get_float(file, name, &cursor, line, sizeof(line));
+  b->m = (int)get_float(file, name ,&cursor, line, sizeof(line));
+  size = b->k * b->m;
+
+  b->cb = (float *)malloc(size * sizeof(float));
+
+  for ( i = 0; i < size; i++ )
+    b->cb[i] = get_float(file, name, &cursor, line, sizeof(line));
+
+  return b;
+}
+
+/*---------------------------------------------------------------------------*\
+                                                       
+  FUNCTION....: synthesise_one_frame_orig_phase()           
+  AUTHOR......: David Rowe                           
+  DATE CREATED: 23/8/2010 
+
+  Synthesise 80 speech samples (10ms) from model parameters.
+
+\*---------------------------------------------------------------------------*/
+
+void synthesise_one_frame_orig_phase(CODEC2 *c2, short speech[], MODEL *model)
+{
+    int     i;
+
+    postfilter(model, &c2->bg_est);
+    synthesise(c2->Sn_, model, c2->Pn, 1);
+
+    for(i=0; i<N; i++) {
+       if (c2->Sn_[i] > 32767.0)
+           speech[i] = 32767;
+       else if (c2->Sn_[i] < -32767.0)
+           speech[i] = -32767;
+       else
+           speech[i] = c2->Sn_[i];
+    }
+}
+
+#define PHASE_POINTS 50
+
+int main(int argc, char *argv[]) {
+    FILE   *fraw, *fvq, *fout;
+    short   buf[N];
+    void   *c2;
+    CODEC2 *c3;
+    MODEL   model;
+    float w[MAX_AMP], A[MAX_AMP];
+    float wres[MAX_AMP], Ares[MAX_AMP], AresdB[MAX_AMP], AresdB_q[MAX_AMP], AresdB_prev[MAX_AMP];
+    float deltat[MAX_AMP];
+    float sam, E;
+    long  f, af;
+    int   i, j, err_less_than_clip_low, err_less_than_clip_high, k, m, besti_low, besti_high;
+    float av_err_low, av_err_high;
+    float clip;
+    struct  codebook *cblow = malloc(sizeof(struct codebook));
+    struct  codebook *cbhigh = malloc(sizeof(struct codebook));
+    float sum_av_err_low, sum_av_err_high;
+    int  sum_err_less_than_clip_low,sum_err_less_than_clip_high;
+    float signal, noise, snr, new_A, sum_snr;
+    float  wph[PHASE_POINTS], Pph[PHASE_POINTS], R[LPC_ORD+1], aks[LPC_ORD+1];
+    int phase_model;
+
+    if (argc < 5) {
+       printf("usage: vq_clip_test RawFileIn LowVQFile HighVQFile clip [RawFileOut] --phase\n");
+       exit(0);
+    }
+
+    f = af = 0;
+
+    c2 = codec2_create();
+    c3 = (CODEC2*)c2;
+
+    for(i=0; i<RES_POINTS; i++)
+       AresdB_prev[i] = 0.0; 
+
+    /* Open files */
+
+    fraw = fopen(argv[1],"rb");
+    if (fraw == NULL) {
+       printf("Error opening input .raw file: %s",argv[1]);
+       exit(1);
+    }
+
+    fvq = fopen(argv[2],"rt");
+    if (fvq == NULL) {
+       printf("Error opening input VQ file: %s",argv[2]);
+       exit(1);
+    }
+    cblow = load(fvq, argv[2]);
+    fclose(fvq);
+
+    fvq = fopen(argv[3],"rt");
+    if (fvq == NULL) {
+       printf("Error opening input VQ file: %s",argv[3]);
+       exit(1);
+    }
+    cbhigh = load(fvq, argv[3]);
+    fclose(fvq);
+
+    clip = atof(argv[4]);
+
+    if (argc >= 6) {
+       fout = fopen(argv[5],"wb");
+       if (fout == NULL) {
+           printf("Error opening output .raw file: %s",argv[4]);
+           exit(1);
+       }
+    }
+    else
+       fout = NULL;
+
+    if (switch_present("--phase",argc, argv)) {
+       phase_model = 1;
+    }
+    else
+       phase_model = 0;
+
+    dump_on("test");
+
+    sum_av_err_low = sum_av_err_high = 0;
+    sum_err_less_than_clip_low = sum_err_less_than_clip_high = 0;
+    sum_snr = 0;
+
+    while(fread(buf, sizeof(short), N, fraw) == N) {
+       analyse_one_frame(c3, &model, buf);
+
+       E = 0.0;
+       for(i=0; i<N; i++) {
+           sam = (float)buf[i];
+           E += sam*sam;
+       }
+       E = 10.0*log10(E/N);
+
+       f++;
+       printf("Frame: %ld  \n", f);
+
+       resample_amp_fixed(&model, w, A, wres, Ares,
+                          AresdB_prev, AresdB, deltat);
+       dump_resample(wres,Ares,RES_POINTS);
+       //#define VQ
+#ifdef VQ
+       besti_low = quantise_clip(cblow->cb, deltat, cblow->k, cblow->m, clip, 
+                                 &av_err_low, 
+                                 &err_less_than_clip_low);
+       printf("LOW av_err: %3.2f err_less_than_clip: %d  besti: %d\n\n", 
+              av_err_low, err_less_than_clip_low, besti_low );
+
+       besti_high = quantise_clip(cbhigh->cb, &deltat[cblow->k], cbhigh->k, cbhigh->m, clip, 
+                                 &av_err_high, 
+                                 &err_less_than_clip_high);
+       printf("HIGH av_err: %3.2f err_less_than_clip: %d  besti: %d\n\n", 
+              av_err_high, err_less_than_clip_high, besti_high );
+
+       /* recover Ares vector */
+
+       for(i=0; i<cblow->k; i++) {
+           AresdB_q[i] = AresdB_prev[i] + cblow->cb[besti_low*cblow->k+i];
+           Ares[i] = pow(10.0, AresdB_q[i]/20.0);
+           //printf("%d %f 2%f\n", i, AresdB[i], AresdB_q[i]);
+       }
+       for(i=0,j=cblow->k; i<cbhigh->k; i++,j++) {
+           AresdB_q[j] = AresdB_prev[j] + cbhigh->cb[besti_high*cbhigh->k+i];
+           Ares[j] = pow(10.0, AresdB_q[j]/20.0);
+           // printf("%d %f %f\n", j, AresdB[j], AresdB_q[j]);
+       }
+#endif
+
+       //#define ART_NOISE
+#ifdef ART_NOISE
+       // add some artificial noise to test effect on phase model
+
+       for(i=0; i<RES_POINTS; i++) {
+           noise = 3.0*(1.0 - 2.0*(float)rand()/RAND_MAX);
+           AresdB[i] = 20.0*log10(Ares[i]) + noise;
+           Ares[i] = pow(10.0, AresdB[i]/20.0);
+       }
+#endif
+
+       /* recover {A} vector */
+
+       signal = noise = 0.0;
+    
+       for(i=1; i<=model.L; i++) {
+           //printf("model.Wo*i %f\n", model.Wo*i);
+           new_A = pow(10.0,sample_log_amp_quad_nl(wres, Ares, RES_POINTS, model.Wo*i));
+           signal += pow(model.A[i], 2.0);
+           noise  += pow(model.A[i] - new_A, 2.0);
+           model.A[i] = new_A;
+       }
+
+       snr = 10.0*log10(signal/noise);
+       printf("snr = %3.2f\n", snr);
+
+       if (phase_model) {
+           /* generate a phase spectrum using freq domain LPC */
+
+           for(i=0; i<PHASE_POINTS; i++) {
+               wph[i] = i*PI/PHASE_POINTS;
+               new_A = pow(10.0,sample_log_amp_quad_nl(wres, Ares, RES_POINTS, wph[i]));
+               Pph[i] = new_A*new_A;
+           }
+
+           //for(i=0; i<RES_POINTS; i++)
+           //  printf("%d wres: %f Ares: %f Pw %f\n", i, wres[i], Ares[i], Pph[i]);
+
+           {
+               COMP    Sw[FFT_ENC];
+               dft_speech(Sw, c3->Sn, c3->w);
+
+               dump_Sn(c3->Sn); dump_Sw(Sw); dump_model(&model);
+           }
+
+           //      #define TIME_DOM
+#ifdef TIME_DOM
+           {
+               float Wn[M];
+               for(i=0; i<M; i++)
+                   Wn[i] = c3->Sn[i]*c3->w[i];
+               autocorrelate(Wn,R,M,LPC_ORD);
+           }
+           levinson_durbin(R, aks, LPC_ORD);
+           dump_ak(aks, LPC_ORD);
+#else
+           autocorrelate_freq(Pph, wph, R, PHASE_POINTS, LPC_ORD);
+           levinson_durbin(R, aks, LPC_ORD);
+           dump_ak(aks, LPC_ORD);
+#endif
+
+           for(i=0; i<=LPC_ORD; i++)
+               printf("%d R: %f aks: %f\n", i, R[i], aks[i]);
+           //if (f==30)
+           //  exit(0);
+           synthesise_one_frame(c3, buf, &model, aks);
+       }
+       else {
+           synthesise_one_frame_orig_phase(c3, buf, &model);
+       }
+
+       if (fout != NULL) fwrite(buf, sizeof(short), N, fout);
+       
+       /* If energy high enough, include this frame in stats */
+
+       if (E > THRESH) {
+           af++;
+           sum_av_err_low += av_err_low;
+           if (err_less_than_clip_low)
+               sum_err_less_than_clip_low++;
+           sum_av_err_high += av_err_high;
+           if (err_less_than_clip_high)
+               sum_err_less_than_clip_high++;
+           sum_snr += snr;
+       }
+       for(i=0; i<RES_POINTS; i++)
+           AresdB_prev[i] = AresdB_q[i];           
+    }
+
+    printf("Active frames: %ld av_error_low: %3.2f  %% less than clip low %3.2f %%"
+          "av_error_high: %3.2f  %% less than clip high %3.2f %%SNR av %3.2f\n", 
+          af, 
+          sum_av_err_low/af, 
+          100.0*(float)sum_err_less_than_clip_low/af,
+          sum_av_err_high/af, 
+          100.0*(float)sum_err_less_than_clip_high/af,
+          sum_snr/af);
+
+    codec2_destroy(c2);
+
+    fclose(fraw);
+    if (fout != NULL) fclose(fout);
+
+    return 0;
+}
+
+/*---------------------------------------------------------------------------*\
+
+       FUNCTION....: quantise_clip()
+
+       AUTHOR......: David Rowe
+       DATE CREATED: 23 March 2011
+
+       Quantises vec by choosing the nearest vector in codebook cb, and
+       returns the vector index.  
+
+        This quantiser uses a centre clipping method.  The quant error
+       of each vector element is inspected.  If it is beneath a
+       certain threshold it is ignored in the error measurement.
+
+\*---------------------------------------------------------------------------*/
+
+long quantise_clip(float cb[], float vec[], int k, int m, float clip, float *av_err,
+             int *less_than_clip)
+{
+    float   element_e;
+    float   e;            /* error for current vector  */
+    long    besti;         /* best index so far                */
+    float   beste;        /* best error so far         */
+    long    j;
+    int     i;
+
+    besti = 0;
+    beste = 1E32;
+    for(j=0; j<m; j++) {
+       e = 0.0;
+       for(i=0; i<k; i++) {
+           element_e = cb[j*k+i]-vec[i];
+           if (fabs(element_e) > clip) {
+               e += fabs(element_e);
+           }
+       }
+       if (e < beste) {
+           beste = e;
+           besti = j;
+       }
+    }
+
+    *av_err = 0;
+    *less_than_clip = 1;
+    for(i=0; i<k; i++) {
+       element_e = cb[besti*k+i]-vec[i];
+       (*av_err) += fabs(element_e);
+       if (fabs(element_e) > clip) {
+           *less_than_clip = 0;
+       }
+    }
+    for(i=0; i<k; i++)
+       printf("% 14f", vec[i]);
+    printf("\n");
+    for(i=0; i<k; i++)
+       printf("% 14f", cb[besti*k+i]);
+    printf("\n");
+
+    (*av_err) /= k;
+
+    return(besti);
+}
diff --git a/codec2/unittest/vqtrain_clip.c b/codec2/unittest/vqtrain_clip.c
new file mode 100644 (file)
index 0000000..a4f4a11
--- /dev/null
@@ -0,0 +1,309 @@
+/*--------------------------------------------------------------------------*\
+
+       FILE........: vqtrain_clipped.c
+       AUTHOR......: David Rowe
+       DATE CREATED: 22 March 2011
+
+       This program trains vector quantisers using K dimensional Lloyd-Max
+       method, modified to centre clip errors.
+
+\*--------------------------------------------------------------------------*/
+
+/*
+  Copyright (C) 2009 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 <http://www.gnu.org/licenses/>.
+*/
+
+/*-----------------------------------------------------------------------*\
+
+                               INCLUDES
+
+\*-----------------------------------------------------------------------*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <ctype.h>
+
+/*-----------------------------------------------------------------------*\
+
+                               DEFINES
+
+\*-----------------------------------------------------------------------*/
+
+#define        DELTAQ  0.001           /* quiting distortion                   */
+#define        MAX_STR 80              /* maximum string length                */
+
+/*-----------------------------------------------------------------------*\
+
+                       FUNCTION PROTOTYPES
+
+\*-----------------------------------------------------------------------*/
+
+void zero(float v[], int k);
+void acc(float v1[], float v2[], int k);
+void norm(float v[], int k, long n);
+long quantise(float cb[], float vec[], int k, int m, float clip, float *se);
+
+/*-----------------------------------------------------------------------*\
+
+                               MAIN
+
+\*-----------------------------------------------------------------------*/
+
+int main(int argc, char *argv[]) {
+    long   k,m;                /* dimension and codebook size                  */
+    float  *vec;       /* current vector                               */
+    float  *cb;                /* vector codebook                              */
+    float  *cent;      /* centroids for each codebook entry            */
+    long   *n;         /* number of vectors in this interval           */
+    long   J;          /* number of vectors in training set            */
+    long   i,j;
+    long   ind;                /* index of current vector                      */
+    float  se;         /* squared error for this iteration             */
+    float  Dn,Dn_1;    /* current and previous iterations distortion   */
+    float  delta;      /* improvement in distortion                    */
+    FILE   *ftrain;    /* file containing training set                 */
+    FILE   *fvq;       /* file containing vector quantiser             */
+    float  clip;
+
+    /* Interpret command line arguments */
+
+    if (argc != 6)     {
+       printf("usage: vqtrain TrainFile K M VQFile clip\n");
+       exit(0);
+    }
+
+    /* 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]);
+    m = atol(argv[3]);
+    clip = atof(argv[5]);
+    printf("dimension K=%ld  number of entries M=%ld clip = %f\n", k,m,clip);
+    vec = (float*)malloc(sizeof(float)*k);
+    cb = (float*)malloc(sizeof(float)*k*m);
+    cent = (float*)malloc(sizeof(float)*k*m);
+    n = (long*)malloc(sizeof(long)*m);
+    if (cb == NULL || cb == NULL || cent == NULL || vec == NULL) {
+       printf("Error in malloc.\n");
+       exit(1);
+    }
+
+    /* determine size of training set */
+
+    J = 0;
+    while(fread(vec, sizeof(float), k, ftrain) == k)
+    J++;
+    printf("J=%ld entries in training set\n", J);
+
+    /* set up initial codebook state from samples of training set */
+
+    rewind(ftrain);
+    fread(cb, sizeof(float), k*m, ftrain);
+
+    /* main loop */
+
+    Dn = 1E32;
+    j = 1;
+    do {
+       Dn_1 = Dn;
+
+       /* zero centroids */
+
+       for(i=0; i<m; i++) {
+           zero(&cent[i*k], k);
+           n[i] = 0;
+       }
+
+       /* quantise training set */
+
+       se = 0.0;
+       rewind(ftrain);
+       for(i=0; i<J; i++) {
+           fread(vec, sizeof(float), k, ftrain);
+           ind = quantise(cb, vec, k, m, clip, &se);
+           n[ind]++;
+           acc(&cent[ind*k], vec, k);
+       }
+       Dn = se/J;
+       delta = (Dn_1-Dn)/Dn;
+
+       printf("\r  Iteration %ld, Dn = %f, Delta = %e\n", j, Dn, delta);
+       j++;
+
+       /* determine new codebook from centriods */
+
+       if (delta > DELTAQ)
+           for(i=0; i<m; i++) {
+               if (n[i] != 0) {
+                   norm(&cent[i*k], k, n[i]);
+                   memcpy(&cb[i*k], &cent[i*k], k*sizeof(float));
+               }
+           }
+
+    } while (delta > DELTAQ);
+
+    /* save codebook to disk */
+
+    fvq = fopen(argv[4],"wt");
+    if (fvq == NULL) {
+       printf("Error opening VQ file: %s\n",argv[4]);
+       exit(1);
+    }
+
+    fprintf(fvq,"%ld %ld\n",k,m);
+    for(j=0; j<m; j++) {
+       for(i=0; i<k; i++)
+           fprintf(fvq,"%f  ",cb[j*k+i]);
+       fprintf(fvq,"\n");
+    }
+    fclose(fvq);
+
+    return 0;
+}
+
+/*-----------------------------------------------------------------------*\
+
+                               FUNCTIONS
+
+\*-----------------------------------------------------------------------*/
+
+/*---------------------------------------------------------------------------*\
+
+       FUNCTION....: zero()
+
+       AUTHOR......: David Rowe
+       DATE CREATED: 23/2/95
+
+       Zeros a vector of length k.
+
+\*---------------------------------------------------------------------------*/
+
+void zero(float v[], int k)
+/*  float  v[];                ptr to start of vector          */
+/*  int    k;          lngth of vector                 */
+{
+    int        i;
+
+    for(i=0; i<k; i++)
+       v[i] = 0.0;
+}
+
+/*---------------------------------------------------------------------------*\
+
+       FUNCTION....: acc()
+
+       AUTHOR......: David Rowe
+       DATE CREATED: 23/2/95
+
+       Adds k dimensional vectors v1 to v2 and stores the result back in v1.
+
+\*---------------------------------------------------------------------------*/
+
+void acc(float v1[], float v2[], int k)
+/*  float  v1[];       ptr to start of vector to accumulate    */
+/*  float  v2[];       ptr to start of vector to add           */
+/*  int           k;           dimension of vectors                    */
+{
+    int           i;
+
+    for(i=0; i<k; i++)
+       v1[i] += v2[i];
+}
+
+/*---------------------------------------------------------------------------*\
+
+       FUNCTION....: norm()
+
+       AUTHOR......: David Rowe
+       DATE CREATED: 23/2/95
+
+       Divides each element in k dimensional vector v by n.
+
+\*---------------------------------------------------------------------------*/
+
+void norm(float v[], int k, long n)
+/*  float  v[];                ptr to start of vector          */
+/*  int           k;           dimension of vectors            */
+/*  int           n;           normalising factor              */
+{
+    int           i;
+
+    for(i=0; i<k; i++)
+       v[i] /= n;
+}
+
+/*---------------------------------------------------------------------------*\
+
+       FUNCTION....: quantise()
+
+       AUTHOR......: David Rowe
+       DATE CREATED: 23/2/95
+
+       Quantises vec by choosing the nearest vector in codebook cb, and
+       returns the vector index.  The squared error of the quantised vector
+       is added to se.
+
+        This quantiser uses a centre clipping method.  The quant error
+       of each vector element is inspected.  If it is beneath a
+       certain threshold it is ignored in the error measurement.
+
+\*---------------------------------------------------------------------------*/
+
+long quantise(float cb[], float vec[], int k, int m, float clip, float *se)
+/* float   cb[][K];    current VQ codebook             */
+/* float   vec[];      vector to quantise              */
+/* int    k;           dimension of vectors            */
+/* int     m;          size of codebook                */
+/* float   clip;        centre clip level               */
+/* float   *se;                accumulated squared error       */
+{
+    float   element_e;
+    float   e;            /* error for current vector  */
+    long    besti;         /* best index so far                */
+    float   beste;        /* best error so far         */
+    long    j;
+    int     i;
+
+    besti = 0;
+    beste = 1E32;
+    for(j=0; j<m; j++) {
+       e = 0.0;
+       for(i=0; i<k; i++) {
+           element_e = cb[j*k+i]-vec[i];
+           if (fabs(element_e) > clip) {
+               e +=  element_e*element_e;
+           }
+       }
+       if (e < beste) {
+           beste = e;
+           besti = j;
+       }
+    }
+
+    *se += beste;
+
+    return(besti);
+}
+