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
%
% R. Perry, Oct. 2001

global HUGE_VAL;
global TYPE;
global z;

global states;
global Memory;
global Nusers;
global Nchips;
global FIR_save;
global var_noise;
global trans;

if( TYPE == 30) % multi-user, Gauss-Markov

  w = HUGE_VAL;

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

    w = 0;

    %%
    %% using known channel -->> must change to Gauss-Markov cost
    %%
    for j = 1:Nchips
      w1 = z(j,t);
      for u = 1:Nusers
        ui = (u-1)*Memory + [1:Memory];
        us = [states(ui,next)' states(ui(Memory),prev)];
        w1 = w1 - us*FIR_save(:,u,j,t);
      end
      w = w + w1*w1;
    end

  end
elseif ( TYPE == 40)
     w = HUGE_VAL;

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

    w = 0;

    %%
    %% using known channel -->> must change to Gauss-Markov cost
    %%
    for j = 1:Nchips
      w1 = z(j,t);
     % for u = 1:Nusers        
         for u=1:1                            %NEW LINE%%%%%%%
        ui = (u-1)*Memory + [1:Memory];
        us = [states(ui,next)' states(ui(Memory),prev)];
        % w1 = w1 - us*FIR_save(:,u,j,t)
        w1=w1-us*FIR_save(:,vnum,j,t)   ;      %NEW LINE%%%%%%
     end %end of for u
     %disp('end of for u loop in get cost')
     %keyboard
      w = w + w1*w1;
    end %end of for j

  end

   
else

  error( 'get_cost: unknown TYPE');

end
