Communications Toolbox | ![]() ![]() |
Creating and Decoding Binary Block Codes
The functions for encoding and decoding linear block codes are encode
, decode
, bchenco
, and bchdeco
. The first two in this list are general-purpose functions that invoke other functions from the list when appropriate. This section discusses how to use these functions to create and decode generic linear block codes, cyclic codes, BCH codes, and Hamming codes.
Generic Linear Block Codes
Encoding a message using a generic linear block code requires a generator matrix. If you have defined variables msg
, n
, k
, and genmat
, then either of the commands
encodes the information in msg
using the [n
,k
] code that the generator matrix genmat
determines. The /decimal
option, suitable when 2^n
and 2^k
are not very large, indicates that msg
contains nonnegative decimal integers rather than their binary representations. See Representing Words for Binary Block Codes or the reference page for encode
for a description of the formats of msg
and code
.
Decoding the code requires the generator matrix and possibly a decoding table. If you have defined variables code
, n
, k
, genmat
, and possibly also trt
, then the commands
newmsg = decode(code,n,k,'linear'
,genmat); newmsg = decode(code,n,k,'linear/decimal'
,genmat); newmsg = decode(code,n,k,'linear'
,genmat,trt); newmsg = decode(code,n,k,'linear/decimal'
,genmat,trt);
decode the information in code
, using the [n
,k
] code that the generator matrix genmat
determines. decode
also corrects errors according to instructions in the decoding table that trt
represents.
Example: Generic Linear Block Coding. The example below encodes a message, artificially adds some noise, decodes the noisy code, and keeps track of errors that the decoder detects along the way. Because the decoding table contains only zeros, the decoder does not correct any errors.
n = 4; k = 2; genmat = [[1 1; 1 0], eye(2)]; % Generator matrix msg = [0 1; 0 0; 1 0]; % Three messages, two bits each % Create three codewords, four bits each. code = encode(msg,n,k,'linear',genmat); noisycode = rem(code + randerr(3,4,[0 1;.7 .3]),2); % Add noise. trt = zeros(2^(n-k),n); % No correction of errors % Decode, keeping track of all detected errors. [newmsg,err] = decode(noisycode,n,k,'linear',genmat,trt); err_words = find(err~=0) % Find out which words had errors.
The output indicates that errors occurred in the first and second words. Your results might vary because this example uses random numbers as errors.
Cyclic Codes
Encoding a message using a cyclic code requires a generator polynomial. If you have defined variables msg
, n
, k
, and genpoly
, then either of the commands
encodes the information in msg
using the [n
,k
] code determined by the generator polynomial genpoly
. genpoly
is an optional argument for encode
. The default generator polynomial is cyclpoly
(n,k)
. The /decimal
option, suitable when 2^n
and 2^k
are not very large, indicates that msg
contains nonnegative decimal integers rather than their binary representations. See Representing Words for Binary Block Codes or the reference page for encode
for a description of the formats of msg
and code
.
Decoding the code requires the generator polynomial and possibly a decoding table. If you have defined variables code
, n
, k
, genpoly
, and trt
, then the commands
newmsg = decode(code,n,k,'cyclic'
,genpoly); newmsg = decode(code,n,k,'cyclic/decimal'
,genpoly); newmsg = decode(code,n,k,'cyclic'
,genpoly,trt); newmsg = decode(code,n,k,'cyclic/decimal'
,genpoly,trt);
decode the information in code
, using the [n
,k
] code that the generator matrix genmat
determines. decode
also corrects errors according to instructions in the decoding table that trt
represents. genpoly
is an optional argument in the first two syntaxes above. The default generator polynomial is cyclpoly
(n,k)
.
Example. You can modify the example in the section Generic Linear Block Codes so that it uses the cyclic coding technique, instead of the linear block code with the generator matrix genmat
. Make the changes listed below:
encode
and decode
commands), replace genmat
by genpoly
and replace 'linear'
by 'cyclic'
.
Another example of encoding and decoding a cyclic code is on the reference page for encode
.
BCH Codes
BCH codes are a special case of cyclic codes, though the decoding algorithm for BCH codes is more complicated than that for generic cyclic codes. The discussion in the section Cyclic Codes above applies almost exactly to the case of BCH codes. The only differences are
bch
replaces cyclic
in the syntax for encode
and decode
.
bchpoly
(n,k)
replaces cyclpoly(n,k)
as the default generator polynomial.
n
and k
must be valid codeword and message lengths for BCH code.
Valid codeword lengths for BCH code are those integers of the form 2m-1 for some integer m greater than or equal to 3. Given a valid BCH codeword length, the corresponding valid BCH message lengths are those numbers in the second column of the output of the command below.
For example, the output of the command below shows that a BCH code with codeword length 15 can have message length 5, 7, or 11. No other message lengths are valid for this codeword length.
The third column of the output above represents the error-correction capability for each pair of codeword length and message length.
Choice of Functions for BCH Coding. To process BCH codes, you can use either the encode
and decode
functions, or the lower level bchenco
and bchdeco
functions. The syntax of the lower level functions is slightly different from that of the higher level functions. The only difference in functionality is that the higher level functions prepare the input data (including default values of options that you omit) before invoking the lower level functions. The reference page for encode
contains an example that uses encode
and decode
. The reference pages for bchenco
and bchdeco
contain other examples.
Hamming Codes
The reference pages for encode
and decode
contain examples of encoding and decoding Hamming codes. Also, the section Decoding Table illustrates error correction in a Hamming code.
![]() | Parameters for Binary Block Codes | Performing Other Binary Block Code Tasks | ![]() |