%
% PRBS Generator
%
%         u = prbs( nbit )
%
% where  u    = sequence vector
%               ( 0's and 1's )
%        nbit = bit length of sequence
% 
% Bob Newell, February 1996
%
%--------------------------------------------
%
function u = prbs( nbit )
%
% check we know the sequence
if nbit == 3, fbseq = [ 2 ];
elseif nbit == 10, fbseq = [ 3 ];
elseif nbit == 11, fbseq = [ 5 3 2 ]; 
elseif nbit == 12, fbseq = [ 6 4 1 ]; 
elseif nbit == 13, fbseq = [ 4 3 1 ]; 
elseif nbit == 14, fbseq = [ 10 6 1 ]; 
elseif nbit == 15, fbseq = [ 1 ]; 
elseif nbit == 16, fbseq = [ 12 3 1 ]; 
elseif nbit == 17, fbseq = [ 3 ]; 
elseif nbit == 18, fbseq = [ 7 ]; 
elseif nbit == 19, fbseq = [ 5 2 1 ]; 
elseif nbit == 20, fbseq = [ 3 ]; 
else, disp( 'Unknown "nbit" value' ); return; 
end;
% initialise
register = zeros( nbit, 1 );
register(1) = 1;
m = 2 ^ nbit - 1;
u = zeros( m, 1 );
fbseq = [ 2 3 ];
% generate the sequence
for i = 1:m,
% calculate the feedback
  feedback = register( nbit );
  for j = fbseq,
    feedback = xor( feedback, register(j) );
  end;
% shift register
  for j = nbit:-1:2,
    register(j) = register(j-1);
  end;
% insert feedback
  register(1) = feedback;
%  disp( register' );
% keep cycling
  u(i) = feedback;
end;
%
return;
%--------------------------------------------------
% the end    
