Communications Toolbox | ![]() ![]() |
Syntax
code = encode(msg,n,k,'linear/
format
',genmat); code = encode(msg,n,k,'cyclic/
format
',genpoly); code = encode(msg,n,k,'bch/
format
',genpoly); code = encode(msg,n,k,'hamming/
format
',primpoly); code = encode(msg,n,k,'rs/
format
',genpoly); code = encode(msg,field,k,'
rs/
format
'
,genpoly); code = encode(msg,n,k); [code,added] = encode(...);
Optional Inputs
Input |
Default Value |
|
binary |
genpoly |
cyclpoly (n,k) for cyclic codes;bchpoly (n,k) for BCH codes;rspoly (n,k) or rspoly(n,k,field) for Reed-Solomon codes |
primpoly |
gfprimdf (n-k) |
For All Syntaxes
The encode
function encodes messages using one of the following error-correction coding methods:
For all of these methods, the codeword length is n
and the message length is k
.
msg
, which represents the messages, can have one of several formats. Table 3-14, Information Formats for Encoding Methods Other than Reed-Solomon, below, which applies to all coding methods supported by encode
except the Reed-Solomon method, shows which formats are allowed for msg
, how the argument format
should reflect the format of msg
, and how the format of the output code
depends on these choices. Table 3-15, Information Formats for the Reed-Solomon Encoding Method, gives the corresponding information for the Reed-Solomon method. The examples in the tables are for k = 4
and, in Table 3-15, Information Formats for the Reed-Solomon Encoding Method, m = 3. If format
is not specified as input, then its default value is binary
.
For Specific Syntaxes
code = encode(msg,n,k,'
encodes linear/
format
',genmat)
msg
using genmat
as the generator matrix for the linear block encoding method. genmat
, a k
-by-n
matrix, is required as input.
code = encode(msg,n,k,'
encodes cyclic/
format
',genpoly)
msg
and creates a systematic cyclic encode. genpoly
is a row vector that gives the coefficients, in order of ascending powers, of the binary generator polynomial. The default value of genpoly
is cyclpoly
(n,k)
. By definition, the generator polynomial for an [n,k] cyclic code must have degree n-k and must divide xn-1.
code = encode(msg,n,k,'
encodes bch/
format
',genpoly)
msg
using the BCH encoding method. genpoly
is a row vector that gives the coefficients, in order of ascending powers, of the degree-(n-k
) binary BCH generator polynomial. The default value of genpoly
is bchpoly
(n,k)
. For this syntax, n
must have the form 2m-1 for some integer m greater than or equal to 3. k
must be a valid message length as reported in the second column of params
in the command
params = bchpoly(n)
code = encode(msg,n,k,'
encodes hamming/
format
',primpoly)
msg
using the Hamming encoding method. For this syntax, n
must have the form 2m-1 for some integer m greater than or equal to 3, and k
must equal n
-m. primpoly
is a row vector that gives the binary coefficients, in order of ascending powers, of the primitive polynomial for GF(2m) that is used in the encoding process. The default value of primpoly
is the default primitive polynomial gfprimdf
(
m)
.
code = encode(msg,n,k,'
encodes rs/
format
',genpoly)
msg
using the Reed-Solomon encoding method. n
must have the form 2m-1 for some integer m greater than or equal to 3. genpoly
is a row vector that gives the coefficients, in order of ascending powers, of the generator polynomial for the code. Each coefficient is an element of GF(2m) expressed in exponential format. For a description of exponential format, see Exponential Format. The default value of genpoly
is the output of the function rspoly
.
code = encode(msg,field,k,'
is the same as the syntax above, except that rs/
format
',genpoly)
field
is a matrix that lists all elements of GF(2m) in the format described in List of All Elements of a Galois Field. The size of field
determines n
. This syntax is faster than the one above.
code = encode(msg,n,k)
is the same as code = encode(msg,n,k,'
hamming/binary
')
.
[code,added] = encode(...)
returns the additional variable added
. added
is the number of zeros that were placed at the end of the message matrix before encoding, in order for the matrix to have the appropriate shape. "Appropriate" depends on n
, k
, the shape of msg
, and the encoding method.
Examples
The example below illustrates the three different information formats (binary vector, binary matrix, and decimal vector) for Hamming code. The three messages have identical content in different formats; as a result, the three codes that encode
creates have identical content in correspondingly different formats.
m = 4; n = 2^m-1; % Codeword length = 15 k = 11; % Message length % Create 100 messages, k bits each. msg1 = randint(100*k,1,[0,1]); % As a column vector msg2 = vec2mat(msg1,k); % As a k-column matrix msg3 = bi2de(msg2); % As a column of decimal integers % Create 100 codewords, n bits each. code1 = encode(msg1,n,k,'hamming/binary'); code2 = encode(msg2,n,k,'hamming/binary'); code3 = encode(msg3,n,k,'hamming/decimal'); if ( vec2mat(code1,n)==code2 & de2bi(code3,n)==code2 ) disp('All three formats produced the same content.') end
The next example creates a cyclic code, adds noise, and then decodes the noisy code. It uses the decode
function. Your error rate results might vary because the noise is random.
n = 3; k = 2; % A (3,2) cyclic code msg = randint(100,k,[0,1]); % 100 messages, k bits each code = encode(msg,n,k,'cyclic/binary'); % Add noise. noisycode = rem(code + randerr(100,n,[0 1;.7 .3]), 2); newmsg = decode(noisycode,n,k,'cyclic'); % Try to decode. % Compute error rate for decoding the noisy code. [number,ratio] = biterr(newmsg,msg); disp(['The bit error rate is ',num2str(ratio)]) The bit error rate is 0.08
The next example encodes the same message using Hamming, BCH, and cyclic methods. Before creating BCH code, it uses the bchpoly
command to find out what codeword and message lengths are valid. This example also creates Hamming code with the '
linear
'
option of the encode
command. It then decodes each code and recovers the original message.
n = 6; % Try codeword length = 6. % Find any valid message length for BCH code. params = bchpoly(n); n = params(1,1); % Redefine codeword length in case earlier one % was invalid. k = params(1,2); % Message length m = log2(n+1); % Express n as 2^m-1. msg = randint(100,1,[0,2^k-1]); % Column of decimal integers % Create various codes. codehamming = encode(msg,n,k,'hamming/decimal'); [parmat,genmat] = hammgen(m); codehamming2 = encode(msg,n,k,'linear/decimal',genmat); if codehamming==codehamming2 disp('The ''linear'' method can create Hamming code.') end codebch = encode(msg,n,k,'bch/decimal'); codecyclic = encode(msg,n,k,'cyclic/decimal'); % Decode to recover the original message. decodedhamming = decode(codehamming,n,k,'hamming/decimal'); decodedbch = decode(codebch,n,k,'bch/decimal'); decodedcyclic = decode(codecyclic,n,k,'cyclic/decimal'); if (decodedhamming==msg & decodedbch==msg & decodedcyclic==msg) disp('All decoding worked flawlessly in this noiseless world.') end
Algorithm
Depending on the encoding method, encode
relies on such lower-level functions as hammgen
, cyclgen
, bchenco
, and rsenco
.
See Also
decode
, hammgen
, cyclpoly
, cyclgen
, bchpoly
, bchenco
, rspoly
, rsenco
, rsencode
, convenc
![]() | dpcmopt | eyediagram | ![]() |