added maximum length sequence

This commit is contained in:
Ahmet Inan 2020-03-31 18:05:10 +02:00
commit 1e7a25e1a8
2 changed files with 39 additions and 0 deletions

View file

@ -27,6 +27,10 @@ Sometimes we need a sequence of ["random enough"](https://en.wikipedia.org/wiki/
Here a [Pseudorandom number generator](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) can help by prodiving a deterministic and thus repeatable sequence of numbers.
[George Marsaglia](https://en.wikipedia.org/wiki/George_Marsaglia) discovered a class of simple and fast pseudorandom number generators, which he called [Xorshift](https://en.wikipedia.org/wiki/Xorshift).
### [mls.hh](mls.hh)
[Maximum length sequences](https://en.wikipedia.org/wiki/Maximum_length_sequence) have useful [correlation properties](https://en.wikipedia.org/wiki/Maximum_length_sequence#Correlation_property).
### [bitstream.hh](bitstream.hh)
When dealing with unaligned and arbitrary-bit-sized elements in a data stream, the bitwise stream container might help avoiding some headaches.

35
mls.hh Normal file
View file

@ -0,0 +1,35 @@
/*
Maximum length sequence
Copyright 2020 Ahmet Inan <inan@aicodix.de>
*/
#pragma once
namespace CODE {
class MLS
{
static int hibit(unsigned n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return n ^ (n >> 1);
}
int poly, test, reg;
public:
MLS(int poly = 0b100000000000000001001, int reg = 1) : poly(poly), test(hibit(poly)>>1), reg(reg) {}
bool operator()()
{
bool fb = reg & test;
reg <<= 1;
reg ^= fb * poly;
return fb;
}
};
}