# comm.HDLRSEncoder System object

Package: comm

Encode data using a Reed-Solomon encoder

## Description

The HDL-optimized `HDLRSEncoder` System object™ creates a Reed-Solomon code with message and codeword lengths you specify.

To create a Reed-Solomon code optimized for HDL code generation:

1. Define and set up your HDL RS encoder object. See Construction.

2. Call `step` to recover a message vector from a Reed-Solomon codeword vector according to the properties of `comm.HDLRSEncoder`. The behavior of `step` is specific to each object in the toolbox.

## Construction

`H = comm.HDLRSEncoder` returns a block encoder System object, `H`, that performs Reed-Solomon (RS) encoding in a streaming fashion for HDL.

`H = comm.HDLRSEncoder(Name,Value,)` creates an HDL-optimized block encoder System object, `H`, with additional options specified by one or more `Name,Value` pair arguments, where `Name` is a property name and `Value` is the corresponding value. `Name` must appear inside single quotes (`''`). You can specify several name-value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

`H = comm.HDLRSEncoder(N,K,Name,Value)` creates an RS encoder object, `H`, with the CodewordLength property set to `N`, the MessageLength property set to `K`, and other specified property ```Name, Value``` pair arguments.

## Properties

 `B` B value for polynomial generation `BSource` Source of B, the starting power for roots of the primitive polynomial Specify the source of the B value as one of these values: Auto: B=0Property Default: Auto `CodewordLength` Codeword length Specify the codeword length of the RS code as a double-precision, positive, integer scalar value. The default is `7`. If you set the `PrimitivePolynomialSource` property to `Auto`, `CodewordLength` must be in the range 3 < `CodewordLength` $\le$ 216–1. When you set the `PrimitivePolynomialSource` property to `Property`, `CodewordLength` must be in the range 3 $\le$ `CodewordLength` $\le$ 2M–1. M is the degree of the primitive polynomial that you specify with the `PrimitivePolynomialSource` and `PrimitivePolynomial` properties. M must be in the range 3$\le$ M $\le$ 16. The difference (`CodewordLength` –`MessageLength`) must be an even integer. The value of this property is rounded up to 2M–1. If the value of this property is less than 2M–1, the object assumes a shortened RS code. `MessageLength` Message length Specify the message length as a double-precision, positive integer scalar value. The default is `3`. The difference (`CodewordLength` – `MessageLength`) must be an even integer. `PrimitivePolynomialSource` Source of primitive polynomial Specify the source of the primitive polynomial as `Auto` | `Property`. The default is `Auto`. When you set this property to `Auto`, the object uses a primitive polynomial of degree M = `ceil`(log2(`CodewordLength`+1)), which is the result of `fliplr`(`de2bi`(`primpoly`(M))). When you set this property to `Property`, you can specify a polynomial using the `PrimitivePolynomial` property. `PrimitivePolynomial` Primitive polynomial Specify the primitive polynomial that defines the finite field `GF`(2M) corresponding to the integers that form messages and codewords. You must set this property to a double-precision, binary row vector that represents a primitive polynomial over `GF`(2) of degree M in descending order of powers. This property applies when you set the `PrimitivePolynomialSource` property to `Property`. `PuncturePatternSource` Source of puncture pattern Specify the source of the puncture pattern as `None` | `Property`. The default is `None`. If you set this property to `None` then the object does not apply puncturing to the code. If you set this property to `Property` then the object punctures the code based on a puncture pattern vector specified in the `PuncturePattern` property. `PuncturePattern` Puncture pattern vector Specify the pattern used to puncture the encoded data as a double-precision, binary column vector with a length of (`CodewordLength`–`MessageLength`). The default is `[ones(2,1); zeros(2,1)]`. Zeros in the puncture pattern vector indicate the position of the parity symbols that are punctured or excluded from each codeword. This property applies when you set the `PuncturePatternSource` property to `Property`.

## Methods

 clone Create HDLRSEncoder System object with same property values isLocked Locked status for input attributes and nontunable properties release Allow property value and input characteristics change step Perform Reed-Solomon encoding

## Examples

collapse all

### Reed-Solomon Error Detection Using HDLRSEncoder and HDLRSDecoder

Create an HDLRSEncoder object with RS(255,239) code. This is the code used in the IEEE802.16 Broadband Wireless Access standard.

B is the starting power of the roots of the primitive polynomial.

```hHDLEnc = comm.HDLRSEncoder(255,239,'BSource','Property','B',0) ```
```hHDLEnc = System: comm.HDLRSEncoder Properties: CodewordLength: 255 MessageLength: 239 PrimitivePolynomialSource: 'Auto' PuncturePatternSource: 'None' BSource: 'Property' B: 0 ```

Create a random message to encode. This message is smaller than the codeword length to demonstrate the shortened-code capability of the objects. Pad the message with zeros to accomodate the Chien search in the decoder and the decoder latency.

```messageLength = 188; dataIn = [randi([0,255],1,messageLength,'uint8') zeros(1,1024-messageLength)]; for ii = 1:1024 messageStart = (ii==1); messageEnd = (ii==messageLength); validIn = (ii<=messageLength); [encOut(ii), startOut(ii), endOut(ii), validOut(ii)] = step(hHDLEnc, dataIn(ii), messageStart, messageEnd, validIn); end ```

Inject errors at random locations in the encoded message. Reed-Solomon can correct up to (N-K)/2 errors in each N symbols. So, in this example the error correction capability is (255-239)/2=8 symbols.

```numErrors = 8; loc = randperm(messageLength, numErrors); % encOut is qualified by validOut, use an offset for injecting errors vi = find(validOut==true,1); for i = 1:numErrors idx = loc(i)+vi; symbol = encOut(idx); encOut(idx) = randi([0 255],'uint8'); fprintf('Symbol(%d), was 0x%x now 0x%x.\n', loc(i), symbol, encOut(idx)); end ```
```Symbol(147), was 0x1f now 0x82. Symbol(16), was 0x6b now 0x82. Symbol(173), was 0x3 now 0xd1. Symbol(144), was 0x66 now 0xcb. Symbol(90), was 0x13 now 0xa4. Symbol(80), was 0x5a now 0x60. Symbol(82), was 0x95 now 0xcf. Symbol(56), was 0xf5 now 0x88. ```

Create an RS Decoder to detect and correct errors in the message. It must have the same code and polynomial as the encoder.

```hHDLDec = comm.HDLRSDecoder(255,239,'BSource','Property','B',0) for ii = 1:1024 [decOut(ii), decStartOut(ii), decEndOut(ii), decValidOut(ii), decErrOut(ii)] = step(hHDLDec, encOut(ii), startOut(ii), endOut(ii), validOut(ii)); end ```
```hHDLDec = System: comm.HDLRSDecoder Properties: CodewordLength: 255 MessageLength: 239 PrimitivePolynomialSource: 'Auto' BSource: 'Property' B: 0 NumErrorsOutputPort: false ```

Select the valid decoder output and compare decoded symbols to the original message.

```decOut = decOut(decValidOut==1); originalMessage = dataIn(1:messageLength); if all(originalMessage==decOut) fprintf('All %d message symbols were correctly decoded.\n', messageLength); else for jj = 1:messageLength if dataIn(jj)~=decOut(jj) fprintf('Error in decoded symbol(%d). Original 0x%x Decoded 0x%x.\n',jj,dataIn(jj),decOut(jj)); end end end ```
```All 188 message symbols were correctly decoded. ```