17 bit/frame mel LSP quantiser working OK
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Tue, 14 Apr 2015 08:19:41 +0000 (08:19 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Tue, 14 Apr 2015 08:19:41 +0000 (08:19 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@2116 01035d8c-6547-0410-b346-abe4f91aad63

15 files changed:
codec2-dev/octave/melstats.m [new file with mode: 0644]
codec2-dev/octave/sd.m
codec2-dev/src/CMakeLists.txt
codec2-dev/src/c2sim.c
codec2-dev/src/codebook/mel1.txt [new file with mode: 0644]
codec2-dev/src/codebook/mel2.txt [new file with mode: 0644]
codec2-dev/src/codebook/mel3.txt [new file with mode: 0644]
codec2-dev/src/codebook/mel4.txt [new file with mode: 0644]
codec2-dev/src/codebook/mel5.txt [new file with mode: 0644]
codec2-dev/src/codebook/mel6.txt [new file with mode: 0644]
codec2-dev/src/defines.h
codec2-dev/src/dump.c
codec2-dev/src/dump.h
codec2-dev/src/quantise.c
codec2-dev/src/quantise.h

diff --git a/codec2-dev/octave/melstats.m b/codec2-dev/octave/melstats.m
new file mode 100644 (file)
index 0000000..d373559
--- /dev/null
@@ -0,0 +1,49 @@
+% melstats.m
+% David Rowe April 2015
+% 
+% plots some stats of mel/lsp quantisers
+
+function melstats(filename)
+
+  mel = load(filename);
+  [m n] = size(mel);
+  nbins = 10;
+
+  % histograms of each value
+
+  figure(1)
+  clf
+  subplot(211)
+  [h x] = hist(mel(:,1),nbins);
+  plot(x,h,"1");
+  hold on
+
+  for i=2:n
+    [h x] = hist(mel(:,i),nbins);
+    colour = sprintf("%d",i);
+    plot(x,h,colour);
+  end
+  hold off
+
+  % histograms differences
+
+  subplot(212)
+  [h x] = hist(mel(:,1),nbins);
+  plot(x,h,"1");
+  hold on
+
+  for i=2:n
+    [h x] = hist(mel(:,i)-mel(:,i-1),nbins);
+    colour = sprintf("%d",i);
+    plot(x,h, colour);
+  end
+  hold off
+
+  figure(2)
+  plot(mel(:,1),mel(:,2),'r+')
+  hold on;
+  plot(mel(:,3),mel(:,4),'g+')
+  plot(mel(:,5),mel(:,6),'b+')
+  hold off;
+
+endfunction
index efff9061598e8acbf693c361804458ee7ad9ca17..8065de7978512deaf5a72e544d63aca550b4351c 100644 (file)
@@ -36,6 +36,7 @@ function sd(raw_filename, dump_file_prefix, f)
   plot(s);
   subplot(212)
   plot(sd);
+  axis([1 frames 0 10])
 
   lsp1_filename = sprintf("%s_lsp.txt", dump_file_prefix);
   lsp2_filename = sprintf("%s_lsp_.txt", dump_file_prefix);
index 43f9a1812582f83cca7c9e5064d1b3701723aa47..30b26513eae182b9b56e30f5af3b25e5bf98bb49 100644 (file)
@@ -78,6 +78,15 @@ set(CODEBOOKSVQANSSI
     ${D}/lspvqanssi4.txt
 )
 
+set(CODEBOOKSMEL
+    ${D}/mel1.txt
+    ${D}/mel2.txt
+    ${D}/mel3.txt
+    ${D}/mel4.txt
+    ${D}/mel5.txt
+    ${D}/mel6.txt
+)
+
 set(CODEBOOKSGE ${D}/gecb.txt)
 
 # when crosscompiling import the executable targets from a file
@@ -147,6 +156,13 @@ add_custom_command(
     DEPENDS generate_codebook ${CODEBOOKSVQANSSI}
 )
 
+# codebookmel.c
+add_custom_command(
+    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebookmel.c
+    COMMAND generate_codebook mel_cb ${CODEBOOKSMEL} > ${CMAKE_CURRENT_BINARY_DIR}/codebookmel.c
+    DEPENDS generate_codebook ${CODEBOOKSMEL}
+)
+
 # codebookge.c
 add_custom_command(
     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/codebookge.c
@@ -183,6 +199,7 @@ set(CODEC2_SRCS
     codebookjvm.c
     codebookvqanssi.c
     codebookdt.c
+    codebookmel.c
     codebookge.c
     golay23.c
     freedv_api.c
index b653b203b00315af34ee8bae7795e67592472282..44fe1670a077a36e13f4f4c54db8bc526776915f 100644 (file)
@@ -591,34 +591,33 @@ int main(int argc, char *argv[])
            */
 
            if (lspmel) {
-               float f, f_;
-               int mel[LPC_ORD];
+               float f, f_, dmel;
+               float mel[LPC_ORD];
+               int   mel_indexes[LPC_ORD];
 
-               for(i=0; i<LPC_ORD; i++) {
+               for(i=0; i<order; i++) {
                    f = (4000.0/PI)*lsps[i];
-                   mel[i] = floor(100.0*log10(1.0 + f/700.0) + 0.5);
+                   mel[i] = floor(2595.0*log10(1.0 + f/700.0) + 0.5);
                }
 
-               for(i=1; i<LPC_ORD; i++) {
+               for(i=1; i<order; i++) {
                    if (mel[i] == mel[i-1])
                        mel[i]++;
                }
 
+               encode_mels_scalar(mel_indexes, mel, 6);
+               decode_mels_scalar(mel, mel_indexes, 6);
+                
                 #ifdef DUMP
-                dump_mel(mel);
+                dump_mel(mel, order);
                 #endif
 
                for(i=0; i<LPC_ORD; i++) {
-                   f_ = 700.0*( pow(10.0, (float)mel[i]/100.0) - 1.0);
+                   f_ = 700.0*( pow(10.0, (float)mel[i]/2595.0) - 1.0);
                    lsps_[i] = f_*(PI/4000.0);
                }
-                /*
-                for(i=5; i<10; i++) {
-                   lsps_[i] = lsps[i];
-               }
-                */
-
-               lsp_to_lpc(lsps_, ak, LPC_ORD);
+               lsp_to_lpc(lsps_, ak, order);
            }
 
            /* we need lsp__prev[] for lspdt and decimate.  If no
diff --git a/codec2-dev/src/codebook/mel1.txt b/codec2-dev/src/codebook/mel1.txt
new file mode 100644 (file)
index 0000000..06d59f8
--- /dev/null
@@ -0,0 +1,9 @@
+1 8
+550
+600
+650
+700
+750
+800
+850
+900
diff --git a/codec2-dev/src/codebook/mel2.txt b/codec2-dev/src/codebook/mel2.txt
new file mode 100644 (file)
index 0000000..b650319
--- /dev/null
@@ -0,0 +1,5 @@
+1 4
+50
+100
+200
+300
diff --git a/codec2-dev/src/codebook/mel3.txt b/codec2-dev/src/codebook/mel3.txt
new file mode 100644 (file)
index 0000000..b81a729
--- /dev/null
@@ -0,0 +1,17 @@
+1 16
+800
+850
+900
+950
+1000
+1050
+1100
+1150
+1200
+1250
+1300
+1350
+1400
+1450
+1500
+1650
diff --git a/codec2-dev/src/codebook/mel4.txt b/codec2-dev/src/codebook/mel4.txt
new file mode 100644 (file)
index 0000000..3f79ca8
--- /dev/null
@@ -0,0 +1,9 @@
+1 8
+25
+50
+75
+100
+125
+150
+175
+250
diff --git a/codec2-dev/src/codebook/mel5.txt b/codec2-dev/src/codebook/mel5.txt
new file mode 100644 (file)
index 0000000..5791d4a
--- /dev/null
@@ -0,0 +1,9 @@
+1 8
+1350
+1400
+1450
+1500
+1550
+1600
+1650
+1700
diff --git a/codec2-dev/src/codebook/mel6.txt b/codec2-dev/src/codebook/mel6.txt
new file mode 100644 (file)
index 0000000..40b319d
--- /dev/null
@@ -0,0 +1,6 @@
+1 4
+25
+50
+100
+150
+
index 3c2b9cdbed3bdac33aed210816a658e2c0ed0839..df7efd833602d0a4bd260afb364f5d8a1b9c95c7 100644 (file)
@@ -88,6 +88,7 @@ extern const struct lsp_codebook lsp_cbjnd[];
 extern const struct lsp_codebook lsp_cbdt[];
 extern const struct lsp_codebook lsp_cbjvm[];
 extern const struct lsp_codebook lsp_cbvqanssi[];
+extern const struct lsp_codebook mel_cb[];
 extern const struct lsp_codebook ge_cb[];
 
 #endif
index 00ce80626ff243424927b55f05d66db920371b16..625215f1e71369a0a301c60a2d293f484ace8125 100644 (file)
@@ -456,7 +456,7 @@ void dump_lsp_(float lsp_[]) {
     fprintf(flsp_,"\n");    
 }
 
-void dump_mel(int mel[]) {
+void dump_mel(float mel[], int order) {
     int i;
     char s[MAX_STR];
 
@@ -468,8 +468,8 @@ void dump_mel(int mel[]) {
        assert(fmel != NULL);
     }
 
-    for(i=0; i<10; i++)
-       fprintf(fmel,"%d\t",mel[i]);
+    for(i=0; i<order; i++)
+       fprintf(fmel,"%f\t",mel[i]);
     fprintf(fmel,"\n");    
 }
 
index a7138301751039b8452a9e81ab12e44829d8dcc6..f14269d053c793064e806b22f0142aac1be81854 100644 (file)
@@ -49,7 +49,7 @@ void dump_Rw(float Rw[]);
 void dump_lsp(float lsp[]);
 void dump_weights(float w[], int ndim);
 void dump_lsp_(float lsp_[]);
-void dump_mel(int mel[]);
+void dump_mel(float mel[], int order);
 void dump_ak(float ak[], int order);
 void dump_ak_(float ak[], int order);
 void dump_E(float E);
index 105380410d3207fa6b5486f354ce5a50142ade2e..802f5497d807e9eae262fd9e14b1bdd795134300 100644 (file)
@@ -1303,6 +1303,74 @@ void decode_lsps_scalar(float lsp[], int indexes[], int order)
 }
 
 
+/*---------------------------------------------------------------------------*\
+                                                       
+  FUNCTION....: encode_mels_scalar()        
+  AUTHOR......: David Rowe                           
+  DATE CREATED: April 2015 
+
+  Low bit rate mel coeff encoder.
+
+\*---------------------------------------------------------------------------*/
+
+void encode_mels_scalar(int indexes[], float mels[], int order)
+{
+    int    i,m;
+    float  wt[1];
+    const float * cb;
+    float se, mel_, dmel;
+    
+    /* scalar quantisers */
+
+    wt[0] = 1.0;
+    for(i=0; i<order; i++) {
+       m = mel_cb[i].m;
+       cb = mel_cb[i].cb;
+        if (i%2) {
+            /* on odd mels quantise difference */
+            mel_ = mel_cb[i-1].cb[indexes[i-1]];
+            dmel = mels[i] - mel_;
+            indexes[i] = quantise(cb, &dmel, wt, 1, m, &se);
+            //printf("%d mel: %f mel_: %f dmel: %f index: %d\n", i, mels[i], mel_, dmel, indexes[i]);
+        }
+        else {
+            indexes[i] = quantise(cb, &mels[i], wt, 1, m, &se);
+            //printf("%d mel: %f dmel: %f index: %d\n", i, mels[i], 0.0, indexes[i]);
+        }
+
+    }
+}
+
+
+/*---------------------------------------------------------------------------*\
+                                                       
+  FUNCTION....: decode_mels_scalar()        
+  AUTHOR......: David Rowe                           
+  DATE CREATED: April 2015 
+
+  From a vector of quantised mel indexes, returns the quantised
+  (floating point) mels.
+
+\*---------------------------------------------------------------------------*/
+
+void decode_mels_scalar(float mels[], int indexes[], int order)
+{
+    int    i;
+    const float * cb;
+
+    for(i=0; i<order; i++) {
+       cb = mel_cb[i].cb;
+        if (i%2) {
+            /* on odd mels quantise difference */
+            mels[i] = mels[i-1] + cb[indexes[i]];
+        }
+        else
+            mels[i] = cb[indexes[i]];
+    }
+
+}
+
+
 #ifdef __EXPERIMENTAL__
 
 /*---------------------------------------------------------------------------*\
index f052635f6b7334ce0594b96b9d786bcfffee497c..b4fd2968f346adbccbc939783a50edc28e206f45 100644 (file)
@@ -91,6 +91,9 @@ void lspdt_quantise(float lsps[], float lsps_[], float lsps__prev[], int mode);
 void lspjvm_quantise(float lsps[], float lsps_[], int order);
 void lspanssi_quantise(float lsps[], float lsps_[], int order, int mbest_entries);
 
+void encode_mels_scalar(int mel_indexes[], float mels[], int order);
+void decode_mels_scalar(float mels[], int mel_indexes[], int order);
+
 void quantise_WoE(MODEL *model, float *e, float xq[]);
 int  encode_WoE(MODEL *model, float e, float xq[]);
 void decode_WoE(MODEL *model, float *e, float xq[], int n1);