soft muting based on BER from demod, acceptable speech in SNR=4dB CCIR poor
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Wed, 2 Apr 2014 08:18:57 +0000 (08:18 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Wed, 2 Apr 2014 08:18:57 +0000 (08:18 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@1485 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/src/c2dec.c
codec2-dev/src/c2demo.c
codec2-dev/src/c2sim.c
codec2-dev/src/codec2.c
codec2-dev/src/codec2.h
codec2-dev/src/quantise.c
codec2-dev/src/quantise.h

index c39193a21c076959aa34774c304a2dbe6f1cd3aa..7e3026477980ac101706d3e4540b0d6bd1b94aab 100644 (file)
@@ -44,18 +44,20 @@ int main(int argc, char *argv[])
     void          *codec2;
     FILE          *fin;
     FILE          *fout;
+    FILE          *fber = NULL;
     short         *buf;
-    unsigned char *bits;
+    unsigned char *bits, *prev_bits;
     int            nsam, nbit, nbyte, i, byte, frames, bits_proc, bit_errors, error_mode;
     int            nstart_bit, nend_bit, bit_rate;
     int            state, next_state;
-    float          ber, r, burst_length, burst_period, burst_timer;
+    float          ber, r, burst_length, burst_period, burst_timer, ber_est;
     unsigned char  mask;
 
     if ((argc != 4) && (argc != 5) && (argc != 6) && (argc != 7)) {
        printf("basic usage.................: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile\n");
-       printf("uniform errors usage........: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile uniformBER startBit endBit smoothingFlag\n");
+       printf("uniform errors usage........: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile uniformBER startBit endBit\n");
        printf("uniform error on range usage: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile uniformBER\n");
+       printf("demod BER estimate..........: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile BERfile\n");
        printf("two state fading usage......: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile burstLength burstPeriod\n");
        printf("e.g    c2dec 1400 hts1a.c2 hts1a_1400.raw\n");
        printf("e.g    c2dec 1400 hts1a.c2 hts1a_1400.raw 0.9\n");
@@ -106,13 +108,18 @@ int main(int argc, char *argv[])
     buf = (short*)malloc(nsam*sizeof(short));
     nbyte = (nbit + 7) / 8;
     bits = (unsigned char*)malloc(nbyte*sizeof(char));
+    prev_bits = (unsigned char*)malloc(nbyte*sizeof(char));
     frames = bit_errors = bits_proc = 0;
     nstart_bit = 0;
     nend_bit = nbit-1;
 
     if (argc == 5) {
-        error_mode = UNIFORM;
-       ber = atof(argv[4]);
+        /* see if 4th argument is a valid file name */
+        if ( (fber = fopen(argv[4],"rb")) == NULL ) {
+            /* otherwise it must be BER value for uniform errors */
+            ber = atof(argv[4]);
+           error_mode = UNIFORM;
+        }
     }
 
     if (argc == 6) {
@@ -194,12 +201,30 @@ int main(int argc, char *argv[])
             state = next_state;
         }
 
-       codec2_decode(codec2, buf, bits);
+        if (fber != NULL) {
+            if (fread(&ber_est, sizeof(float), 1, fber) != 1) {
+                fprintf(stderr, "ran out of BER estimates!\n");
+                exit(1);
+            }
+            //fprintf(stderr, "ber_est: %f\n", ber_est);
+        }
+        else
+            ber_est = 0.0;
+
+        /* frame repeat logic */
+        if (ber_est > 0.15) {
+            //memcpy(bits, prev_bits, nbyte);
+            // fprintf(stderr, "repeat\n");
+        }
+            
+       codec2_decode(codec2, buf, bits, ber_est);
        fwrite(buf, sizeof(short), nsam, fout);
        //if this is in a pipeline, we probably don't want the usual
         //buffering to occur
         if (fout == stdout) fflush(stdout);
         if (fin == stdin) fflush(stdin);         
+
+        memcpy(prev_bits, bits, nbyte);
     }
 
     if (error_mode)
index 042ec62aab9d97f6d7407de74bb40338b832a65e..84308fd441718255f9fee19aecdd8eef7621c2c3 100644 (file)
@@ -85,7 +85,7 @@ int main(int argc, char *argv[])
 
     while(fread(buf, sizeof(short), nsam, fin) == (size_t)nsam) {
        codec2_encode(codec2, bits, buf);
-       codec2_decode(codec2, buf, bits);
+       codec2_decode(codec2, buf, bits, 0.0);
        fwrite(buf, sizeof(short), nsam, fout);
     }
 
index 255bd1a1e5167105197e1782730f37b2f4537392..750b55d60076f97b2cab19772067ad2d86091e33 100644 (file)
@@ -518,7 +518,7 @@ int main(int argc, char *argv[])
            if (lsp) {
                encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD);
                decode_lsps_scalar(lsps_, lsp_indexes, LPC_ORD);
-               bw_expand_lsps(lsps_, LPC_ORD);
+               bw_expand_lsps(lsps_, LPC_ORD, 50.0, 100.0);
                lsp_to_lpc(lsps_, ak, LPC_ORD);
            }
 
@@ -531,7 +531,7 @@ int main(int argc, char *argv[])
 #ifdef __EXPERIMENTAL__
            if (lspvq) {
                lspvq_quantise(lsps, lsps_, LPC_ORD);
-               bw_expand_lsps(lsps_, LPC_ORD);
+               bw_expand_lsps(lsps_, LPC_ORD, 50.0, 100.0);
                lsp_to_lpc(lsps_, ak, LPC_ORD);
            }
 #endif
@@ -542,7 +542,7 @@ int main(int argc, char *argv[])
                { 
                    float lsps_bw[LPC_ORD];
                    memcpy(lsps_bw, lsps_, sizeof(float)*LPC_ORD);
-                   bw_expand_lsps(lsps_bw, LPC_ORD);                       
+                   bw_expand_lsps(lsps_bw, LPC_ORD, 50.0, 100.0);                          
                    lsp_to_lpc(lsps_bw, ak, LPC_ORD);
                }
            }
@@ -552,7 +552,7 @@ int main(int argc, char *argv[])
                /*  multi-stage VQ from Anssi Ramo OH3GDD */
 
                lspanssi_quantise(lsps, lsps_, LPC_ORD, 5);
-               bw_expand_lsps(lsps_, LPC_ORD);                     
+               bw_expand_lsps(lsps_, LPC_ORD, 50.0, 100.0);                        
                lsp_to_lpc(lsps_, ak, LPC_ORD);
            }
 #endif
@@ -618,7 +618,7 @@ int main(int argc, char *argv[])
            if (lspdt && !decimate) {
                if (frames%2) {
                    lspdt_quantise(lsps, lsps_, lsps__prev, lspdt_mode);
-                   bw_expand_lsps(lsps_, LPC_ORD);
+                   bw_expand_lsps(lsps_, LPC_ORD, 50.0, 100.0);
                    lsp_to_lpc(lsps_, ak, LPC_ORD);
                }
                for(i=0; i<LPC_ORD; i++)
@@ -650,7 +650,7 @@ int main(int argc, char *argv[])
                    for(i=0; i<LPC_ORD; i++)
                        lsps_[i] = lsps__prev2[i];                
                     #endif                 
-                   bw_expand_lsps(lsps_, LPC_ORD);
+                   bw_expand_lsps(lsps_, LPC_ORD, 50.0, 100.0);
                    lsp_to_lpc(lsps_, ak, LPC_ORD);
                }
                
index 3d54ad9b269c3b0be54db484c33ce2c80e9762b0..5bc3be6e9c6b866150af0a9b9ff0ee60f2470ccd 100644 (file)
@@ -64,7 +64,7 @@ void codec2_decode_1600(struct CODEC2 *c2, short speech[], const unsigned char *
 void codec2_encode_1400(struct CODEC2 *c2, unsigned char * bits, short speech[]);
 void codec2_decode_1400(struct CODEC2 *c2, short speech[], const unsigned char * bits);
 void codec2_encode_1300(struct CODEC2 *c2, unsigned char * bits, short speech[]);
-void codec2_decode_1300(struct CODEC2 *c2, short speech[], const unsigned char * bits);
+void codec2_decode_1300(struct CODEC2 *c2, short speech[], const unsigned char * bits, float ber_est);
 void codec2_encode_1200(struct CODEC2 *c2, unsigned char * bits, short speech[]);
 void codec2_decode_1200(struct CODEC2 *c2, short speech[], const unsigned char * bits);
 static void ear_protection(float in_out[], int n);
@@ -248,7 +248,7 @@ void CODEC2_WIN32SUPPORT codec2_encode(struct CODEC2 *c2, unsigned char *bits, s
        codec2_encode_1200(c2, bits, speech);
 }
 
-void CODEC2_WIN32SUPPORT codec2_decode(struct CODEC2 *c2, short speech[], const unsigned char *bits)
+void CODEC2_WIN32SUPPORT codec2_decode(struct CODEC2 *c2, short speech[], const unsigned char *bits, float ber_est)
 {
     assert(c2 != NULL);
     assert(
@@ -269,7 +269,7 @@ void CODEC2_WIN32SUPPORT codec2_decode(struct CODEC2 *c2, short speech[], const
     if (c2->mode == CODEC2_MODE_1400)
        codec2_decode_1400(c2, speech, bits);
     if (c2->mode == CODEC2_MODE_1300)
-       codec2_decode_1300(c2, speech, bits);
+       codec2_decode_1300(c2, speech, bits, ber_est);
     if (c2->mode == CODEC2_MODE_1200)
        codec2_decode_1200(c2, speech, bits);
 }
@@ -534,7 +534,7 @@ void codec2_decode_2400(struct CODEC2 *c2, short speech[], const unsigned char *
     }
     decode_lsps_scalar(&lsps[1][0], lsp_indexes, LPC_ORD);
     check_lsp_order(&lsps[1][0], LPC_ORD);
-    bw_expand_lsps(&lsps[1][0], LPC_ORD);
+    bw_expand_lsps(&lsps[1][0], LPC_ORD, 50.0, 100.0);
  
     /* interpolate ------------------------------------------------*/
 
@@ -717,7 +717,7 @@ void codec2_decode_1600(struct CODEC2 *c2, short speech[], const unsigned char *
     }
     decode_lsps_scalar(&lsps[3][0], lsp_indexes, LPC_ORD);
     check_lsp_order(&lsps[3][0], LPC_ORD);
-    bw_expand_lsps(&lsps[3][0], LPC_ORD);
+    bw_expand_lsps(&lsps[3][0], LPC_ORD, 50.0, 100.0);
  
     /* interpolate ------------------------------------------------*/
 
@@ -890,7 +890,7 @@ void codec2_decode_1400(struct CODEC2 *c2, short speech[], const unsigned char *
     }
     decode_lsps_scalar(&lsps[3][0], lsp_indexes, LPC_ORD);
     check_lsp_order(&lsps[3][0], LPC_ORD);
-    bw_expand_lsps(&lsps[3][0], LPC_ORD);
+    bw_expand_lsps(&lsps[3][0], LPC_ORD, 50.0, 100.0);
  
     /* interpolate ------------------------------------------------*/
 
@@ -1027,7 +1027,7 @@ void codec2_encode_1300(struct CODEC2 *c2, unsigned char * bits, short speech[])
 
 \*---------------------------------------------------------------------------*/
 
-void codec2_decode_1300(struct CODEC2 *c2, short speech[], const unsigned char * bits)
+void codec2_decode_1300(struct CODEC2 *c2, short speech[], const unsigned char * bits, float ber_est)
 {
     MODEL   model[4];
     int     lsp_indexes[LPC_ORD];
@@ -1071,8 +1071,15 @@ void codec2_decode_1300(struct CODEC2 *c2, short speech[], const unsigned char *
     }
     decode_lsps_scalar(&lsps[3][0], lsp_indexes, LPC_ORD);
     check_lsp_order(&lsps[3][0], LPC_ORD);
-    bw_expand_lsps(&lsps[3][0], LPC_ORD);
+    bw_expand_lsps(&lsps[3][0], LPC_ORD, 50.0, 100.0);
  
+    if (ber_est > 0.15) {
+        model[0].voiced =  model[1].voiced = model[2].voiced = model[3].voiced = 0;
+        e[3] = decode_energy(10);
+        bw_expand_lsps(&lsps[3][0], LPC_ORD, 200.0, 200.0);
+        fprintf(stderr, "soft mute\n");
+    }
+
     /* interpolate ------------------------------------------------*/
 
     /* Wo, energy, and LSPs are sampled every 40ms so we interpolate
@@ -1248,7 +1255,7 @@ void codec2_decode_1200(struct CODEC2 *c2, short speech[], const unsigned char *
     }
     decode_lsps_vq(lsp_indexes, &lsps[3][0], LPC_ORD);
     check_lsp_order(&lsps[3][0], LPC_ORD);
-    bw_expand_lsps(&lsps[3][0], LPC_ORD);
+    bw_expand_lsps(&lsps[3][0], LPC_ORD, 50.0, 100.0);
  
     /* interpolate ------------------------------------------------*/
 
index e871b71e8e4cc3d4d2b73e9955b2af8452710e4f..6e82f8f20d6c489acceb3cabacfaef3fac3c9fad 100644 (file)
@@ -58,7 +58,7 @@ struct CODEC2;
 struct CODEC2 * CODEC2_WIN32SUPPORT codec2_create(int mode);
 void CODEC2_WIN32SUPPORT codec2_destroy(struct CODEC2 *codec2_state);
 void CODEC2_WIN32SUPPORT codec2_encode(struct CODEC2 *codec2_state, unsigned char * bits, short speech_in[]);
-void CODEC2_WIN32SUPPORT codec2_decode(struct CODEC2 *codec2_state, short speech_out[], const unsigned char *bits);
+      void CODEC2_WIN32SUPPORT codec2_decode(struct CODEC2 *codec2_state, short speech_out[], const unsigned char *bits, float ber_est);
 int  CODEC2_WIN32SUPPORT codec2_samples_per_frame(struct CODEC2 *codec2_state);
 int  CODEC2_WIN32SUPPORT codec2_bits_per_frame(struct CODEC2 *codec2_state);
 
index bc4cdddb537532ffec33f586d880ffe7fb0be9cf..95a45ce628e211e62a8990b2a65b78333e8d56e0 100644 (file)
@@ -1553,16 +1553,14 @@ void decode_lsps_vq(int *indexes, float *xq, int ndim)
 
 \*---------------------------------------------------------------------------*/
 
-void bw_expand_lsps(float lsp[],
-                   int   order
-)
+void bw_expand_lsps(float lsp[], int order, float min_sep_low, float min_sep_high)
 {
     int i;
 
     for(i=1; i<4; i++) {
        
-       if ((lsp[i] - lsp[i-1]) < 50.0*(PI/4000.0))
-           lsp[i] = lsp[i-1] + 50.0*(PI/4000.0);
+       if ((lsp[i] - lsp[i-1]) < min_sep_low*(PI/4000.0))
+           lsp[i] = lsp[i-1] + min_sep_low*(PI/4000.0);
        
     }
 
@@ -1572,8 +1570,8 @@ void bw_expand_lsps(float lsp[],
     */
 
     for(i=4; i<order; i++) {
-       if (lsp[i] - lsp[i-1] < 100.0*(PI/4000.0))
-           lsp[i] = lsp[i-1] + 100.0*(PI/4000.0);
+       if (lsp[i] - lsp[i-1] < min_sep_high*(PI/4000.0))
+           lsp[i] = lsp[i-1] + min_sep_high*(PI/4000.0);
     }
 }
 
index a8597d99b66f09df61cdd470887ba45a49607059..158919dd57918f43a3c05a2505f4e8b3fa6c9793 100644 (file)
@@ -111,7 +111,7 @@ float speech_to_uq_lsps(float lsp[],
                        int   order
                        );
 int check_lsp_order(float lsp[], int lpc_order);
-void bw_expand_lsps(float lsp[], int order);
+void bw_expand_lsps(float lsp[], int order, float min_sep_low, float min_sep_high);
 void bw_expand_lsps2(float lsp[], int order);
 void locate_lsps_jnd_steps(float lsp[], int order);
 float decode_amplitudes(MODEL *model,