⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encode.m

📁 数字通信第四版原书的例程
💻 M
字号:
function [code, added] = encode(msg, n, k, method, opt)
%ENCODE Error-control code encoding computation.
%       CODE = ENCODE(MSG, N, K) encodes binary message specified in MSG using
%       Hamming coding technique. The code word length is N and the message
%       length is K. The format of MSG can be either a vector or K column
%       matrix. A Hamming code has code word length 2^M -1, where M is an
%       integer no less than 3. The message length is 2^M - M - 1. Hamming
%       code is a single error-correction code.
%
%       CODE = ENCODE(MSG, N, K, METHOD, OPT) encodes the message signal MSG
%       using an error-control coding method such that the message length is
%       K and code word length is N with the coding method as specified in
%       variable METHOD. OPT is an extra optional parameter used in some of
%       the methods.
%       
%        METHOD              ENCODE SCHEME
%       'hamming'   Hamming code.
%       'linear'    Linear block code.
%       'cyclic'    Cyclic code.
%       'bch'       Binary BCH code.
%       'rs'        Reed-Solomon code.
%       'convol'    Convolution code.
%
%       The CODE outputs the encoded code word. The format of CODE matched the
%       format of MSG. When MSG is a K-column matrix, the output CODE is a 
%       N-column matrix. 
%
%       [CODE, ADDED] = ENCODE(MSG, N, K, METHOD, OPT) outputs the number of
%       columns added to MSG in order to make the encoding.
%
%       Use ENCODE(METHOD) to view the help for a specific method.
%
%       See also: DECODE, BCHPOLY, BCHDECO, RSPOLY, RSDECO, CYCLPOLY, CYCLGEN.

%       Wes Wang 8/16/94, 10/3/95
%       Copyright (c) 1995-96 by The MathWorks, Inc.
%       $Revision: 1.1 $  $Date: 1996/04/01 17:57:20 $
% routine check
if nargin == 2
    error('Not enough input parameters')
elseif nargin > 3
    method = lower(method);
elseif nargin == 3
    method = 'hamming';
end;

if nargin < 1
    feval('help', 'encode');
    return;
elseif isstr(msg)
    method = lower(deblank(msg));
    if length(method) < 2
        error('Invalid method option for ENCODE.')
    end
    if nargin == 1
        addition = 'See also DECODE, BCHPOLY, HAMMGEN, GEN2PAR, SIM2GEN, CYCLPOLY.';
        callhelp('encode.hlp',method(1:2),addition);
    else
        disp('Warning: Worng number of input variable for ENCODE.')
    end;
    return;
elseif ~isempty(findstr(method, 'rs'))
    % Reed-Solomon method.
    if ~isempty(findstr(method, 'power'))
        type_flag = 'power';
    elseif ~isempty(findstr(method, 'decimal'))
        type_flag = 'decimal';
    else
        type_flag = 'binary';
    end;
    if nargin <= 4
        [code, added] = rsenco(msg, n, k, type_flag);
    else
        [code, added] = rsenco(msg, n, k, type_flag, opt);
    end;
else
    % make msg to be a column vector when it is a vector.
    if min(size(msg)) == 1
        msg = msg(:);
    end;

    added = 0;
    [n_msg, m_msg] = size(msg);

    if ~isempty(findstr(method, 'decimal'))
        type_flag = 1;      % decimal
        if m_msg > 1
            method = method(1:find(method=='/'));
            error(['The MSG format for ',method,' code with decimal style can only be a vector'])
        else
            if ~isempty([find(msg > 2^k-1); find(msg < 0); find(floor(msg)~=msg)])
                error('MSG contains invalid elements')
            end;
        end;
        msg = de2bi(msg, k);
        [n_msg, m_msg] = size(msg);
    else
        type_flag = 0;      % binary matrix
        if ~isempty([find(msg > 1); find(msg < 0); find(floor(msg)~=msg)])
            error('MSG contains invalid elements')
        end;
        if m_msg == 1
            type_flag = 2;  % binary vector
            [msg, added] = vec2mat(msg, k);
            [n_msg, m_msg] = size(msg);
        elseif m_msg ~= k
            error('Matrix MSG must have column length same as variable K in ENCODE');
        end;
    end;
    % at this stage MSG is a K-colomn matrix
    if ~isempty(findstr(method, 'bch'))
        % BCH code.
        if nargin <= 4
            code = bchenco(msg, n, k);
        else
            code = bchenco(msg, n, k, opt);
        end;
    elseif ~isempty(findstr(method, 'hamming'))
        % hamming code.
        m = n - k;
        if 2^m - 1 ~= n
            error('The specified code-word length and message length are not valid.')
        end;
        if nargin <= 4
            h = hammgen(m);
        else
            h = hammgen(m, opt);
        end;
        gen = gen2par(h);
        code = rem(msg * gen, 2);
    elseif ~isempty(findstr(method, 'linear'))
        % block code.
        if nargin < 5
            error('The generator matrix must be specified for linear block code.');
        end;
        [n_opt, m_opt] = size(opt);
        if (m_opt ~= n) | (n_opt ~= k)
            error('The generator matrix dimension is not valid');
        end;
        code = rem(msg * opt, 2);
    elseif ~isempty(findstr(method, 'cyclic'))
        % cyclic code.
        if nargin < 4
            error('Not enough input parameters.')
        elseif nargin < 5
            opt = cyclpoly(n, k);
        end;
        [h, gen] = cyclgen(n, opt);
        code = rem(msg * gen, 2);
    elseif ~isempty(findstr(method, 'convol'))
        code = convenco(msg, opt);
    else
      error(['Unknown encode method ''',method,'''']);
    end;

    % convert back to the original strucutre.
    if type_flag == 1
        code = bi2de(code);
    elseif type_flag == 2
        code = code';
        code = code(:);
    end;
end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -