function [ outdat ] = code( dat, m, n, k )
%[ outsig ] = code( insig, m, n, k )
%   This file assumes M=2 and that the # of channels is even.
%   Channels are paired to make M=4 bits, the signal is encoded
%   and then the coded date is again split into N channels


[rows, cols] = size(dat);

%Create M=4 data
%shortdat = zeros(rows/2, cols);
%for i = 1:rows/2
%    shortdat(i,:) = dat(2*i-1,:) + 4*dat(2*i, :);
%end


codedat = gf(zeros(rows/2, ceil(n*cols/k)), m);
%outdat = rsenc(outdat, n, k)

for r = 1:rows
    for c = 1:ceil(cols/k)
        msg = gf(dat(r, (c-1)*k+1:c*k), m);
        codemsg = rsenc(msg, n, k);
        codedat(r, (c-1)*n+1:n*c) = codemsg(1, :);
    end
end    

outdat = gf2dec(codedat, m, primpoly(m));
    
%outdat = codedat;    
%for i = 1:rows/2
%    outdat(2*i-1, :) = mod(codedat(i), 4);
%    outdat(2*i, :) = (codedat(i)-outdat(2*i-1, :))/4;
%end
    
