# tpcenc

Turbo product code (TPC) encoder

## Description

performs 2-D TPC encoding of the input message, `code`

= tpcenc(`msg`

,`N`

,`K`

)`msg`

, using two
linear block codes specified by codeword length `N`

and message
length `K`

. For a description of 2-D TPC encoding, see Turbo Product Code Construction.

## Examples

### Encode Using Full-Length TPC Codes

Encode a random bit vector using 2-D turbo product coding (TPC) with extended Hamming codes and extended BCH codes.

Specify (N,K) code pairs for TPC encoding.

N = [32;64]; K = [21;57];

Generate a column vector of random message bits. The desired length for the message bits is the product of elements in `K`

.

msg = randi([0 1],prod(K),1);

TPC-encode the message.

code = tpcenc(msg,N,K);

Verify that the length of the encoded codeword is the product of elements in `N`

.

size(code)

`ans = `*1×2*
2048 1

prod(N)

ans = 2048

### Encode Shortened Message Using Turbo Product Coding

Encode a random bit vector using 2-D turbo product coding (TPC), applying message shortening.

Specify (N,K) code pairs and S for TPC encoding.

N = [32;64]; K = [21;57]; S = [19;24];

Generate a column vector of random message bits. The desired length for the shortened message bits is the product of the elements in `S`

.

msg = randi([0 1],prod(S),1);

TPC-encode the shortened message.

code = tpcenc(msg,N,K,S);

Verify that the length of the encoded codeword is the product of elements in (`N-K+S`

).

size(code)

`ans = `*1×2*
930 1

prod(N-K+S)

ans = 930

## Input Arguments

`msg`

— Input message bits to encode

column vector

Input message bits to encode, specified as a column vector.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `uint8`

| `uint16`

| `uint32`

| `logical`

`N`

— Codeword length

two-element integer vector

Codeword length, specified as a two-element integer vector,
[*N*_{R};
*N*_{C}].
*N*_{R} represents the number of
rows in the product code matrix.
*N*_{C} represents the number of
columns in the product code matrix. For more information about
*N*_{R} and
*N*_{C}, see Turbo Product Code Construction. For a
list of valid
(*N*(*i*),*K*(*i*))
code pairs, see Component Codes.

**Data Types: **`double`

`K`

— Message length

two-element integer vector

Message length, specified as a two-element integer vector,
[*K*_{R};
*K*_{C}]. For a full-length
message, the input column vector containing the message bits to encode is
arranged into a
*K*_{R}-by-*K*_{C}
matrix. *K*_{R} represents the number
of rows in the message matrix. *K*_{C}
represents the number of columns in the message matrix. For more information
about *K*_{R} and
*K*_{C}, see Turbo Product Code Construction. For a
list of valid
(*N*(*i*),*K*(*i*))
code pairs, see Component Codes.

**Data Types: **`double`

`S`

— Shortened message length

two-element integer vector

Shortened message length, specified as a two-element integer vector,
[*S*_{R};
*S*_{C}]. For a shortened message,
the input column vector containing the message bits to encode is arranged
into an
*S*_{R}-by-*S*_{C}
matrix. *S*_{R} represents the number
of rows in the matrix. *S*_{C}
represents the number of columns in the matrix. For more information about
*S*_{R} and
*S*_{C}, see Turbo Product Code Construction.

When you specify this parameter, specify *N* and
*K* vectors for the full-length TPC codes that are
shortened to
(*N*(*i*)–*K*(*i*)+*S*(*i*),
*S*(*i*)) codes.

**Data Types: **`double`

## Output Arguments

`code`

— TPC-encoded message

column vector

TPC-encoded message, returned as a column vector with the same data type as the input message bits.

## More About

### Component Codes

This table lists the supported component code pairs for the row
(*N*_{R},*K*_{R})
and column
(*N*_{C},*K*_{C})
parameters.

*N*_{R}and*K*_{R}represent the number of rows in the product code matrix and message matrix, respectively.*N*_{C}and*K*_{C}represent the number of columns in the product code matrix and message matrix, respectively.

Within each code type, any two component code pairs can form a 2-D TPC code. The table also includes the error-correction capability for each code pair.

Code type | Component Code
Pairs(N_{R},K_{R})
and
(N_{C},K_{C}) | Error-Correction Capability
(T) |

Hamming code | (255,247) | 1 |

(127,120) | 1 | |

(63,57) | 1 | |

(31,26) | 1 | |

(15,11) | 1 | |

(7,4) | 1 | |

Extended Hamming code | (256,247) | 1 |

(128,120) | 1 | |

(64,57) | 1 | |

(32,26) | 1 | |

(16,11) | 1 | |

(8,4) | 1 | |

BCH code | (255,239) | 2 |

(127,113) | 2 | |

(63,51) | 2 | |

(31,21) | 2 | |

(15,7) | 2 | |

Extended BCH code | (256,239) | 2 |

(128,113) | 2 | |

(64,51) | 2 | |

(32,21) | 2 | |

(16,7) | 2 | |

Parity check code | (256,255) | - |

(128,127) | - | |

(64,63) | - | |

(32,31) | - | |

(16,15) | - | |

(8,7) | - | |

(4,3) | - |

### Turbo Product Code Construction

Turbo product codes (TPC) are a form of concatenated codes used as forward error-correcting (FEC) codes. Two or more component block codes, such as systematic linear block codes, are used to construct TPCs. This encoder implements 2-D product code encoding, as described in [1], using two Linear Block Codes.

The TPC encoder accepts either full-length or shortened messages.

Construction of Full-Length Message Product Codes

Full-length input messages are encoded using specified 2-D TPC code pairs. Row-wise
encoding uses the
(*N*_{C},*K*_{C})
code pair and column-wise encoding uses the
(*N*_{R},*K*_{R})
code pair. The input vector length must be *K*_{R}
· *K*_{C}. The input message bits vector is
arranged into a
*K*_{R}-by-*K*_{C}
matrix.

Row-wise encoding uses an
(*N*_{C},*K*_{C})
systematic linear block encoder with *K*_{C} bits
per row. The row-wise encoding results in a
*K*_{R}-by-*N*_{C}
matrix that includes parity bits added to each row.

Next, column-wise encoding uses an
(*N*_{R},*K*_{R})
systematic linear block encoder on each of the
*N*_{C} columns. Applying this 2-D TPC encoding
to the initial
*K*_{R}-by-*K*_{C}
matrix results in an
*N*_{R}-by-*N*_{C}
matrix that includes parity bits added to each row and column.

The 2-D TPC full-code matrix is reshaped into a column vector of length
*N*_{R} ·
*N*_{C} and returned as the TPC-encoded
output.

Construction of Shortened Message Product Codes

Shortened input messages are encoded using specified 2-D TPC code pairs. Row-wise
encoding uses the
(*N*_{C},*K*_{C})
code pair and column-wise encoding uses an
(*N*_{R},*K*_{R})
code pair. The input vector length must be *S*_{R}
· *S*_{C}. The input shortened message bits vector
is arranged into an
*S*_{R}-by-*S*_{C}
matrix. The shortened message matrix prepends two dimensions by padding the beginning of
the message matrix with zeros. The resulting matrix is a
*K*_{R}-by-*K*_{C}
matrix.

Row-wise encoding uses an
(*N*_{C},*K*_{C})
systematic linear block encoder with *K*_{C} bits
per row. The row-wise encoding results in a
*K*_{R}-by-*N*_{C}
matrix that includes parity bits added to each row.

Next, the column-wise encoding uses an
(*N*_{R},*K*_{R})
systematic linear block encoder on each of the
*N*_{C} columns.

Applying this 2-D TPC encoding to the initial
*K*_{R}-by-*K*_{C}
matrix and excluding the zero-padded bits from the output results in an
(*N*_{R}–*K*_{R}+*S*_{R})-by-(*N*_{C}–*K*_{C}+*S*_{C})
matrix. This matrix includes parity bits added to each row and column.

The 2-D TPC shortened-code matrix is reshaped into a column vector of length
(*N*_{R}–*K*_{R}+*S*_{R})
·
(*N*_{C}–*K*_{C}+*S*_{C})
and returned as the TPC-encoded output.

## References

[1] Pyndiah, R. M. "Near-Optimum
Decoding of Product Codes: Block Turbo Codes." *IEEE Transactions on
Communications*. Volume 46, Number 8, August 1998, pp.
1003–1010.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

TPC parameters

`N`

,`K`

, and`S`

must be constant values. If the value used for each of these parameters does not change, then you can assign them by expression or variable.

## Version History

**Introduced in R2018a**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)