fec enc and dec appears to be correcting errors, not thoroughly tested
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Mon, 4 Mar 2013 05:56:33 +0000 (05:56 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Mon, 4 Mar 2013 05:56:33 +0000 (05:56 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@1188 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/src/Makefile.am
codec2-dev/src/Makefile.in
codec2-dev/src/fec_dec.c
codec2-dev/src/fec_enc.c
codec2-dev/src/golay23.c

index 80f77a9c10dfcaec03b6ec6af8d78aca2c11e9ce..1e856aaf047f94009d50bcdd2662d2e84efa4445 100644 (file)
@@ -183,10 +183,10 @@ fdmdv_interleave_LDFLAGS = $(LIBS)
 insert_errors_SOURCES =  insert_errors.c
 insert_errors_LDFLAGS = $(LIBS)
 
-fec_enc_SOURCES =  fec_enc.c
+fec_enc_SOURCES =  fec_enc.c golay23.c
 fec_enc_LDADD = $(lib_LTLIBRARIES) 
 fec_enc_LDFLAGS = $(LIBS)
 
-fec_dec_SOURCES =  fec_dec.c
+fec_dec_SOURCES =  fec_dec.c  golay23.c
 fec_dec_LDADD = $(lib_LTLIBRARIES) 
 fec_dec_LDFLAGS = $(LIBS)
index 3cd9ace634c62835e7534a79ec0b66095552cc48..4c9ef875686e8f6d79a6c809a89afad62feaeb19 100644 (file)
@@ -110,10 +110,10 @@ am_fdmdv_put_test_bits_OBJECTS = fdmdv_put_test_bits.$(OBJEXT) \
        fdmdv.$(OBJEXT) kiss_fft.$(OBJEXT)
 fdmdv_put_test_bits_OBJECTS = $(am_fdmdv_put_test_bits_OBJECTS)
 fdmdv_put_test_bits_LDADD = $(LDADD)
-am_fec_dec_OBJECTS = fec_dec.$(OBJEXT)
+am_fec_dec_OBJECTS = fec_dec.$(OBJEXT) golay23.$(OBJEXT)
 fec_dec_OBJECTS = $(am_fec_dec_OBJECTS)
 fec_dec_DEPENDENCIES = $(am__DEPENDENCIES_1)
-am_fec_enc_OBJECTS = fec_enc.$(OBJEXT)
+am_fec_enc_OBJECTS = fec_enc.$(OBJEXT) golay23.$(OBJEXT)
 fec_enc_OBJECTS = $(am_fec_enc_OBJECTS)
 fec_enc_DEPENDENCIES = $(am__DEPENDENCIES_1)
 generate_codebook_SOURCES = generate_codebook.c
@@ -385,10 +385,10 @@ fdmdv_interleave_SOURCES = fdmdv_interleave.c
 fdmdv_interleave_LDFLAGS = $(LIBS)
 insert_errors_SOURCES = insert_errors.c
 insert_errors_LDFLAGS = $(LIBS)
-fec_enc_SOURCES = fec_enc.c
+fec_enc_SOURCES = fec_enc.c golay23.c
 fec_enc_LDADD = $(lib_LTLIBRARIES) 
 fec_enc_LDFLAGS = $(LIBS)
-fec_dec_SOURCES = fec_dec.c
+fec_dec_SOURCES = fec_dec.c  golay23.c
 fec_dec_LDADD = $(lib_LTLIBRARIES) 
 fec_dec_LDFLAGS = $(LIBS)
 all: all-am
@@ -552,6 +552,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fec_enc.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/generate_codebook.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/genlspdtcb.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/golay23.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/insert_errors.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/kiss_fft.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libcodec2_la-codebook.Plo@am__quote@
index 86c5b3d66058ed7e5c7e502ed9f3b2ec0f48d4c6..52912cbc6a46879090b7185a3ae5e7a32e549384 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "codec2.h"
 #include "codec2_fdmdv.h"
+#include "golay23.h"
 
 #include <assert.h>
 #include <stdio.h>
@@ -48,6 +49,7 @@ int main(int argc, char *argv[])
     int           *unpacked_output_bits;
     int            mode, Nc, bit, byte;
     int            i;
+    int            recd_codeword, codeword1, codeword2;
 
     if (argc < 3) {
        printf("%s InputFromModemWithFECFile OutputToCodec2File\n", argv[0]);
@@ -99,11 +101,13 @@ int main(int argc, char *argv[])
     unpacked_output_bits = (int*)malloc(bits_per_output_frame*sizeof(int));
     assert(unpacked_output_bits != NULL);
     
-    fprintf(stderr, "input bits: %d  input_bytes: %d  output_bits: %d  output_bytes: %d\n",
-            bits_per_input_frame,  bytes_per_input_frame, bits_per_output_frame,  bytes_per_output_frame);
+    // fprintf(stderr, "input bits: %d  input_bytes: %d  output_bits: %d  output_bytes: %d\n",
+    //        bits_per_input_frame,  bytes_per_input_frame, bits_per_output_frame,  bytes_per_output_frame);
 
     /* main loop */
 
+    golay23_init();
+
     while(fread(packed_input_bits, sizeof(char), bytes_per_input_frame, fin) == (size_t)bytes_per_input_frame) {
 
        /* unpack bits, MSB first */
@@ -119,7 +123,51 @@ int main(int argc, char *argv[])
        }
        assert(byte == bytes_per_input_frame);
 
-        for(i=0; i<bits_per_output_frame; i++)
+        #ifdef TEST
+        /* Some test bit errors (not comprehesnive) */
+        unpacked_input_bits[0] = (unpacked_input_bits[0] ^ 1) & 0x1;
+        unpacked_input_bits[23] = (unpacked_input_bits[23] ^ 1) & 0x1;
+        #endif
+
+        /* decode first codeword */
+
+        recd_codeword = 0;
+        for(i=0; i<12; i++) {
+            recd_codeword <<= 1;
+            recd_codeword |= unpacked_input_bits[i];
+        }
+        for(i=bits_per_output_frame; i<bits_per_output_frame+11; i++) {
+            recd_codeword <<= 1;
+            recd_codeword |= unpacked_input_bits[i];
+        }
+        codeword1 = golay23_decode(recd_codeword);
+        //fprintf(stderr, "received codeword1: 0x%x  decoded codeword1: 0x%x\n", recd_codeword, codeword1);
+
+        for(i=0; i<12; i++) {
+            unpacked_output_bits[i] = codeword1 >> (22-i);
+        }
+
+        /* decode second codeword */
+
+        recd_codeword = 0;
+        for(i=12; i<24; i++) {
+            recd_codeword <<= 1;
+            recd_codeword |= unpacked_input_bits[i];
+        }
+        for(i=bits_per_output_frame+11; i<bits_per_output_frame+11+11; i++) {
+            recd_codeword <<= 1;
+            recd_codeword |= unpacked_input_bits[i];
+        }
+        codeword2 = golay23_decode(recd_codeword);
+        //fprintf(stderr, "received codeword2: 0x%x  decoded codeword2: 0x%x\n", recd_codeword, codeword2);
+
+        for(i=0; i<12; i++) {
+            unpacked_output_bits[12+i] = codeword2 >> (22-i);
+        }
+
+        /* unprotected bits */
+
+        for(i=24; i<bits_per_output_frame; i++)
             unpacked_output_bits[i] = unpacked_input_bits[i];
 
         /* pack bits, MSB first  */
index 109ae4ec24c110309192151d8b781b0dcba5bba7..26c4d11589a9cd706e6a3179f1d291a26d9e867c 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "codec2.h"
 #include "codec2_fdmdv.h"
+#include "golay23.h"
 
 #include <assert.h>
 #include <stdio.h>
@@ -49,7 +50,8 @@ int main(int argc, char *argv[])
     unsigned char *packed_output_bits;
     int           *unpacked_output_bits;
     int            mode, Nc, bit, byte;
-    int            i;
+    int            i,j;
+    int            data, codeword1, codeword2;
 
     if (argc < 3) {
        printf("%s InputFromCodecFile OutputToModemWithFECFile\n", argv[0]);
@@ -102,11 +104,13 @@ int main(int argc, char *argv[])
     unpacked_output_bits = (int*)malloc(bits_per_output_frame*sizeof(int));
     assert(unpacked_output_bits != NULL);
     
-    fprintf(stderr, "input bits: %d  input_bytes: %d  output_bits: %d  output_bytes: %d\n",
-            bits_per_input_frame,  bytes_per_input_frame, bits_per_output_frame,  bytes_per_output_frame);
+    //fprintf(stderr, "input bits: %d  input_bytes: %d  output_bits: %d  output_bytes: %d\n",
+    //        bits_per_input_frame,  bytes_per_input_frame, bits_per_output_frame,  bytes_per_output_frame);
 
     /* main loop */
 
+    golay23_init();
+
     while(fread(packed_input_bits, sizeof(char), bytes_per_input_frame, fin) == (size_t)bytes_per_input_frame) {
 
        /* unpack bits, MSB first */
@@ -122,10 +126,46 @@ int main(int argc, char *argv[])
        }
        assert(byte == bytes_per_input_frame);
 
+        /* add FEC  ----------------------------------*/
+
+        /* Protect first 24 bits with (23,12) Golay Code.  The first
+           24 bits are the most sensitive, as they contain the
+           pitch/energy VQ and voicing bits. This uses 56 + 11 + 11 =
+           78 bits, so we have two spare in 80 bit frame sent to
+           modem. */
+
+        /* first codeword */
+
+        data = 0;
+        for(i=0; i<12; i++) {
+            data <<= 1;
+            data |= unpacked_input_bits[i];
+        }
+        codeword1 = golay23_encode(data);
+        //fprintf(stderr, "data1: 0x%x codeword1: 0x%x\n", data, codeword1);
+
+        /* second codeword */
+
+        data = 0;
+        for(i=12; i<24; i++) {
+            data <<= 1;
+            data |= unpacked_input_bits[i];
+        }
+        codeword2 = golay23_encode(data);
+        //fprintf(stderr, "data: 0x%x codeword2: 0x%x\n", data, codeword2);
+
+        /* now pack output frame with parity bits at end to make them
+           as far apart as possible from the data the protect.  Parity
+           bits are LSB of the Golay codeword */
+
         for(i=0; i<bits_per_input_frame; i++)
             unpacked_output_bits[i] = unpacked_input_bits[i];
-        for(i=bits_per_input_frame; i<bits_per_output_frame; i++)
-            unpacked_output_bits[i] = 1;
+        for(j=0; i<bits_per_input_frame+11; i++,j++) {
+            unpacked_output_bits[i] = (codeword1 >> (10-j)) & 0x1;
+        }
+        for(j=0; i<bits_per_input_frame+11+11; i++,j++) {
+            unpacked_output_bits[i] = (codeword2 >> (10-j)) & 0x1;
+        }
 
         /* pack bits, MSB first  */
 
index 03024191d3deb6b5115682d53e9da594382fc94d..9cb3830fe4278a7d4dc41692bc00c802fdcab9dd 100644 (file)
@@ -83,6 +83,8 @@
  * a[] = auxiliary array to generate correctable error patterns
  */
 
+static int inited =  0;
+
 static int encoding_table[4096], decoding_table[2048];
 static int position[23] = { 0x00000001, 0x00000002, 0x00000004, 0x00000008,
                             0x00000010, 0x00000020, 0x00000040, 0x00000080,
@@ -229,6 +231,7 @@ void golay23_init(void ) {
         decoding_table[get_syndrome(temp)] = temp;
     }
 
+    inited = 1;
 }
 
 /*---------------------------------------------------------------------------*\
@@ -243,6 +246,8 @@ void golay23_init(void ) {
 \*---------------------------------------------------------------------------*/
 
 int golay23_encode(int data) {
+    assert(inited);
+
     //printf("data: 0x%x\n", data);
     assert(data <= 0xfff);
     return encoding_table[data];
@@ -259,6 +264,8 @@ int golay23_encode(int data) {
 \*---------------------------------------------------------------------------*/
 
 int golay23_decode(int received_codeword) {
+    assert(inited);
+
     return received_codeword ^= decoding_table[get_syndrome(received_codeword)];
 }