# encode

## Syntax

``code = encode(msg,n,k)``
``code = encode(msg,n,k,codingMethod,prim_poly)``
``code = encode(msg,n,k,codingMethod,genmat)``
``code = encode(msg,n,k,codingMethod,genpoly)``
``[code,added] = encode(___)``

## Description

example

````code = encode(msg,n,k)` encodes message, `msg`, using the Hamming encoding method with codeword length, `n`, and message length, `k`. The value of `n` must be calculated for an integer, m, such that m ≥ 2. The values of `n` and `k` are calculated as 2m–1 and `n`–m, respectively.```

example

````code = encode(msg,n,k,codingMethod,prim_poly)` encodes `msg` using `codingMethod` as the Hamming encoding method, and `prim_poly` as the primitive polynomial. The value of `n` must be calculated for an integer, `m`≥2.```

example

````code = encode(msg,n,k,codingMethod,genmat)` encodes `msg` using `codingMethod` as the linear block encoding method and `genmat` as the generator matrix. The value of `n` must be calculated for an integer, `m`≥2.```

example

````code = encode(msg,n,k,codingMethod,genpoly)` encodes `msg` using `codingMethod` as the systematic cyclic code and `genpoly`, as the generator polynomial. The value of `n` must be calculated for an integer, `m`≥2.```
````[code,added] = encode(___)` returns the additional variable `added`. `added` denotes the number of zeros appended at the end of the message matrix before encoding. You can specify any of the input argument combinations from the previous syntaxes.```

## Examples

collapse all

Set the values of the codeword length and message length.

```n = 15; % Codeword length k = 11; % Message length```

Create a random binary message with length equal to the message length.

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

Encode the message.

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

Corrupt the encoded message sequence by introducing an error in the fourth bit.

`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 ```

Set the values of codeword length and message length.

```n = 7; % Codeword length k = 3; % Message length```

Create a random binary message with length equal to the message length.

`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);`

Corrupt the encoded message sequence by introducing an error in the third bit.

`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);`
```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 = biterr(data,decData)`
```numerr = 0 ```

Set the values of the codeword length and message length.

```n = 15; % Codeword length k = 5; % Message length```

Create a random binary message with length equal to the message length.

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

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

```genpoly = cyclpoly(n,k); parmat = cyclgen(n,genpoly);```

Create a syndrome decoding table 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',genpoly);`

Corrupt the encoded message sequence by introducing errors in the first, second, fourth and seventh bits.

```encData(1) = ~encData(1); encData(2) = ~encData(2); encData(4) = ~encData(4);```

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

```decData = decode(encData,n,k,'cyclic/binary',genpoly,trt); numerr = biterr(data,decData)```
```numerr = 0 ```

## Input Arguments

collapse all

Input messages, specified as one of these options:

• Binary column or row vector with `k` columns

• Binary matrix with `k` columns

• Column or row vector of `k` columns and having integers in the range [0, 2k–1]

Example: `msg = [0 1 1 0, 0 1 0 1, 1 0 0 1]` specifies a binary row vector for `k`=4.

Example: `msg = [0 1 1 0; 0 1 0 1; 1 0 0 1]` specifies a binary matrix for `k`=4.

Example: `msg = [6, 10, 9]` specifies a row vector of integers for `k`=4.

Data Types: `double`

Codeword length, specified as a positive integer. The function calculates this value as `2^m–1`, where m must be greater than or equal to 2.

Data Types: `double`

Message length, specified as a positive integer. The function calculates this value as `n`m, where m must be greater than or equal to 2.

Data Types: `double`

Error coding method and format, specified as one of these:

• `'hamming/binary'`

• `'hamming/decimal'`

• `'linear/binary'`

• `'linear/decimal'`

• `'cyclic/binary'`

• `'cyclic/decimal'`

Data Types: `char` | `string`

Primitive polynomial, specified as one of these options:

• Binary row vector — This vector gives coefficients of `prim_poly` in the order of ascending powers.

• Character vector or a string scalar — This value defines `prim_poly` in textual representation. For more information, see polynomial character vector.

• Positive integer — This value defines `prim_poly` in the range [2m + 1, 2m + 1 – 1].

Data Types: `double` | `char` | `string`

Generator matrix, specified as a `k`-by-`n` numeric matrix.

Data Types: `double`

Generator polynomial, specified as a polynomial character vector or a row vector that gives the coefficients in order of ascending powers of the binary generator polynomial. The value of `genpoly` for an [`n`, `k`] cyclic code must have degree `n``k` and divide xn-1, where x is an identifier.

Data Types: `char` | `string`

## Output Arguments

collapse all

Output code, returned as one of the options in this table. The value and dimension of `code` depends on the value and dimension of the msg and the input message format according to this table:

`msg` ValueInput Message Format`code` Value

Binary column or row vector

binary

Binary column or row vector

Binary matrix with `k` columns

binary

Binary matrix with `n` columns

Column or row vector of integers in the range [0, 2k–1]

decimal

Column or row vector of integers in the range [0, 2n–1]

Additional variable, returned as the number of zeros that were appended at the end of the message matrix before encoding for the matrix to have the appropriate size. The size of the message matrix depends on the `n`, `k`, and `msg` and the encoding method.

## Algorithms

Depending on the error-correction coding method, the `encode` function relies on lower-level functions such as `hammgen` and `cyclgen`.

## Version History

Introduced before R2006a