function vit_init( L, M, N, Nv)
%
% initialization for generalized Viterbi algorithm
%
% R. Perry, June 2001
%
% Usage:
%
%   vit_init( L, M, N, Nv)
%
%     L  - number of L-best paths to keep
%     M  - number of states
%     N  - number of input data points
%     Nv - number of Viterbi instances
%
% If Nv is not specified, it defaults to 1.
%
% Example usage, with Nv = 1:
%
%   vit_init( L, M, N);
%
%   for t=1:N
%     vit_update;
%   end
%
%   paths = vit_paths;
%
% After calling vit_init, instances of the Viterbi algorithm can be
% invoked, for example, like this:
%
%   vit_init( L, M, N, Nv);
%
%   for t=1:N              % for each input data point
%     for vnum=1:Nv        % for each Viterbi instance
%       vit_update(vnum);  % advance to next data point
%     end
%   end
%
%   paths = zeros(N,L,Nv);
%
%   for vnum=1:Nv
%     paths(:,:,vnum) = vit_paths(vnum);
%   end
%
% and each instance can access the variables associated with the
% other instances.  This may be useful for running separate trellises
% for each user in parallel in the multiuser ISI problem, though
% we may have to include some more global variables to help with that.
%
% See also:
%
%   vit_update, vit_paths

% global variables
%
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

% Nv set to 1 if not specified
%
if( nargin < 4)
  Nv = 1;
end

V  = zeros( L, M, N, Nv);
Vl = V;

% initialize cost like zstate=1 in old Viterbi
%
Vw = HUGE_VAL*ones( L, M, Nv);
Vw(1,1,:) = 0;

Vparam = zeros( 1, 4 + Nv);
Vparam(1:4) = [L, M, N, Nv];

Vlprev = [1:L]' * ones(1,M);
Vlprev = Vlprev(:);             % 1..L, M times

Vprev = ones(L,1) * [1:M];      % 1..M, L times
Vprev = Vprev(:);               % e.g. for M=3, L=2 it is  1 1 2 2 3 3
