From: drowe67 Date: Sun, 10 Jan 2016 20:02:09 +0000 (+0000) Subject: implemented and tested scrambler to make demods life easier for long strings of 0s X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=d7f8dfcab3ded678e104ed2d930448b3607d1696;p=freetel-svn-tracking.git implemented and tested scrambler to make demods life easier for long strings of 0s git-svn-id: https://svn.code.sf.net/p/freetel/code@2623 01035d8c-6547-0410-b346-abe4f91aad63 --- diff --git a/codec2-dev/src/horus_l2.c b/codec2-dev/src/horus_l2.c index bbdbf8ea..873c5368 100644 --- a/codec2-dev/src/horus_l2.c +++ b/codec2-dev/src/horus_l2.c @@ -36,13 +36,13 @@ 3/ Generate some tx_bits as input for testing with fsk_horus: - $ gcc horus_l2.c -o horus_l2 -Wall -DGEN_TX_BITS + $ gcc horus_l2.c -o horus_l2 -Wall -DGEN_TX_BITS -DSCRAMBLER $ ./horus_l2 $ more ../octave/horus_tx_bits_binary.txt 4/ Unit testing interleaver: - $ gcc horus_l2.c -o horus_l2 -Wall -DINTERLEAVER -DTEST_INTERLEAVER + $ gcc horus_l2.c -o horus_l2 -Wall -DINTERLEAVER -DTEST_INTERLEAVER -DSCRAMBLER 5/ Compile for use as decoder called by fsk_horus.m and fsk_horus_stream.m: @@ -72,6 +72,7 @@ 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); +void scramble(unsigned char *inout, int nbytes); /* Functions ----------------------------------------------------------*/ @@ -268,6 +269,13 @@ int horus_l2_encode_tx_packet(unsigned char *output_tx_data, interleave(&output_tx_data[sizeof(uw)], num_tx_data_bytes-2); #endif + /* optional scrambler to prevent long strings of the same symbol + which upsets the modem - we dont scramble UW */ + + #ifdef SCRAMBLER + scramble(&output_tx_data[sizeof(uw)], num_tx_data_bytes-2); + #endif + return num_tx_data_bytes; } @@ -282,11 +290,15 @@ void horus_l2_decode_rx_packet(unsigned char *output_payload_data, unsigned char *pin = input_rx_data; int ninbit, ingolay, ningolay, paritybyte, nparitybits; int ninbyte, shift, inbit, golayparitybit, i, outbit, outbyte, noutbits, outdata; + int num_tx_data_bytes = horus_l2_get_num_tx_data_bytes(num_payload_data_bytes); - /* optional interleaver - we dont interleave UW */ + /* optional scrambler and interleaver - we dont interleave UW */ + + #ifdef SCRAMBLER + scramble(&input_rx_data[sizeof(uw)], num_tx_data_bytes-2); + #endif #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 @@ -567,6 +579,55 @@ int main(void) { } #endif + +#ifdef SCRAMBLER + +/* 16 bit DVB additive scrambler as per Wikpedia example */ + +void scramble(unsigned char *inout, int nbytes) +{ + int nbits = nbytes*8; + int i, ibit, ibits, ibyte, ishift, mask; + uint16_t scrambler = 0x4a80; /* init additive scrambler at start of every frame */ + uint16_t scrambler_out; + + /* in place modification of each bit */ + + for(i=0; i> 1) ^ (scrambler & 0x1); + + /* modify i-th bit by xor-ing with scrambler output sequence */ + + ibyte = i/8; + ishift = i%8; + ibit = (inout[ibyte] >> ishift) & 0x1; + ibits = ibit ^ scrambler_out; // xor ibit with scrambler output + + mask = 1 << ishift; + inout[ibyte] &= ~mask; // clear i-th bit + inout[ibyte] |= ibits << ishift; // set to scrambled value + + /* update scrambler */ + + scrambler >>= 1; + scrambler |= scrambler_out << 14; + + #ifdef DEBUG0 + printf("i: %02d ibyte: %d ishift: %d ibit: %d ibits: %d scrambler_out: %d\n", + i, ibyte, ishift, ibit, ibits, scrambler_out); + #endif + + } + + #ifdef DEBUG0 + printf("\nScrambler Out:\n"); + for (i=0; i