function z=encode_ham74(x)
% z=encode_ham74(x)
%
% Hamming 7,4 channel encoder
% x : uncoded bit sequence
% z : encoded bit sequence
% x,z are column vectors

if size(x,2) ~= 1
    error('x should be a column.');
end

a=length(x)/4; 

if a~=round(a)
    error('The length of x must be a multiple of 4.');
end

% Parity matrix P
P = [1 1 1;...
    0 1 1;...
    1 0 1;...
    1 1 0];

% Generator matrix G
G = [eye(4) P];

% Message M
M=zeros(a,4);
for m=1:a
    M(m,:) = x(4*(m-1)+1:4*m);
end

Z=mod(M*G,2);

z=zeros(a*7,1);
for m=1:a
     z(7*(m-1)+1:7*m) = Z(m,:).';
end
