From 1afd96370945d36cf687a279c062ba13e75c61d6 Mon Sep 17 00:00:00 2001 From: drowe67 Date: Fri, 1 Jan 2016 11:44:53 +0000 Subject: [PATCH] first pass at interleaver. Unit test works, however mixed results when integrating with protocol. More testing needed git-svn-id: https://svn.code.sf.net/p/freetel/code@2601 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2-dev/src/horus_l2.c | 183 +++++++++++++++++++++++++++++++++----- 1 file changed, 162 insertions(+), 21 deletions(-) diff --git a/codec2-dev/src/horus_l2.c b/codec2-dev/src/horus_l2.c index 895a5f23..a5513dfb 100644 --- a/codec2-dev/src/horus_l2.c +++ b/codec2-dev/src/horus_l2.c @@ -58,6 +58,14 @@ $ gcc horus_l2.c -o horus_l2 -Wall -DGEN_TX_BITS $ ./horus_l2 $ more ../octave/horus_tx_bits_binary.txt + + 4/ Testing interleaver: + + $ gcc horus_l2.c -o horus_l2 -Wall -DINTERLEAVER -DTEST_INTERLEAVER + + 5/ Use as decoder with fsk_horus.m and fsk_horus_stream.m: + + $ gcc horus_l2.c -o horus_l2 -Wall -DDEC_RX_BITS -DHORUS_L2_RX \*---------------------------------------------------------------------------*/ @@ -76,10 +84,15 @@ static char uw[] = {'$','$'}; +/* Function Prototypes ------------------------------------------------*/ + int32_t get_syndrome(int32_t pattern); void golay23_init(void); int golay23_decode(int received_codeword); unsigned short gen_crc16(unsigned char* data_p, unsigned char length); +void interleave(unsigned char *inout, int nbytes); + +/* Functions ----------------------------------------------------------*/ /* We are using a Golay (23,12) code which has a codeword 23 bits @@ -268,6 +281,12 @@ int horus_l2_encode_tx_packet(unsigned char *output_tx_data, #endif assert(pout == (output_tx_data + num_tx_data_bytes)); + /* optional interleaver - we dont interleave UW */ + + #ifdef INTERLEAVER + interleave(&output_tx_data[sizeof(uw)], num_tx_data_bytes-2); + #endif + return num_tx_data_bytes; } @@ -283,6 +302,13 @@ void horus_l2_decode_rx_packet(unsigned char *output_payload_data, int ninbit, ingolay, ningolay, paritybyte, nparitybits; int ninbyte, shift, inbit, golayparitybit, i, outbit, outbyte, noutbits, outdata; + /* optional interleaver - we dont interleave UW */ + + #ifdef INTERLEAVER + int num_tx_data_bytes = horus_l2_get_num_tx_data_bytes(num_payload_data_bytes); + interleave(&input_rx_data[sizeof(uw)], num_tx_data_bytes-2); + #endif + pin = input_rx_data + sizeof(uw) + num_payload_data_bytes; /* Read input data bits one at a time. When we have 12 read 11 parity bits. Golay decode. @@ -445,6 +471,114 @@ void horus_l2_decode_rx_packet(unsigned char *output_payload_data, } #endif +#ifdef INTERLEAVER +/* call for interleaving and de-interleaving, operates in place */ + +static unsigned char scrambler[] = { + 0, + 16, + 13, + 2, + 23, + 28, + 3, + 21, + 4, + 5, + 22, + 6, + 7, + 15, + 8, + 27, + 9, + 10, + 11, + 30, + 26, + 12, + 14, + 17, + 1, + 19, + 20, + 24, + 18, + 25, + 29, + 31 +}; + + +void interleave(unsigned char *inout, int nbytes) +{ + int nbits = nbytes*8; + int nbits2 = nbits/2; + int i, j, ibit, jbit, ibyte, ishift, jbyte, jshift, mask; + + /* swap bits in first half with those in 2nd half, using + small scrambling table to move bits about a bit */ + + for(i=0; i> ishift) & 0x1; + //printf("i: %02d ibyte: %d ishift: %d ibit: %d\n", i, ibyte, ishift, ibit); + + jbyte = j/8; + jshift = j%8; + jbit = (inout[jbyte] >> jshift) & 0x1; + //printf("j: %02d jbyte: %d jshift: %d jbit: %d\n", j, jbyte, jshift, jbit); + + /* write jbit to ibit position */ + + mask = 1 << ishift; + inout[ibyte] &= ~mask; // clear ibit + inout[ibyte] |= jbit << ishift; + + /* write ibit to jbit position */ + + mask = 1 << jshift; + inout[jbyte] &= ~mask; // clear jbit + inout[jbyte] |= ibit << jshift; + } +} +#endif + + +#ifdef TEST_INTERLEAVER +int main(void) { + int nbytes = 43; + unsigned char inout[nbytes]; + unsigned char inter[nbytes]; + unsigned char incopy[nbytes]; + int i; + + /* copy of input for later comp */ + + for(i=0; i