% check g as a conditional mean estimator
% ML exhaustive search
%
%%%
%%
% global variables for Viterbi
%
global HUGE_VAL; % 1e99
global DEBUG;    % for debugging
global V;        % main state array
global Vl;       % array for L-best indices
global Vw;       % cost array
global Vparam;   % parameters
global Vprev;    % previous state indices
global Vlprev;   % previous state el indices
global VW;       % final path costs

HUGE_VAL = 1e99;

DEBUG = 0;
%
%%
%%%

%%%
%%
% global variables for the cost functions
%
% use big GG_tmp and qq_tmp arrays or not
%
global USE_GGqq_tmp; USE_GGqq_tmp = 1
global G;
global q;

global z;
global trans;
global states;

global FIR_length;
global Memory;
global FIR_0;
global FIR_save;

global Nchips;
global Nusers;
global Nsyms;

global TYPE;     % problem type

global alpha;
global alpha2;
global var_v;
global stdev_v;
global C_inv;

global var_noise;
global SNR;

global GG_tmp;
global qq_tmp;
global GG;
global qq;

global ex_known;
global ex_gm;
global ex_p;

%
%%
%%%

format short g;

% initialize rand() and randn() with time-of-day
%
  seed=floor(sum(100*clock))
  rand( 'state', seed);
  randn( 'state', seed);

%%%
%%
% input data setup
%
Nusers = 1
Nchips = 1
FIR_length = 3

Nsyms = 5	% number of received data samples (symbols) per user

alpha = 0.995
alpha2 = alpha*alpha;

var_v = 0.01
stdev_v = sqrt( var_v);

Memory = FIR_length - 1;     % channel memory size

states = FSM( [-1 1], Nusers*Memory);
trans = get_trans_multi( states); % state transition feasibility matrix

M = size(states,2);          % number of states

% full inv(C) matrix would have FIR_length*Nusers*Nchips rows and columns
% but here we are assuming that it is block diagonal with these blocks:
%
C_inv = eye( FIR_length*Nusers, FIR_length*Nusers) / var_v;

% user#1 has higher SNR
%
s = 20 % [10 20 30];
SNR_array = [ s; ones(Nusers-1,1)*(s-10)]

trials_array = 3 * ones(size(s))

for SNR_index = 1:size(SNR_array,2)

  SNR = SNR_array(:,SNR_index)

  BER_exh = zeros( Nusers, 1);

  trials = trials_array(SNR_index)

  for trial_count = 1:trials

    % generate users' sequences, FIRs, and received data
    %
    get_data;

    %%
    % exhaustive search, Gauss-Markov cost
    %
      [ex, FIR_ml] = get_GM_ML;
      tmp = (x ~= ex);
      BER_exh = BER_exh + sum(tmp)';

      disp( '---')

      FIR_0
      FIR_n = FIR_save(:,:,Nchips,Nsyms)
      FIR_ml = reshape( FIR_ml, FIR_length, Nusers)

  end % trials

  BER_exh = BER_exh / (trials*Nsyms)


end % SNR
