Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

encode

Syntax

code = encode(msg,n,k,'linear/fmt',genmat)
code = encode(msg,n,k,'cyclic/fmt',genpoly)
code = encode(msg,n,k,'hamming/fmt',prim_poly)
code = encode(msg,n,k)
[code,added] = encode(...)

Optional Inputs

InputDefault Value
fmtbinary
genpoly cyclpoly(n,k)
prim_poly gfprimdf(n-k)

Description

For All Syntaxes

The encode function encodes messages using one of the following error-correction coding methods:

  • Linear block

  • Cyclic

  • Hamming

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. The table Information Formats below shows which formats are allowed for msg, how the argument fmt should reflect the format of msg, and how the format of the output code depends on these choices. The examples in the table are for k = 4. If fmt is not specified as input, its default value is binary.

    Note:   If 2^n or 2^k is large, 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.

Information Formats

Dimension of msgValue of fmt ArgumentDimension of code
Binary column or row vector binaryBinary column or row vector
Example: msg = [0 1 1 0, 0 1 0 1, 1 0 0 1].'
Binary matrix with k columns binaryBinary matrix with n columns
Example: msg = [0 1 1 0; 0 1 0 1; 1 0 0 1]
Column or row vector of integers in the range [0, 2^k-1] decimalColumn or row vector of integers in the range [0, 2^n-1]
Example: msg = [6, 10, 9].'

For Specific Syntaxes

code = encode(msg,n,k,'linear/fmt',genmat) encodes 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,'cyclic/fmt',genpoly) encodes msg and creates a systematic cyclic code. genpoly is a polynomial character vector or 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,'hamming/fmt',prim_poly) encodes 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. prim_poly is a polynomial character vector or 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 prim_poly is the default primitive polynomial gfprimdf(m).

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

collapse all

Encode and decode corrupted data using three types of linear block codes.

Hamming Code

Set the code parameters.

n = 15;                % Code length
k = 11;                % Message length

Create a binary message having length k.

data = randi([0 1],k,1);

Encode the message.

encData = encode(data,n,k,'hamming/binary');

Introduce an error in the 4th bit of the encoded sequence.

encData(4) = ~encData(4);

Decode the corrupted sequence. Observe that the decoder has correctly recovered the message.

decData = decode(encData,n,k,'hamming/binary');

numerr = biterr(data,decData)
numerr =

     0

Linear Block Code

Set the code parameters.

n = 7;                % Code length
k = 3;                % Message length

Create a binary message having length k.

data = randi([0 1],k,1);

Create a cyclic generator polynomial. Then, create a parity-check matrix and convert it into a generator matrix.

pol = cyclpoly(n,k);
parmat = cyclgen(n,pol);
genmat = gen2par(parmat);

Encode the message sequence by using the generator matrix.

encData = encode(data,n,k,'linear/binary',genmat);

Introduce an error in the 3rd bit of the encoded sequence.

encData(3) = ~encData(3);

Decode the corrupted sequence. Observe that the decoder has correctly recovered the message.

decData = decode(encData,n,k,'linear/binary',genmat);

numerr = biterr(data,decData)
Single-error patterns loaded in decoding table.  8 rows remaining.
2-error patterns loaded.  1 rows remaining.
3-error patterns loaded.  0 rows remaining.

numerr =

     0

Cyclic Code

Set the code parameters.

n = 15;                % Code length
k = 5;                 % Message length

Create a binary message having length k.

data = randi([0 1],k,1);

Create a generator polynomial for a cyclic code. Create a parity-check matrix by using the generator polynomial.

gpol = cyclpoly(n,k);
parmat = cyclgen(n,gpol);

Create a syndrome decoding tabled by using the parity-check matrix.

trt = syndtable(parmat);
Single-error patterns loaded in decoding table.  1008 rows remaining.
2-error patterns loaded.  918 rows remaining.
3-error patterns loaded.  648 rows remaining.
4-error patterns loaded.  243 rows remaining.
5-error patterns loaded.  0 rows remaining.

Encode the data by using the generator polynomial.

encData = encode(data,n,k,'cyclic/binary',gpol);

Introduce errors in the 4th and 7th bits of the encoded sequence.

encData(4) = ~encData(4);
encData(7) = ~encData(7);

Decode the corrupted sequence. Observe that the decoder has correctly recovered the message.

decData = decode(encData,n,k,'cyclic/binary',gpol,trt);

numerr = biterr(data,decData)
numerr =

     0

More About

collapse all

Algorithms

Depending on the encoding method, encode relies on such lower-level functions as hammgen and cyclgen.

Introduced before R2006a

Was this topic helpful?