Last active
August 29, 2015 14:09
-
-
Save p3trus/c275923a99bad8dd1f03 to your computer and use it in GitHub Desktop.
Naive implementation of a pseudo random binary sequence generator.
This file contains 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
import collections | |
from operator import xor | |
class PRBS(object): | |
"""Generates PRBS sequences. | |
E.g.:: | |
# Generate a single period of a prbs7 sequence with | |
# polynom x**7 + x**6 + 1 | |
prbs7_gen = PRBS([7, 6], [0,0,0,0,0,0,1]) | |
prbs7_seq = [i for i in it.islice(prbs7_gen, len(prbs7_gen))] | |
# Generate two periods of a prbs9 sequence with | |
# polynom x**9 + x**5 + 1 | |
prbs9_gen = PRBS([9, 5], [0,0,0,0,0,0,0,0,1]) | |
prbs9_seq = [i for i in it.islice(prbs9_gen, len(prbs9_gen))] | |
""" | |
def __init__(self, taps, sequence): | |
self.taps = taps | |
self._buffer = collections.deque(sequence, maxlen=len(sequence)) | |
def __len__(self): | |
return 2**len(self._buffer) - 1 | |
def __iter__(self): | |
return self | |
def next(self): | |
out = self._buffer[-1] | |
i, j = self.taps | |
a0, a1 = self._buffer[i - 1], self._buffer[j - 1] | |
self._buffer.appendleft(xor(a0, a1)) | |
return a0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment