| Communications Toolbox | ![]() |
The cyclic, Hamming, and generic linear block code functionality in this toolbox offers you multiple ways to organize bits in messages or codewords. These topics explain the available formats:
To learn how to represent words for BCH or Reed-Solomon codes, see Representing Words for BCH Codes or Representing Words for Reed-Solomon Codes.
Your messages and codewords can take the form of vectors containing 0s and 1s. For example, messages and codes might look like msg and code in the lines below.
n = 6; k = 4; % Set codeword length and message length % for a [6,4] code. msg = [1 0 0 1 1 0 1 0 1 0 1 1]'; % Message is a binary column. code = encode(msg,n,k,'cyclic'); % Code will be a binary column. msg' code'
The output is below.
ans =
Columns 1 through 5
1 0 0 1 1
Columns 6 through 10
0 1 0 1 0
Columns 11 through 12
1 1
ans =
Columns 1 through 5
1 1 1 0 0
Columns 6 through 10
1 0 0 1 0
Columns 11 through 15
1 0 0 1 1
Columns 16 through 18
0 1 1 In this example, msg consists of 12 entries, which are interpreted as three 4-digit (because k = 4) messages. The resulting vector code comprises three 6-digit (because n = 6) codewords, which are concatenated to form a vector of length 18. The parity bits are at the beginning of each codeword.
You can organize coding information so as to emphasize the grouping of digits into messages and codewords. If you use this approach, then each message or codeword occupies a row in a binary matrix. The example below illustrates this approach by listing each 4-bit message on a distinct row in msg and each 6-bit codeword on a distinct row in code.
n = 6; k = 4; % Set codeword length and message length. msg = [1 0 0 1; 1 0 1 0; 1 0 1 1]; % Message is a binary matrix. code = encode(msg,n,k,'cyclic'); % Code will be a binary matrix. msg code
The output is below.
msg =
1 0 0 1
1 0 1 0
1 0 1 1
code =
1 1 1 0 0 1
0 0 1 0 1 0
0 1 1 0 1 1
Note In the binary matrix format, the message matrix must have k columns. The corresponding code matrix has n columns. The parity bits are at the beginning of each row. |
Your messages and codewords can take the form of vectors containing integers. Each element of the vector gives the decimal representation of the bits in one message or one codeword.
Note If 2^n or 2^k is very large, then you should use the default binary format instead of the decimal format. This is because the function uses a binary format internally, while the roundoff error associated with converting many bits to large decimal numbers and back might be substantial. |
Note When you use the decimal vector format, encode expects the leftmost bit to be the least significant bit. |
The syntax for the encode command must mention the decimal format explicitly, as in the example below. Notice that /decimal is appended to the fourth argument in the encode command.
n = 6; k = 4; % Set codeword length and message length. msg = [9;5;13]; % Message is a decimal column vector. % Code will be a decimal vector. code = encode(msg,n,k,'cyclic/decimal')
The output is below.
code =
39
20
54
Note The three examples above used cyclic coding. The formats for messages and codes are similar for Hamming and generic linear block codes. |
| Creating and Decoding BCH Codes | Parameters for Linear Block Codes | ![]() |
© 1994-2005 The MathWorks, Inc.