# decode

## Syntax

``msg = decode(code,n,k)``
``msg = decode(code,n,k,codingMethod,prim_poly)``
``msg = decode(code,n,k,codingMethod,genmat)``
``msg = decode(code,n,k,codingMethod,genmat,trt)``
``msg = decode(code,n,k,codingMethod,genpoly)``
``msg = decode(code,n,k,codingMethod,genpoly,trt)``
``[msg,err,ccode,cerr] = decode(___)``

## Description

example

````msg = decode(code,n,k)` decodes the coded message `code`. The function assumes that the Hamming encoding method was used, with codeword length `n` and message length `k`.```

example

````msg = decode(code,n,k,codingMethod,prim_poly)` decodes `code` assuming that `prim_poly` is the primitive polynomial used for encoding. This syntax applies when `codingMethod` is `'hamming/binary'` or `'hamming/decimal'`.```

example

````msg = decode(code,n,k,codingMethod,genmat)` decodes `code` assuming that `genmat` is the generator matrix used for encoding. This syntax applies when `codingMethod` is `'linear/binary'` or `'linear/decimal'`.```
````msg = decode(code,n,k,codingMethod,genmat,trt)` additionally specifies the decoding table `trt`. This syntax applies when `codingMethod` is `'linear/binary'` or `'linear/decimal'`.```

example

````msg = decode(code,n,k,codingMethod,genpoly)` decodes `code` assuming that `genpoly` is the generator polynomial used for encoding. This syntax applies when `codingMethod` is `'cyclic/binary'` or `'cyclic/decimal'`.```
````msg = decode(code,n,k,codingMethod,genpoly,trt)` additionally specifies the decoding table `trt`. This syntax applies when `codingMethod` is `'cyclic/binary'` or `'cyclic/decimal'`.```
````[msg,err,ccode,cerr] = decode(___)` additionally returns the message word errors `err`, the corrected codeword `ccode`, and the codeword errors `cerr`, using any input argument combination 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 the 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 by introducing an error at a random location.

```errLoc = randerr(1,n); encData = mod(encData + errLoc',2);```

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 the codeword length and message length.

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

Create a random binary message with the 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 by introducing an error at a random location.

```errLoc = randerr(1,n); encData = mod(encData + errLoc',2);```

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 the 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 by introducing three errors at random locations.

```errLoc = randerr(1,n,3); encData = mod(encData + errLoc',2);```

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 code, specified as one of these options:

• Binary column or row vector

• Binary matrix with `n` columns

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

Data Types: `single` | `double`

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

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.

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

• `'hamming/binary'`

• `'hamming/decimal'`

• `'linear/binary'`

• `'linear/decimal'`

• `'cyclic/binary'`

• `'cyclic/decimal'`

The value you choose for this input determines the syntax you must use. This table summarizes the dependency. The table does not cover the first syntax because you do not specify a value for `codingMethod` when you use it.

Value of `codingMethod`Syntax to Use
`'hamming/binary'` or `'hamming/decimal'`2 or 7
`'linear/binary'` or `'linear/decimal'`3, 4, or 7
`'cyclic/binary'` or `'cyclic/decimal'`5, 6, or 7

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 Representation of Polynomials in Communications Toolbox.

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

For more information, see Default Primitive Polynomials and Primitive Polynomials and Element Representations.

#### Dependencies

This argument applies when you set `CodingMethod` to `'hamming/binary'` or `'hamming/decimal'`.

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

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

#### Dependencies

This argument applies when you set `CodingMethod` to `'linear/binary'` or `'linear/decimal'`.

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 x`n`–1, where x is an identifier.

#### Dependencies

This argument applies when you set `CodingMethod` to `'cyclic/binary'` or `'cyclic/decimal'`.

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

Decoding table, specified as a 2`n-k`-by-`n` binary matrix. Create the decoding table using the `syndtable` function.

#### Dependencies

This argument applies when you set `CodingMethod` to `'linear/binary'`, `'linear/decimal'`, `'cyclic/binary'`, or `'cyclic/decimal'`.

## Output Arguments

collapse all

Decoded messages, returned as one of the options in the table. The value and dimension of `msg` depends on the value and dimension of `code` and the input message format.

`code` ValueInput Message Format`msg` Value

binary column or row vector

binary

binary column or row vector

binary matrix with `n` columns

binary

binary matrix with `k` columns

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

decimal

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

Data Types: `single` | `double`

Message error correction information, returned as a column vector of integers. The length of the vector is equal to the number of message words. The vector entries can take these values:

• `0` — The function did not detect an error in the corresponding message word.

• `1` — The function detected and corrected an error in the corresponding message word.

• `–1` — The decoding algorithm detected more errors in the corresponding message word than could be corrected.

Corrected code, returned as one of these options:

• Binary column or row vector with `n` columns

• Binary matrix with `n` columns

• Column or row vector with `n` entries which are integers in the range [0, 2`n`–1]

The format of `ccode` matches that of `code`.

Codeword error correction information, returned as a column vector of integers. The length of the vector is equal to the number of message words. The meaning of this output depends on the format of `code`. If `code` is not a binary vector, `cerr` is equal to `err`. If `code` is a binary vector, this output contains error detection information for codewords. The information is given in the same way as in `err`.

## Algorithms

Depending on the decoding method, `decode` relies on lower-level functions such as `hammgen`, `syndtable`, and `cyclgen`.

## Version History

Introduced before R2006a