%***********************************************************
% MATLAB function implements a binary sequence generator
% that generates symbols with prior probabilities [p_o,p_1]
% USAGE: s_out = bingen(N,p_vec,opt)
% N : number of samples needed
% opt : Option specified 
%     : 1 ---> {1,-1}
%     : 2 ---> {0,1}
% p_vec : vector containing prior probabilities 
%         p_o (-1), p_1 (+1)
% AUTHOR : Balu Santhanam
% DATE   : 02/26/01
%***********************************************************
function s_out = bingen(N,p_vec,opt)
u = rand(1,N); s_out = zeros(1,N);
if nargin < 2
   error('Insufficient input arguments')
elseif opt ~= 1 & opt ~= 2
   error('Invalid option')
elseif p_vec(1) > 1 | p_vec(1) <  0
   error('Invalid prior probability entered')
elseif p_vec(2) > 1 | p_vec(2) <  0
   error('Invalid prior probability entered')
end
switch opt 
case 1, 
      ind1 = find(u >= p_vec(1)); 
      ind2 = find(u < p_vec(1)); 
      s_out(ind1) = 1*ones(1,length(ind1));
      s_out(ind2) = -1*ones(1,length(ind2));
otherwise, 
      ind1 = find(u >= p_vec(1)); 
      ind2 = find(u < p_vec(1)); 
      s_out(ind1) = 1*ones(1,length(ind1));
      s_out(ind2) = 0*ones(1,length(ind2));
end
