% Pseudo-random binary signal generator. Truncated exponential distribution
% of switching period.
%     Tore Gustafsson, 22.10.1992. Version 4.0 24.2.1993
% Usage:
%   y = prbs(n, mist, mast, F, opt)
%
% Input arguments:
%   n     number of values in the produced time series.
%   mist  minimum discrete time interval (integer >= 1) between switches.
%   mast  maximum discrete time interval (integer >= mist) between switches.
%   F     distribution parameter (0.001 <= F <= 0.999). Default value :
%         F = 0.8. F = 1 means exponential distribution of switching periods,
%         F = 0 means uniform distribution of switching periods.
%   opt   opt=1 signal with different intensity
%
% Output argument:
%   y     time series of produced PRBS signal. y is a column vector of 
%         dimension n x 1.
%
  function y = prbs(n, mist, mast, F, opt )
  if (nargin<3),
    error('prbs requires three input arguments')
  end
  if (mist<1),
    mist = 1;
  end
  if (mast<mist),
    mast = mist;
  end
  if (n<1),
    n = 1;
  end
  if (nargin<4),
    F = 0.8;
  end
  if (nargin<5),
    opt = 0 ;
  end
  if (F<=0),
    F = 0.001;
  elseif (F>1),
    F = 0.999;
  end
  mini  = mist - 0.4999;
  maxi  = mast + 0.4999;
  maxu  = 1;
  minu  = (1 - F) * maxu;
  deltu = maxu - minu;
  delta = (maxi - mini) / log(minu);
  state = sign(rand(1) - 0.5);
  if (state==0), 
    state = 1;
  end
  j = 1;
  y = zeros(n,1);
  while j <= n
    t  = round( mini + log(minu + rand(1) * deltu) * delta);
    state = -state;
    if opt, aux= state*rand(1); else aux=state; end
    for k = 1:t,
      y(j,1) = aux;
      j      = j + 1;
      if j>n, break, end
    end
  end
