我确实为基于以下文档的布尔数组中的线性反馈包移位寄存器编写了一个非常简单的 C 类代码:
http://courses.cse.tamu.edu/walker/csce680/lfsr_table.pdf https://www.xilinx.com/support/documentation/application_notes/xapp052.pdf
n = 63
编码:
#include <stdio.h>
#include <stdint.h>
#include <math.h>
int main() {
bool lfsr[63] = {true};
const int iterations = 16384 * 8;
int deviation = 0;
///*
for(int bit = 0; bit <= 62; bit++) {
int x = 0, y = 0;
for(int j = 0; j < iterations; j++) {
bool feedback = lfsr[62] ^ lfsr[61];
for(int k = 62; k >= 1; k--)
lfsr[k] = lfsr[k - 1];
lfsr[0] = feedback;
if(lfsr[bit])
x++;
else
y++;
}
deviation += y - x;
printf("bit %d \t= %d <-> %d (deviation: %d)\r\n", bit, y, x, y - x);
}
printf("Total deviation: %d\r\n", deviation);
}
结果:
bit 2 = 76565 <-> 54507 (deviation: 22058)
bit 3 = 68863 <-> 62209 (deviation: 6654)
...
bit 58 = 65636 <-> 65436 (deviation: 200)
bit 59 = 65155 <-> 65917 (deviation: -762)
bit 60 = 65030 <-> 66042 (deviation: -1012)
bit 61 = 65654 <-> 65418 (deviation: 236)
bit 62 = 64667 <-> 66405 (deviation: -1738)
Total deviation: 306364
关键是您可以看到分布几乎不均匀/均匀。事实上,偏差对于我的目的来说太高了。
我犯了错误还是我对LFSR一无所知?