function x=decode_ham1511(y)
% x=decode_ham1511(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)/15;   

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 15.');
end

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

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

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

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

for m=1:a
    if S(m,:) == [1 1 0 0]
        Y(m,1) = Y(m,1) ~= 1;
    elseif S(m,:) == [1 0 1 0]
        Y(m,2) = Y(m,2) ~= 1;
    elseif S(m,:) == [0 1 1 0]
        Y(m,3) = Y(m,3) ~= 1;
    elseif S(m,:) == [1 1 1 0]
        Y(m,4) = Y(m,4) ~= 1;
    elseif S(m,:) == [1 0 0 1]
        Y(m,5) = Y(m,5) ~= 1;
    elseif S(m,:) == [0 1 0 1]
        Y(m,6) = Y(m,6) ~= 1;
    elseif S(m,:) == [1 1 0 1]
        Y(m,7) = Y(m,7) ~= 1;
    elseif S(m,:) == [0 0 1 1]
        Y(m,8) = Y(m,8) ~= 1;
    elseif S(m,:) == [1 0 1 1]
        Y(m,9) = Y(m,9) ~= 1;
    elseif S(m,:) == [0 1 1 1]
        Y(m,10) = Y(m,10) ~= 1;
    elseif S(m,:) == [1 1 1 1]
        Y(m,11) = Y(m,11) ~= 1;
    elseif S(m,:) == [1 0 0 0]
        Y(m,12) = Y(m,12) ~= 1;
    elseif S(m,:) == [0 1 0 0]
        Y(m,13) = Y(m,13) ~= 1;
    elseif S(m,:) == [0 0 1 0]
        Y(m,14) = Y(m,14) ~= 1;
    elseif S(m,:) == [0 0 0 1]
        Y(m,15) = Y(m,15) ~= 1;
    end
end

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

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