% example script to run a non-time-varying known-channel
% single-user ISI problem
%
% R. Perry, June 2001
%
%%%
%%
% 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
%
global z;
global trans;
global states;
global FIR;
global FIR_length;
global Memory;

global TYPE;     % problem type

TYPE = 1;        % single-user, known/constant-channel
%
%%
%%%

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
%
% an exact double null at the band edge
% H(z) = 0.4 z^-2 (z+1) (z+1)
%
FIR = [0.4 0.8 0.4]';

FIR_length = size(FIR,1);

Memory = FIR_length - 1;     % channel memory size

states = FSM( [-1 1], Memory);

trans = get_trans( states, FIR); % state transition cost matrix

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

N = 4;                       % number of data samples

x = 2*(rand(N,1) > 0.5) - 1; % input sequence

xt = [flipud(states(:,1)); x];

y = zeros(N,1);

for i=1:N
    y(i) = xt(i+Memory:-1:i)' * FIR;          % ISI
end

SNR = 8;

var_noise = 10^(-SNR/10) * sum(FIR.*FIR);     % noise variance

noise = sqrt(var_noise).*randn(N,1);          % random noise

z = y + noise;                                % signal plus noise
%
%%
%%%

%%%
%%
% run Viterbi
%
L = 2;
Nv = 1;

vit_init( L, M, N, Nv);

for t=1:N
  vit_update;
end

paths = vit_paths;

ex = get_ex( paths);
