#include<stdio.h>
bool givenStream[2000];
void ReadStream() {
FILE *f = fopen("stream.txt","r");
for(int i=0;!feof(f);i++)
{
givenStream[i] = fgetc(f)-'0';
fgetc(f);
}
fclose(f);
}
// This gives you the last bit - so needs to be added to the stream of bits for a register
// Works by counting the number of bits, and if even, the last bit must be a 0, if odd it must be a 1 - so cnt counts the bits, then
// returning cnt mod 2 gives you the last bit
int countBitsMod2(int x) {
int temp=x;
int cnt=0;
int pos;
for(pos=0;pos<32;pos++) {
if(temp%2==1) {
cnt+=1;
} else {
cnt+=0;
}
temp=temp/2;
}
return cnt%2;
}
int shiftreg1(int r) {
return (r >> 1) | (((r & 1) ^ ((r >> 6) & 1)) << 6);
}
int shiftreg2(int r) {
// return (r>>1) | (((r&1)^((r>>9)&1))<<10);
return (r >> 1) | (((r & 1) ^ ((r >> 9) & 1)) << 10);
}
int compareStream(int stream[]) {
int matches;
float matchPercentage;
for(int i=0;i<2000;i++) {
if(stream[i]==givenStream[i]) {
matches++;
}
}
matchPercentage = (float(matches)/2000)*100;
if(matchPercentage>60) {
printf("Correlation: %f\n",matchPercentage);
}
return 1;
}
int main() {
// Reg 1, 50% (uncorrelated), tap seq=1,7
// Reg 2, 75% correlation, tap seq=1,10
// Reg 3, 75% correlation, tap seq=1,10,11,13
int reg1length=128,reg2length=2048,reg3length=8192;
int reg1,reg2,reg3;
int reg1Output[2000],reg2Output[2000],reg3Output[2000];
for(reg1=1;reg1<128;reg1++) {
int tempReg1=reg1;
for(int i=0;i<2000;i++) {
reg1Output[i] = countBitsMod2(tempReg1);
tempReg1 = shiftreg1(tempReg1);
}
compareStream(reg1Output);
}
for(reg2=1;reg2<reg2length;reg2++) {
int tempReg2=reg2;
for(int i=0;i<2000;i++) {
reg2Output[i]=countBitsMod2(tempReg2);
tempReg2 = shiftreg2(tempReg2);
}
compareStream(reg2Output);
}
}