first pass at a 37 bits/fr LSP quantiser, sounds OK on hts1a and hts2a
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 28 Aug 2009 03:37:19 +0000 (03:37 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 28 Aug 2009 03:37:19 +0000 (03:37 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@34 01035d8c-6547-0410-b346-abe4f91aad63

codec2/TODO.txt
codec2/script/menu.sh
codec2/src/quantise.c
codec2/src/quantise.h
codec2/src/sinedec.c
codec2/unittest/extract.c
codec2/unittest/vqtrain.c

index f7c0cbbb25b581764138616077ad56f3f1b186e6..4da876f38a486dd4f978a72683230488a4a96df0 100644 (file)
@@ -12,3 +12,5 @@ TODO for codec2
     Science Lib (GSL) SL FFT as NRC code has restrictive licencing
 [ ] A way to handle m=1 harmonic for males when LPC modelling
 [ ] Is BW expansion and Rk noise floor required before LSP quant
+[ ] test split VQ to make sure no silly errors
+    + for example test MSE or index historgram for training data
index 78f0fd08a95c33c85406e7bb7f09a46ee8e1e5c8..48ec3b33972c5c29660c56443affea68d5d8ad3d 100755 (executable)
@@ -39,7 +39,7 @@
 #  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
 files=0
-items="0 QUIT "
+items="Q-Quit "
 while [ ! -z "$1" ]
 do
   case "$1" in
@@ -47,7 +47,7 @@ do
      *) files=`expr 1 + $files`;
         new_file=$1;
         file[$files]=$new_file;
-        items="${items} ${files} ${new_file}";;
+        items="${items} ${files}-${new_file}";;
   esac
   shift
 done
index b4a5b49eb55d4151539393a3eb2a191bed2d5219..a8ea3d5a993c19316dddf53995fe2b3a725cce3c 100644 (file)
@@ -25,6 +25,7 @@
 */
 
 #include <assert.h>
+#include <ctype.h>
 #include "sine.h"
 #include "quantise.h"
 #include "lpc.h"
@@ -45,6 +46,22 @@ const float lag_window[11] = {
    0.83367, 0.79434, 0.75258
 };
 
+/* 10 + 9 + 9 + 9 = 37 bit quantiser (1850 bit/s with 20ms frames) */
+#define LSP_12_K  2
+#define LSP_12_M  1024
+#define LSP_34_K  2
+#define LSP_34_M  512
+#define LSP_57_K  3
+#define LSP_57_M  512
+#define LSP_810_K 3
+#define LSP_810_M 512
+
+static float cb12[LSP_12_K*LSP_12_M];
+static float cb34[LSP_34_K*LSP_34_M];
+static float cb57[LSP_57_K*LSP_57_M];
+static float cb810[LSP_810_K*LSP_810_M];
+
 /*---------------------------------------------------------------------------*\
                                                                              
   quantise_uniform
@@ -76,9 +93,9 @@ void quantise_uniform(float *val, float min, float max, int bits)
 
 /*---------------------------------------------------------------------------*\
                                                                              
-  lsp_quantise
+  lspd_quantise
 
-  Differential lsp quantiser
+  Simulates differential lsp quantiser
 
 \*---------------------------------------------------------------------------*/
 
@@ -106,6 +123,112 @@ void lsp_quantise(
        lsp_[i] = lsp_[i-1] + dlsp_[i];
 }
 
+/*---------------------------------------------------------------------------*\
+
+  scan_line()
+
+  This function reads a vector of floats from a line in a text file.
+
+\*---------------------------------------------------------------------------*/
+
+void scan_line(FILE *fp, float f[], int n)
+/*  FILE   *fp;                file ptr to text file           */
+/*  float  f[];        array of floats to return       */
+/*  int    n;          number of floats in line        */
+{
+    char   s[MAX_STR];
+    char   *ps,*pe;
+    int           i;
+
+    fgets(s,MAX_STR,fp);
+    ps = pe = s;
+    for(i=0; i<n; i++) {
+       while( isspace(*pe)) pe++;
+       while( !isspace(*pe)) pe++;
+       sscanf(ps,"%f",&f[i]);
+       ps = pe;
+    }
+}
+
+/*---------------------------------------------------------------------------*\
+
+  load_cb
+
+  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.
+
+\*---------------------------------------------------------------------------*/
+
+void load_cb(char *filename, float *cb, int k, int m)
+{
+    FILE *ftext;
+    int   lines;
+    int   i;
+
+    ftext = fopen(filename,"rt");
+    if (ftext == NULL) {
+       printf("Error opening text file: %s\n",filename);
+       exit(1);
+    }
+
+    lines = 0;
+    for(i=0; i<m; i++) {
+       scan_line(ftext, &cb[k*lines++], k);
+    }
+    printf("%d lines\n",lines);
+
+    fclose(ftext);
+}
+
+void quantise_init()
+{
+    load_cb("../unittest/lsp12.txt", cb12,  LSP_12_K,  LSP_12_M);
+    load_cb("../unittest/lsp34.txt", cb34,  LSP_34_K,  LSP_34_M);
+    load_cb("../unittest/lsp57.txt", cb57, LSP_57_K, LSP_57_M);
+    load_cb("../unittest/lsp810.txt", cb810, LSP_810_K, LSP_810_M);
+}
+
+/*---------------------------------------------------------------------------*\
+
+  quantise
+
+  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.
+
+\*---------------------------------------------------------------------------*/
+
+long quantise(float cb[], float vec[], int k, int m, float *se)
+/* float   cb[][K];    current VQ codebook             */
+/* float   vec[];      vector to quantise              */
+/* int    k;           dimension of vectors            */
+/* int     m;          size of codebook                */
+/* float   *se;                accumulated squared error       */
+{
+   float   e;          /* current error                */
+   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++)
+           e += pow(cb[j*k+i]-vec[i],2.0);
+       if (e < beste) {
+           beste = e;
+           besti = j;
+       }
+   }
+
+   *se += beste;
+
+   return(besti);
+}
+
 /*---------------------------------------------------------------------------*\
                                                                              
   lpc_model_amplitudes
@@ -133,7 +256,8 @@ float lpc_model_amplitudes(
   float lsp[MAX_ORDER];
   float lsp_[MAX_ORDER];  /* quantised LSPs */
   int   roots;            /* number of LSP roots found */
-  SpeexBits bits;
+  int   index;
+  float se;
 
   for(i=0; i<AW_ENC; i++)
       Wn[i] = Sn[i]*w[i];
@@ -145,10 +269,28 @@ float lpc_model_amplitudes(
       E += ak[i]*R[i];
   
   if (lsp_quantisation) {
-    roots = lpc_to_lsp(&ak[1], order , lsp, 10, LSP_DELTA1, NULL);
-    lsp_quantise(lsp, lsp_, order);
-    lsp_to_lpc(lsp_, &ak[1], order, NULL);
-    dump_lsp(lsp_);
+    roots = lpc_to_lsp(&ak[1], order, lsp, 10, LSP_DELTA1, NULL);
+
+    index = quantise(cb12, &lsp[0], LSP_12_K, LSP_12_M, &se);
+    lsp[0] = cb12[index*LSP_12_K+0];
+    lsp[1] = cb12[index*LSP_12_K+1];
+    
+    index = quantise(cb34, &lsp[2], LSP_34_K, LSP_34_M, &se);
+    lsp[2] = cb34[index*LSP_34_K+0];
+    lsp[3] = cb34[index*LSP_34_K+1];
+    
+    index = quantise(cb57, &lsp[4], LSP_57_K, LSP_57_M, &se);
+    lsp[4] = cb57[index*LSP_57_K+0];
+    lsp[5] = cb57[index*LSP_57_K+1];
+    lsp[6] = cb57[index*LSP_57_K+2];
+    
+    index = quantise(cb810, &lsp[7], LSP_810_K, LSP_810_M, &se);
+    lsp[7] = cb810[index*LSP_810_K+0];
+    lsp[8] = cb810[index*LSP_810_K+1];
+    lsp[9] = cb810[index*LSP_810_K+2];
+
+    lsp_to_lpc(lsp, &ak[1], order, NULL);
+    dump_lsp(lsp);
   }
 
   aks_to_M2(ak,order,model,E,&snr);   /* {ak} -> {Am} LPC decode */
index 5b1a15f0176fcd120ab0a418055909657ec0309b..762c0dfc144ffc20d9fdb4f45916aeeb8172ce77 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "sine.h"
 
+void quantise_init();
 float lpc_model_amplitudes(float Sn[], MODEL *model, int order, int lsp);
 void aks_to_M2(float ak[], int order, MODEL *model, float E, float *snr);
 
index aa4f937a541e351b1350cb977716dc383d27edc4..37c131a6711f9a55da3ebdc57a4a27a383fad980 100644 (file)
@@ -149,6 +149,7 @@ int main(int argc, char *argv[])
   init_encoder();
   Nw = 220;
   make_window(Nw);
+  quantise_init();
 
   /* Main loop ------------------------------------------------------------*/
 
index 59c6267a6332d9283c554b2163aecbd6380a7aa4..3b44d36998f1b4add9e090ee0ccaf84a6534fe2b 100644 (file)
@@ -94,7 +94,7 @@ int main(int argc, char *argv[]) {
        FUNCTION....: scan_line()
 
        AUTHOR......: David Rowe
-       DATE CREATED: 20/9/96
+       DATE CREATED: 20/2/95
 
        This function reads a vector of floats from a line in a text file.
 
index f657f8ab7976a6b6a570be088ac618d6cc326a77..9084746cc4ab5e717418dec29af0e2aa89a8d3bd 100644 (file)
@@ -192,7 +192,7 @@ int main(int argc, char *argv[]) {
        FUNCTION....: zero()
 
        AUTHOR......: David Rowe
-       DATE CREATED: 27/9/96
+       DATE CREATED: 23/2/95
 
        Zeros a vector of length k.
 
@@ -213,7 +213,7 @@ void zero(float v[], int k)
        FUNCTION....: acc()
 
        AUTHOR......: David Rowe
-       DATE CREATED: 27/9/96
+       DATE CREATED: 23/2/95
 
        Adds k dimensional vectors v1 to v2 and stores the result back in v1.
 
@@ -235,7 +235,7 @@ void acc(float v1[], float v2[], int k)
        FUNCTION....: norm()
 
        AUTHOR......: David Rowe
-       DATE CREATED: 27/9/96
+       DATE CREATED: 23/2/95
 
        Divides each element in k dimensional vector v by n.
 
@@ -257,7 +257,7 @@ void norm(float v[], int k, long n)
        FUNCTION....: quantise()
 
        AUTHOR......: David Rowe
-       DATE CREATED: 27/9/96
+       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