Created
May 9, 2019 06:59
-
-
Save Bemmu/a2faf25bbaf5ebe0ebc85725773d8e46 to your computer and use it in GitHub Desktop.
Test implementation of a polynomial scrambler used in modems and fax machines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Test implementation of a polynomial scrambler used in modems and fax machines as explained in | |
# http://i56578-swl.blogspot.com/2018/03/scrambling-and-descrambling.html | |
class PolynomialScrambler(): | |
def __init__(self, degrees): | |
self.degrees = sorted(degrees) | |
self.step = 0 | |
self.bits = [0] * max(degrees) | |
def state(self): | |
return " ".join(map(str, self.bits)) | |
def shift_and_set_first_bit(self, new_first_bit): | |
self.bits = [new_first_bit] + self.bits[:-1] | |
def scramble(self, bit): | |
print("step % 5d : input %s - %s" % (self.step, bit, self.state()), end = '') | |
self.step += 1 | |
output = None | |
for d in self.degrees: | |
if output is None: | |
output = self.bits[d - 1] | |
else: | |
output = output ^ self.bits[d - 1] | |
# + 1 | |
output = output ^ 1 | |
self.shift_and_set_first_bit(output) | |
print("- output %s" % output) | |
s = PolynomialScrambler([10, 1]) | |
for x in range(20): | |
s.scramble(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment