function x=decode_ham74(y)
% x=decode_ham74(y)
%
% Hamming 15,11 channel decoder
% y : received coded bit sequence (with errors)
% x : decoded bit sequence
% y,x are column vectors

a=length(y)/7;   

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

if a~=round(a)
    error('The length of y should be a multiple of 7.');
end

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

% Parity check matrix H
H = [P' eye(3)];

% The message Y
Y=zeros(a,7);
for m=1:a
    Y(m,:) = y(7*(m-1)+1:7*m);
end

% Syndrom matrix S
S = mod(Y*H',2);

for m=1:a
    if S(m,:) == [1 1 1]
        Y(m,1) = Y(m,1) ~= 1;
    elseif S(m,:) == [0 1 1]
        Y(m,2) = Y(m,2) ~= 1;
    elseif S(m,:) == [1 0 1]
        Y(m,3) = Y(m,3) ~= 1;
    elseif S(m,:) == [1 1 0]
        Y(m,4) = Y(m,4) ~= 1;
    end
end

% Cut away the parity
X=Y(:,1:4);

x=zeros(a*4,1);
for m=1:a
     x(4*(m-1)+1:4*m) = X(m,:).';
end
 
 