Skip to content

Instantly share code, notes, and snippets.

@Bemmu
Created May 9, 2019 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bemmu/a2faf25bbaf5ebe0ebc85725773d8e46 to your computer and use it in GitHub Desktop.
Save Bemmu/a2faf25bbaf5ebe0ebc85725773d8e46 to your computer and use it in GitHub Desktop.
Test implementation of a polynomial scrambler used in modems and fax machines
# 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