Embedded LC BPTC Progress

This commit is contained in:
Cort Buffington 2016-10-19 15:52:43 -05:00
parent a690066d8c
commit f7f0f4a132
3 changed files with 39 additions and 6 deletions

42
bptc.py
View File

@ -9,6 +9,7 @@
from __future__ import print_function
from bitarray import bitarray
import hamming
from time import time
# Does anybody read this stuff? There's a PEP somewhere that says I should do this.
__author__ = 'Cortney T. Buffington, N0MJS'
@ -133,11 +134,6 @@ def encode_19696(_data):
return _bdata
#------------------------------------------------------------------------------
# Used to execute the module directly to run built-in tests
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# BPTC Embedded LC Decoding Routines
#------------------------------------------------------------------------------
@ -147,10 +143,39 @@ def encode_19696(_data):
# BPTC Embedded LC Encoding Routines
#------------------------------------------------------------------------------
# Accepts 12 byte LC header + 5-bit checksum, converts to binary and builts out the BPTC
# encoded result with hamming(16,11,4) and parity.
def encode_emblc(_lc, _csum5):
# Create a bitarray from the 4 bytes of LC data (includes 5-bit checksum).
_binlc = bitarray(endian='big')
_binlc.frombytes(_lc)
# Insert the checksum bits at the right location in the matrix (this is actually faster than with a for loop)
_binlc.insert(32, _csum5[0])
_binlc.insert(43,_csum5[1])
_binlc.insert(54,_csum5[2])
_binlc.insert(65,_csum5[3])
_binlc.insert(76,_csum5[4])
# Insert the hamming bits at the right location in the matrix
for index in xrange(0,112,16):
for hindex,hbit in zip(xrange(index+11,index+16), hamming.enc_16114(_binlc[index:index+11])):
_binlc.insert(hindex,hbit)
# TO DO NEXT:
# ADD PARITY BIT CALUCATIONS AND INTERLEAVE, RETURN A TUPLE OR LIBRARY OR EACH SEGMENT OF THE LC
#------------------------------------------------------------------------------
# Used to execute the module directly to run built-in tests
#------------------------------------------------------------------------------
if __name__ == '__main__':
from binascii import b2a_hex as h
from time import time
import crc
def to_bytes(_bits):
#add_bits = 8 - (len(_bits) % 8)
@ -205,4 +230,9 @@ if __name__ == '__main__':
print('ENCODED vs. DECODED:')
print('enc:', enc_data)
print('dec:', deint_data)
print(enc_data == deint_data)
print(enc_data == deint_data)
orig_data = '\x00\x10\x20\x00\x0c\x30\x2f\x9b\xe5'
orig_csum = crc.csum5(orig_data)
emblc = encode_emblc(orig_data, orig_csum)

2
crc.py
View File

@ -22,12 +22,14 @@ def csum5(_data):
_data = bytearray(_data)
accum = 0
assert len(_data) == 9, 'csum5 expected 9 bytes of data and got something else'
for i in xrange(9):
accum += _data[i]
accum = chr(accum % 31)
csum = bitarray()
csum.frombytes(accum)
del csum[0:3]
return csum

View File

@ -117,6 +117,7 @@ def dec_1393(_data):
# ENCODER - returns a bitarray object containing the hamming checksums
def enc_16114(_data):
assert len(_data) == 11, 'Hamming Encoder 16,11,4: Data not 11 bits long {}'
csum = bitarray(5)
csum[0] = _data[0] ^ _data[1] ^ _data[2] ^ _data[3] ^ _data[5] ^ _data[7] ^ _data[8]
csum[1] = _data[1] ^ _data[2] ^ _data[3] ^ _data[4] ^ _data[6] ^ _data[8] ^ _data[9]