Skip to content

Instantly share code, notes, and snippets.

@andreasWallner
Created June 12, 2014 22:12
Show Gist options
  • Save andreasWallner/11c9784cb42d8777217c to your computer and use it in GitHub Desktop.
Save andreasWallner/11c9784cb42d8777217c to your computer and use it in GitHub Desktop.
Galois LSFR implementation for python
""" simple LFSR generator
Uses galois algorithmn to return next bit of LFSR.
To initialize the generator pass the seed value (one
value that the generator will generate, != 0) and
the used polynom.
Observe that for a n bit polynom, x^n has to be in
the polynom, which should be the case anyhow therefore e.g.:
x^4 + x^3 + 1 = 0b1100
>>> import itertools
>>> x = lsfr(0b0110, 0b1100)
>>> list(itertools.islice(x, 16))
[0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0]
"""
def lsfr(seed, polynom):
data = seed
poly = polynom
while 1:
lsb = data & 1
data = data >> 1
if lsb != 0:
data = data ^ poly
yield 1
else:
yield 0
""" polynoms for maximum length sequences """
maxLenPoly = {
2 : 0b11,
3 : 0b110,
4 : 0b1100,
5 : 0b10100,
6 : 0b110000,
7 : 0b1100000,
8 : 0b10111000,
9 : 0b100010000,
10 : 0b1001000000,
11 : 0b10100000000,
12 : 0b111000001000,
13 : 0b1110010000000,
14 : 0b11100000000010,
15 : 0b110000000000000,
16 : 0b1011010000000000,
17 : 0b10010000000000000,
18 : 0b100000010000000000,
19 : 0b1110010000000000000,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment