PRBS array C code generator (pretty nasty)

Needed an 11 bit PRBS (Pseudo Random Binary Sequence) for use in a project. After some searching online, I came across this sweet wikipedia article on Linear Feedback Shift Registers (LFSRs). This article includes some sample C code to generate a PRBS sequence so I modified this code to generate a sequence that is 2048 bits in length.

// taken from the prbs wikipedia page and modified to make an 11 bit prbs and print to file
// wattnotions april 18th 2015
# include <stdint.h>
# include <stdio.h>
# include <stdbool.h>

bool prbs[2047]; 

int main(void)
{
    uint16_t start_state = 0x7D0;  /* Any nonzero start state will work. */
    uint16_t lfsr = start_state;
    bool bit;
    unsigned period = 0;
	
	FILE *fp;
	fp=fopen("test.txt", "w");
	
	fflush( stdout );
    do
    {
        /* taps: 11 and 9; feedback polynomial: x^11 +x^9 + 1 */
        bit  = ((lfsr >> 0) ^ (lfsr >> 2) ) & 1;
		fprintf(fp, "%d", bit);
        lfsr =  (lfsr >> 1) | (bit << 10);
        ++period;
    } while (lfsr != start_state);
	
	
 
    
}

This 2048 bit sequence in printed as a sequence of 1s and 0s to a .txt file. It looks something like this:
0010010001110110101101011000110001110111101101010010110000110011100111111011110000101001100100011111101……you get the idea

Fairly useless in its current state so some Python magic is required to convert this in to C code.

# opens file containg prbs sequence, parses it into an array of bytes in C code
f = open('Cgen.h', 'w')  # open the file that will hold the generated C code


with open ("test.txt", "r") as myfile:   # open the file containing the prbs 1s and 0s
    data=myfile.read().replace('\n', '')
	

num = 0

f.write('int prbs[] = {\n')
for i in range( ((len(data))//8)  ):
	
	f.write("'0b" + data[i:i+8] +"'")
	if( i != (((len(data))//8) -1) ):
		f.write(", ")
	num = num + 1
	if (num == 7):        # print a newline after every 7th element to keep things pretty
		f.write("\n")    
		num = 0
		
f.write(");")
		

The output from this python script is saved in a file called Cgen.h . It looks like this:
int prbs[] = {
‘0b00100100′, ‘0b01001000′, ‘0b10010001′, ‘0b00100011′, ‘0b01000111′, ‘0b10001110′, ‘0b00011101′,
‘0b00111011′, ‘0b01110110′, ‘0b11101101′, ‘0b11011010′, ‘0b10110101′, ‘0b01101011′, ‘0b11010110′,
‘0b10101101′, ‘0b01011010′, ‘0b10110101′, ‘0b01101011′, ‘0b11010110′, ‘0b10101100′, ‘0b01011000′,
‘0b10110001′, ‘0b01100011′, ‘0b11000110′, ‘0b10001100′, ‘0b00011000′, ‘0b00110001′, ‘0b01100011′,
‘0b11000111′, ‘0b10001110′, ‘0b00011101′, ‘0b00111011′, ‘0b01110111′, ‘0b11101111′, ‘0b11011110′,

….and so on, then near the end it looks like this:

‘0b10011001′, ‘0b00110010′, ‘0b01100101′, ‘0b11001010′, ‘0b10010101′, ‘0b00101011′, ‘0b01010111′,
‘0b10101111′, ‘0b01011111′, ‘0b10111111′, ‘0b01111110′, ‘0b11111100′, ‘0b11111000′, ‘0b11110000′,
‘0b11100001′, ‘0b11000010′, ‘0b10000100′);

So its just an array definition that can be conveniently hidden away in a header file. There’s a github repo called prbs_C_gen with all of the source files and example output files if you fancy taking a peek.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply