function w = get_cost( t, prev, next, el, vnum)
%
% compute incremental state transition cost from the
% el'th best of prev state, to next state, at time t,
% for Viterbi instance number vnum
%
% may use el here for PSP
%
% R. Perry, June 2001

global HUGE_VAL;
global TYPE;
global z;

if( TYPE == 1) % single-user, known/constant-channel

  global trans;

  w = trans( prev, next);

  if( w ~= HUGE_VAL)
    w = z(t) - w;
    w = w * w;
  end

elseif( TYPE == 2) % multi-user, known-channel

  global states;
  global Memory;
  global Nusers;
  global FIR_save;
  global trans;

  w = HUGE_VAL;

  if( trans(prev,next)) % transition is feasible

    w = 0;

    for u1 = 1:Nusers
      w1 = z(t,u1);
      for u2 = 1:Nusers
        ui = (u2-1)*Memory + [1:Memory];
        us = [states(ui,next)' states(ui(Memory),prev)];
        w1 = w1 - us*FIR_save(:,t,u2,u1);
      end
      w = w + w1*w1;
    end

  end

elseif( TYPE == 22) % multi-user, separate independent trellises

  global states;
  global Memory;
  global Nusers;
  global FIR_save;
  global trans;

  w = HUGE_VAL;

  if( trans(prev,next)) % transition is feasible

    us = [states(:,next)' states(Memory,prev)];

    w = z(t,vnum) - us*FIR_save(:,t,vnum,vnum);

    w = w * w;

  end

elseif( TYPE == 23) % multi-user, separate trellises

% this is just an ad-hoc experiment

  global states;
  global Memory;
  global Nusers;
  global FIR_save;
  global FIR_length;
  global trans;
  global ex;

  w = HUGE_VAL;

  if( trans(prev,next)) % transition is feasible

    w = z(t,vnum);

    for u2 = 1:Nusers
      if( u2 == vnum) % that's me
        us = [states(:,next)' states(Memory,prev)];
      else
        us = [flipud(states(:,1)); ex(:,1,u2)]; % best estimate
        us = us(t+Memory:-1:t)';
      end
        w = w - us*FIR_save(:,t,u2,vnum);
    end

    w = w * w;

  end

elseif( TYPE == 24) % multi-user, separate trellises, soft estimates

% this is just an ad-hoc experiment

  global states;
  global Memory;
  global Nusers;
  global FIR_save;
  global FIR_length;
  global trans;
  global Vparam;
  global VW;

  w = HUGE_VAL;

  if( trans(prev,next)) % transition is feasible

    w = z(t,vnum);

    for u2 = 1:Nusers
      if( u2 == vnum) % that's me
        us = [states(:,next)' states(Memory,prev)];
      else
        T = Vparam( 4 + u2);
        if( T == 0)
          us = zeros(1,FIR_length);
        else
          L = Vparam(1);
          u2_paths = vit_paths( u2);   % vit_paths() also sets VW
          u2_ex = get_ex( u2_paths);
          p = exp( -(VW - min(VW)));   % change costs to probabilities
          p = p/sum(p);                %  and normalize so sum is 1.
	  u2_ex = u2_ex * diag(p);     % scale by p
          u2_ex = sum(u2_ex,2);        % then marginalize

          us = [flipud(states(:,1)); u2_ex; zeros(t-T,1)]; % soft estimate
          us = us(t+Memory:-1:t)';
        end
      end
        w = w - us*FIR_save(:,t,u2,vnum);
    end

    w = w * w;

  end

else

  error( 'get_cost: unknown TYPE');

end
