From 4eccd854b87c25faf71584c54a6281b3470d59bf Mon Sep 17 00:00:00 2001 From: drowe67 Date: Mon, 4 Mar 2013 05:56:33 +0000 Subject: [PATCH] fec enc and dec appears to be correcting errors, not thoroughly tested git-svn-id: https://svn.code.sf.net/p/freetel/code@1188 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2-dev/src/Makefile.am | 4 +-- codec2-dev/src/Makefile.in | 9 ++++--- codec2-dev/src/fec_dec.c | 54 +++++++++++++++++++++++++++++++++++--- codec2-dev/src/fec_enc.c | 50 +++++++++++++++++++++++++++++++---- codec2-dev/src/golay23.c | 7 +++++ 5 files changed, 110 insertions(+), 14 deletions(-) diff --git a/codec2-dev/src/Makefile.am b/codec2-dev/src/Makefile.am index 80f77a9c..1e856aaf 100644 --- a/codec2-dev/src/Makefile.am +++ b/codec2-dev/src/Makefile.am @@ -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) diff --git a/codec2-dev/src/Makefile.in b/codec2-dev/src/Makefile.in index 3cd9ace6..4c9ef875 100644 --- a/codec2-dev/src/Makefile.in +++ b/codec2-dev/src/Makefile.in @@ -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@ diff --git a/codec2-dev/src/fec_dec.c b/codec2-dev/src/fec_dec.c index 86c5b3d6..52912cbc 100644 --- a/codec2-dev/src/fec_dec.c +++ b/codec2-dev/src/fec_dec.c @@ -28,6 +28,7 @@ #include "codec2.h" #include "codec2_fdmdv.h" +#include "golay23.h" #include #include @@ -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> (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> (22-i); + } + + /* unprotected bits */ + + for(i=24; i #include @@ -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> (10-j)) & 0x1; + } + for(j=0; i> (10-j)) & 0x1; + } /* pack bits, MSB first */ diff --git a/codec2-dev/src/golay23.c b/codec2-dev/src/golay23.c index 03024191..9cb3830f 100644 --- a/codec2-dev/src/golay23.c +++ b/codec2-dev/src/golay23.c @@ -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)]; } -- 2.25.1