www.pudn.com > cyclic3.rar > cyclic3.m


clear; 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% Encoding 
n = 15; k = 7; m = 8;                       % Where n is codeword length, k 
                                            % is message length and m is 
                                            % the number of the messages. 
g = [1 0 0 0 1 0 1 1 1];                    % Set generator polynomial. 
msg = []; 
code = []; 
for a = 1:m 
    msg(a,:)= bitget(a-1,k:-1:1);           % Message is a binary matrix. 
    stage = [0 0 0 0 0 0 0 0];              % Set original registers. 
    for i = 1:k                             % Input messages bit by bit. 
        temp = xor(msg(a,i),stage(1)); 
        s_temp = stage; 
        stage(n-k) = temp; 
        for j = 1:(n-k-1) 
            if g(n-k-j+1) 
                stage(n-k-j) = xor(temp,s_temp(n-k-j+1)); 
            else 
                stage(n-k-j) = s_temp(n-k-j+1); 
            end 
        end 
    end 
    code = [code;msg(a,:) stage]; 
end 
msg                                         % Display original message. 
code                                        % Display codeword. 
 
% Add random error 
error = randerr(m,n,[2 1])                  % We got 1 or 2 errors here. 
code_r = xor(code, error)                   % Put error into codeword. 
 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
ep = [1 0 0 1 0 1 1 1               % position 0,14 
    1 0 1 0 1 1 1 0                 %          1,14 
    1 1 0 1 1 1 0 0                 %          2,14 
    0 0 1 1 1 0 0 0                 %          3,14 
    1 1 1 0 0 1 1 1                 %          4,14 
    0 1 0 0 1 1 1 0                 %          5,14 
    0 0 0 0 1 0 1 1                 %          6,14 
    1 0 0 0 0 0 0 1                 %          7,14 
    1 0 0 0 0 0 1 0                 %          8,14 
    1 0 0 0 0 1 0 0                 %          9,14 
    1 0 0 0 1 0 0 0                 %          10,14 
    1 0 0 1 0 0 0 0                 %          11,14 
    1 0 1 0 0 0 0 0                 %          12,14 
    1 1 0 0 0 0 0 0                 %          13,14 
    1 0 0 0 0 0 0 0];               %          14 
 
% Decoding 
code_de = code_r; 
for b = 1:m 
    for c = 1:n 
        code_in = circshift(code_r(b,:), [1,1-c]);      % Shift the sequence 
        stage_r = [0 0 0 0 0 0 0 0]; 
        for i = 1:n                                     % Calculate syndromes 
            temp_r = xor(code_in(i),stage_r(1)); 
            s_temp_r = stage_r; 
            stage_r(n-k) = temp_r; 
            for j = 1:(n-k-1) 
                if g(n-k-j+1) 
                    stage_r(n-k-j) = xor(temp_r,s_temp_r(n-k-j+1)); 
                else 
                    stage_r(n-k-j) = s_temp_r(n-k-j+1); 
                end 
            end 
        end 
        syndrome = stage_r; 
        for d = 1:n 
            judge = 0; 
            [num,ratio] = biterr(syndrome,ep(d,:)); 
            if num == 0 
                if syndrome == [1 0 0 0 0 0 0 0]        % I only one error happend. 
                    temp_p = c; 
                    code_de(b,temp_p) = ~code_r(b,temp_p); 
                    judge = 1; 
                    break; 
                else                                    % If two errors happend. 
                    temp_p1 = c; 
                    temp_p2 = n-d+c; 
                    code_de(b,temp_p1) = ~code_de(b,temp_p1); 
                    code_de(b,temp_p2) = ~code_de(b,temp_p2); 
                    judge = 1; 
                    break;                              % Jump out from d loop. 
                end 
            else 
            end 
        end 
        if judge 
            break;                                      % Jump out from c loop. 
        else 
        end 
    end 
end 
code_de                                                 % Display decoded codeword. 
msg_de = code_de(:, 1:k)                                % Display decoded message.