added bit error generation to c2dec
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Wed, 15 Feb 2012 03:05:14 +0000 (03:05 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Wed, 15 Feb 2012 03:05:14 +0000 (03:05 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@318 01035d8c-6547-0410-b346-abe4f91aad63

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

index 56cfcca7f52913f9c1a46e4de92fa92f285f9b51..e5dc7a398449546ac33709b1defa3037a0fb71ed 100644 (file)
@@ -40,9 +40,10 @@ int main(int argc, char *argv[])
     FILE          *fout;
     short         *buf;
     unsigned char *bits;
-    int            nsam, nbit, nbyte;
+    int            nsam, nbit, nbyte, i, byte, frames, bit_errors;
+    float          ber, r;
 
-    if (argc != 4) {
+    if (argc < 4) {
        printf("usage: c2dec 2500|1500|1125 InputBitFile OutputRawSpeechFile\n");
        printf("e.g    c2dec 1500 hts1a.c2 hts1a_1500.raw\n");
        exit(1);
@@ -73,23 +74,43 @@ int main(int argc, char *argv[])
        exit(1);
     }
 
+    if (argc == 5)
+       ber = atof(argv[4]);
+    else
+       ber = 0.0;
+
     codec2 = codec2_create(mode);
     nsam = codec2_samples_per_frame(codec2);
     nbit = codec2_bits_per_frame(codec2);
     buf = (short*)malloc(nsam*sizeof(short));
     nbyte = (nbit + 7) / 8;
     bits = (unsigned char*)malloc(nbyte*sizeof(char));
-    
+    frames = bit_errors = 0;
+
     while(fread(bits, sizeof(char), nbyte, fin) == nbyte) {
+       frames++;
+       if (ber != 0.0) {
+           for(i=0; i<nbit; i++) {
+               r = (float)rand()/RAND_MAX;
+               if (r < ber) {
+                   byte = i/8;
+                   //printf("nbyte %d nbit %d i %d byte %d\n", nbyte, nbit, i, byte);
+                   bits[byte] ^= 1 << (i - byte*8);
+                   bit_errors++;
+               }
+           }
+       }
        codec2_decode(codec2, buf, bits);
        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);
-          
+        if (fin == stdin) fflush(stdin);         
     }
 
+    if (ber != 0.0)
+       printf("actual BER: %1.3f\n", (float)bit_errors/(frames*nbit));
+
     codec2_destroy(codec2);
 
     free(buf);
index bb01bd660a14f8fc70c58298f9756f6cdcff0788..6204e9e0d9c090509c0d7d3a23e8f8532c8b2154 100644 (file)
@@ -596,6 +596,7 @@ void codec2_decode_1500(struct CODEC2 *c2, short speech[], const unsigned char *
     /* decode frame 2 LSPs and model amplitudes */
 
     decode_lsps_scalar(lsps_, lsp_indexes, LPC_ORD);
+    check_lsp_order(lsps_, LPC_ORD);
     bw_expand_lsps(lsps_, LPC_ORD);
     lsp_to_lpc(lsps_, ak, LPC_ORD);
     energy = decode_energy(energy_index);
index 09e816273ee151ddf6b32dbea25a9b9e8d86417a..33e1e4e58366cb9201786ec6c2993213cf9936e6 100644 (file)
@@ -534,7 +534,7 @@ void check_lsp_order(float lsp[], int lpc_order)
 
     for(i=1; i<lpc_order; i++)
        if (lsp[i] < lsp[i-1]) {
-           printf("swap %d\n",i);
+           //printf("swap %d\n",i);
            tmp = lsp[i-1];
            lsp[i-1] = lsp[i]-0.05;
            lsp[i] = tmp+0.05;
index 6e04173d87e3aca43b81fc2e7c549ae3cb742cf1..bad8137c65b26ef542ee17bc4a15fd1cacb32a9a 100644 (file)
@@ -94,6 +94,7 @@ float speech_to_uq_lsps(float lsp[],
                        float w[],
                        int   order
                        );
+void check_lsp_order(float lsp[], int lpc_order);
 void bw_expand_lsps(float lsp[], int order);
 void locate_lsps_jnd_steps(float lsp[], int order);