function paths = vit1_paths( vnum)
%
% get trellis path for Viterbi algorithm
%
% R. Perry, June 2001
%
% Usage:
%
%   path = vit1_paths( vnum)
%
%     vnum - Viterbi instance number
%
% If vnum is not specified, it defaults to 1.
%
% vit1_update() must be called at least once after vit1_init()
% before invoking vit1_paths().  The path returned will
% extend up to the time of the last trellis update.
%
% Besides returning the path array, vit1_paths() also sets
% the global VW array to contain the path cost.
%
% Vparam(1) is set to 1 in vit1_init, but before calling vit1_paths
% Vparam(1) could be set anywhere from 1 to M if you want to get
% more of the possible final paths.
%
% See also:
%
%   vit1_init, vit1_update

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

% get parameters
%
L  = Vparam(1);
%
% L is set to 1 in vit1_init, but before calling vit1_paths
% L could be set anywhere from 1 to M if you want to get
% more of the possible final paths.
%
M  = Vparam(2);
N  = Vparam(3);
Nv = Vparam(4);

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

% check L
%
if( L < 1 | L > M)
  error('L out of bounds');
end

% check vnum
%
if( vnum < 1 | vnum > Nv)
  error('vnum out of bounds');
end

% get/check time index
%
T  = Vparam( 4 + vnum);

if( T < 1)
  error( 'trellis has not yet advanced to t>1');
end

% get the best path
%
paths = zeros( L, T);

w = Vw(:,vnum);
[s1 s2] = sort(w(:));
VW = s1(1:L);
smin = s2(1:L); % state indices

paths(:,T) = smin;

for t = T:-1:2 % backtrack through the trellis

  smin  = V( smin, t, vnum);

  paths(:,t-1) = smin;

end % t

paths = paths';
