Moving code around to help my understanding. file names most probably wrong right now but will update when understand it better

This commit is contained in:
nzkarit
2017-09-05 21:35:26 +12:00
parent 22ed603fd3
commit 960a697976
5 changed files with 243 additions and 235 deletions
+42
View File
@@ -0,0 +1,42 @@
"""
Hamming and Manchester Encoding example
Author: Joel Addison
Date: March 2013
Functions to do (7,4) hamming encoding and decoding, including error detection
and correction.
Manchester encoding and decoding is also included, and by default will use
least bit ordering for the byte that is to be included in the array.
"""
def extract_bit(byte, pos):
"""
Extract a bit from a given byte using MS ordering.
ie. B7 B6 B5 B4 B3 B2 B1 B0
"""
return (byte >> pos) & 0x01
def manchester_encode(byte):
"""
Encode a byte using Manchester encoding. Returns an array of bits.
Adds two start bits (1, 1) and one stop bit (0) to the array.
"""
# Add start bits (encoded 1, 1)
# manchester_encoded = [0, 1, 0, 1]
manchester_encoded = []
# Encode byte
for i in range(7, -1, -1):
if extract_bit(byte, i):
manchester_encoded.append(0)
manchester_encoded.append(1)
else:
manchester_encoded.append(1)
manchester_encoded.append(0)
# Add stop bit (encoded 0)
# manchester_encoded.append(1)
# manchester_encoded.append(0)
return manchester_encoded