% global variables for Viterbi
%
global HUGE_VAL; % 1e99
global DEBUG;    % for debugging
global V;        % state array
global Vw;       % cost array
global Vparam;   % parameters

HUGE_VAL = 1e99;

DEBUG = 0;

% global variables for ISI
%
global FIR;     % channel response
global Memory;  % channel memory
global states;  % trellis states
global M;       % number of states
global trans;   % state transition cost matrix
global z;       % received data
global N;       % number of received data values

% data setup for symbol values (-1,1)
% KB ECE 8770 Fall 2000 section 3.2, Example 2
%

% channel response was not specified, so make one up:
%
FIR = [0.407 0.815 0.407]';

Memory = size(FIR,1) - 1;

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

M = size(states,2);

trans = FSM_trans( states, FIR);

% create an input sequence
%
N = 5;

x = 2*(rand(1,N) > 0.5) - 1

% go through the channel
%
xt = [fliplr(states(:,1)'), x];

y = zeros(1,N);

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

% add some noise
%
SNR = 0

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

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

z = y + noise;

% run the Viterbi algorithm
%
vit1_init( M, N);

for t=1:N
  vit1_update;
  % t, Vw % check Vw for debugging
end

path = vit1_paths;

ex = get_ex( path)
