単純なボリュームコード符号化プログラム

1941 ワード

以下は簡単な畳み込み符号化プログラムのMATLAB実装である.
        % *****************************************
        % *                                       *
        % *   This function implements the        *
        % *   state machine for a convolutional   *
        % *   code with rate 1/2 and K = 3.       *
        % *****************************************

function output = conv_code(mssg)

    input = zeros(1,length(mssg) + 3);
    input(1:length(mssg)) = mssg;               % padding message with 0 in the front
    output = zeros(1,2*(length(mssg) + 2));     % output of covolutional code
    k = 1;
    state = 0;  % initial state
    i = 1;
    while i <= length(mssg) + 3
        switch(state)
            case 0  % state 0
                if(input(i) == 0)
                    state = 0;  % next state 0
                    y = [0 0];
                else
                    state = 2;  % next state 2
                    y = [1 1];
                end
            case 1  % state 1
                if(input(i) == 0)
                    state = 0;  % next state 0
                    y = [1 1];
                else
                    state = 2;  % next state 2
                    y = [0 0];
                end
            case 2  % state 2
                if(input(i) == 0)
                    state = 1;  % next state 1
                    y = [1 0];
                else
                    state = 3;  % next state 3
                    y = [0 1];
                end
            case 3  % state 3
                if(input(i) == 0)
                    state = 1;  % next state 1
                    y = [0 1];
                else
                    state = 3;  % next state 3
                    y = [1 0];
                end        
        end
        if(i ~= length(mssg) + 3)
            output(k) = y(1);
            output(k + 1) = y(2);
        end
        k = k + 2;
        i = i + 1;
    end 
end