% [nstates,ninput,trellis_trans,trellis_out] = trellis_par23(nstates,H)  
% Returns trellis description of the systematic convolutional code described
% by the parity matrix H, where the number of states is
% nstates. The algorithm applies to codes of rate 2/3.
%
% George Ginis, June 2001

function [nstates,ninput,trellis_transition,trellis_output] = trellis_par23(nstates,H) 

  ninput=2;
  
  % convert octal description to binary description
  m=log2(nstates);
  Hbin=par_binconv(H,m);
  
  % observer canonical form is assumed
  
  for s=0:2^m-1,           % iterate for each possible state
    
    for in=0:2^ninput-1,   % iterate for each possible input
      
      sm=dec2binm(s,m+1);    % convert state to binary description
      inm=dec2binm(in,ninput);   % convert input to binary description
      
      % compute parity bit output 
      % v_0=s_1+v_2*h_{1,0}+v_1*h_{2,0}
      v0=sm(1)+inm(2)*Hbin(1,1)+inm(1)*Hbin(1,(m+1)+1);
      v0=mod(v0,2);
      
      
      for st_index=1:m % update all states
	
	sm_new(st_index) = ...
	   Hbin(1,(m+1)*2+1+st_index)*v0+...
	    inm(2)*Hbin(1,1+st_index)+inm(1)*Hbin(1,(m+1)+1+st_index)+...
	    sm(st_index+1);
	sm_new(st_index)=mod(sm_new(st_index),2);
	    
      end
      
      snew=binm2dec(sm_new);  % new state in decimal form
      out=2*in+v0;            % corresponding output in decimal form

      trellis_transition(s+1,in+1)=snew+1;
      trellis_output(s+1,in+1)=out;
      
    end
    
  end
  
  
  