% ML exhaustive search for Gauss-Markov h
%
% originally written by Hai Chen, May 2000
%
% assumes that the symobls are -1 and +1
%
% for N=13, Memory=2, a runtime test
%  using get_A.m and FSM_inc.m:	    64.502 secs.
%  using get_A_C.c and FSM_inc_C.c: 52.986 secs,
%  a decrease of 17.854%
%
function min_b = get_ML_OLD()

global z;
global N;
global var_noise;
global HUGE_VAL;
global inv_Rh;
global inv_Rh_d;
global alpha;
global alpha2;

global pilot;
global pilot_index;

s = [-1; 1];
nsyms = size(s,1);
nstates = prod( nsyms*ones(1,N));	% nstates = nsyms ** N

c = ones( 1, N);

min_cost = HUGE_VAL;

% if using pilot bits
%
% pb = pilot(pilot_index);

for i = 1:nstates

  b = s(c);

%% if using pilot bits
%
%  if( all( b(pilot_index) == pb) )

    % using get_A.m
    %
    % A = get_A( b);
  
    % using get_A_C.c
    %
    A = get_A_C( b);
  
  %%% debug check
  %%
  %B = get_A_C(b);
  %t = (B ~= A);
  %if( any(t(:)) )
  %  disp( 'get_A and get_A_C mismatch!');
  %end
  %%
  %%%
  
    t = A(1,:)';
    q = t*(z(1)/var_noise) + inv_Rh_d;   
    G = inv( t*(t'/var_noise) + inv_Rh );
  
    cost = - log(det(G)) - q'*G*q;
  
    for k = 2:N-1
    
        t = A(k,:)';
        q = t*z(k)/var_noise + (1-alpha)*inv_Rh_d + alpha*inv_Rh*G*q;
        G = inv( t*(t'/var_noise) + inv_Rh + alpha2*(inv_Rh-inv_Rh*G*inv_Rh) );
  
        cost = cost - log(det(G)) - q'*G*q;
    end      
    
    t = A(N,:)';
    q = t*z(N)/var_noise + inv_Rh_d + alpha*inv_Rh*G*q;
    G = inv( t*(t'/var_noise) + inv_Rh - alpha2*inv_Rh*G*inv_Rh );
  
    cost = cost - log(det(G)) - q'*G*q;
   
    if( cost < min_cost)
      min_cost = cost;
      min_b = b;
    end
  
%  end % if pilot

  % using FSM_inc.m
  %
  % c = FSM_inc( c, nsyms);

  % using FSM_inc_C.c
  %
  FSM_inc_C( c, nsyms);
  
  %% debug check
  %%
  % d = FSM_inc( c, nsyms);
  % FSM_inc_C( c, nsyms);
  % t = (d ~= c);
  % if( any(t(:)) )
  %   disp( 'FSM_inc and FSM_inc_C mismatch!');
  % end
  
end
