function [osqc] = conv_encode(trels, n, p, isqc, m)
% Convolutional encoder.
% Finds the binary output sequence matching the supplied
% .. Trellis graph and binary input sequence.
% trels represents the Trellis diagram of the encoder.
% .. Its dimensions are (n*p) x 1 x 3.
% n is the number of encoder states.
% p is the number of distinct input combinations in the code.
% m is the number of bits contained in isqc.
% For convenience, each output symbol value of the Trellis
% .. is encoded as real + imaginary components of one number.
%
% D. Bozarth 03-23-08 SSU Engineering Science
%
osqc = zeros(1, m * log2(n));   % (#output symbols) * (bits per symbol)
vrow = 1;

for i = 0:(m - 1)     % Each input signal bit => log2(n) output bits.
    val = isqc(1, i + 1);
    bse = (vrow - 1) * p;   % base pointer for edge rows
    bso = i * p;            % base pointer for output bits
    
    for j = 1:p
        erow = bse + j;
        
        if val == trels(erow, 1, 2)
            vrow  = trels(erow, 1, 1);
            sym   = trels(erow, 1, 3);
            
            % testing - supply function as arg
            bin = cpx_to_bin2(sym, 1);
            
            for k = 1:p
               osqc(1, bso + k) = bin(1, k);
            end
                
            break;
        end
    end
end
% end function