# comm.HDLRSEncoder

Encode message using Reed-Solomon encoder

## Description

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

To encode a message using a Reed-Solomon code:

1. Create the `comm.HDLRSEncoder` object and set its properties.

2. Call the object with arguments, as if it were a function.

## Creation

### Syntax

``RSEnc = comm.HDLRSEncoder``
``RSEnc = comm.HDLRSEncoder(Name,Value)``
``RSEnc = comm.HDLRSEncoder(N,K,Name,Value)``

### Description

````RSEnc = comm.HDLRSEncoder` creates an HDL-optimized block encoder System object, `RSEnc`, that performs Reed-Solomon encoding in a streaming fashion for HDL.```
````RSEnc = comm.HDLRSEncoder(Name,Value)` sets properties using one or more name-value pairs. Enclose each property name in single quotes. For example, comm.HDLRSEncoder('BSource','Property','B',2) sets a starting power of 2 for the roots of the primitive polynomial.```

example

````RSEnc = comm.HDLRSEncoder(N,K,Name,Value)` sets the `CodewordLength` property to `N`, the `MessageLength` property to `K`, and other specified property names to the specified values.```

## Properties

expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the `release` function unlocks them.

If a property is tunable, you can change its value at any time.

Starting power for roots of the primitive polynomial, specified as a positive integer.

#### Dependencies

The object uses this value when you set `BSource` to `'Property'`.

Source of the starting power for roots of the primitive polynomial, specified as either `'Property'` or `'Auto'`. When you select `'Auto'`, the object uses `B = 1`.

Number of symbols, N, in the RS codeword, specified as a positive integer. This value is rounded up to 2M–1. M is the degree of the primitive polynomial, as specified by the `PrimitivePolynomialSource` and `PrimitivePolynomial` properties. The difference of `CodewordLength``MessageLength` must be an even integer.

If the value of this property is less than 2M–1, the object assumes a shortened RS code.

If you set `PrimitivePolynomialSource` to `'Auto'`, then `CodewordLength` must be in the range 3 < `CodewordLength` ≤ 216 – 1.

If you set `PrimitivePolynomialSource` to `'Property'`, then `CodewordLength` must be in the range 3 ≤ `CodewordLength` ≤ 2M– 1. M must be in the range 3 ≤ M ≤ 16.

Message length, K, specified as a positive integer. The difference of `CodewordLength``MessageLength` must be an even integer.

Source of the primitive polynomial, specified as either `'Property'` or `'Auto'`.

• When you set this property to `'Auto'`, the object uses a primitive polynomial of degree M = `ceil` (Fixed-Point Designer)(log2(`CodewordLength`+1)), which is the result of `fliplr`(`de2bi`(`primpoly`(M))).

• When you set this property to `'Property'`, you must specify a polynomial using the `PrimitivePolynomial` property.

Primitive polynomial, specified as a binary row vector that represents a primitive polynomial over `gf`(2) of degree M, in descending order of powers. The polynomial defines the finite field `gf`(2M) corresponding to the integers that form messages and codewords.

#### Dependencies

The object uses this value when you set `PrimitivePolynomialSource` to `'Property'`.

Source of the puncture pattern, specified as `'None'` or `'Property'`. 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.

Pattern used to puncture the encoded data, specified 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.

#### Dependencies

This property applies when you set the `PuncturePatternSource` property to `'Property'`.

## Usage

### Syntax

``[Y,startOut,endOut,validOut] = RSEnc(X,startIn,endIn,validIn) ``

### Description

example

````[Y,startOut,endOut,validOut] = RSEnc(X,startIn,endIn,validIn) `encodes one input message symbol, `X`, and returns one symbol of encoded data, `Y`. The `start` and `end` signals indicate the message frame boundaries. The object returns associated parity symbols at the end of each message frame.```

### Input Arguments

expand all

Input message data, one symbol at a time, specified as an unsigned integer or `fi()` with any binary point scaling. The word length of each symbol must be `ceil(log2(CodewordLength+1))`.

`double` type is allowed for simulation but not supported for HDL code generation.

Data Types: `double` | `uint8` | `uint16` | `uint32` | `fi`

Start of input data frame, specified as a logical scalar.

Data Types: `logical`

End of input data frame, specified as a logical scalar.

Data Types: `logical`

Validity of input data, specified as a logical scalar.

Data Types: `logical`

### Output Arguments

expand all

Message data and parity symbols, returned one symbol at a time, as an integer with the same data type as the input message, `X`.

Data Types: `double` | `uint8` | `uint16` | `uint32` | `fi`

Start of output data frame, returned as a logical scalar.

Data Types: `logical`

End of output data frame, returned as a logical scalar.

Data Types: `logical`

Validity of output data, returned as a logical scalar.

Data Types: `logical`

## Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named `obj`, use this syntax:

`release(obj)`

expand all

 `step` Run System object algorithm `release` Release resources and allow changes to System object property values and input characteristics `reset` Reset internal states of System object

## Examples

collapse all

Encode and decode a signal using Reed Solomon encoder and decoder System objects. This example shows how to include each object in a function for HDL code generation.

Create a random message to encode. This message is smaller than the codeword length to show how the objects support shortened codes. Pad the message with zeros to accommodate the latency of the decoder, including the Chien search.

```messageLength = 188; dataIn = [randi([0,255],1,messageLength,'uint8') zeros(1,1024-messageLength)]; ```

Write a function that creates and calls a `HDLRSEncoder` System object™ with an RS(255,239) code. This code is used in the IEEE® 802.16 Broadband Wireless Access standard. `B` is the starting power of the roots of the primitive polynomial. You can generate HDL from this function.

Note: This object syntax runs only in R2016b or later. If you are using an earlier release, replace each call of an object with the equivalent `step` syntax. For example, replace `myObject(x)` with `step(myObject,x)`.

```function [dataOut,startOut,endOut,validOut] = HDLRSEnc80216(dataIn,startIn,endIn,validIn) %HDLRSEnc80216 % Processes one sample of data using the comm.HDLRSEncoder System object(TM) % dataIn is a uint8 scalar, representing 8 bits of binary data. % startIn, endIn, and validIn are logical scalar values. % You can generate HDL code from this function. persistent rsEnc80216; if isempty(rsEnc80216) rsEnc80216 = comm.HDLRSEncoder(255,239,'BSource','Property','B',0) end [dataOut,startOut,endOut,validOut] = rsEnc80216(dataIn,startIn,endIn,validIn); end ```

Call the function to encode the message.

```for ii = 1:1024 messageStart = (ii==1); messageEnd = (ii==messageLength); validIn = (ii<=messageLength); [encOut(ii),startOut(ii),endOut(ii),validOut(ii)] = ... HDLRSEnc80216(dataIn(ii),messageStart,messageEnd,validIn); end ```
```rsEnc80216 = comm.HDLRSEncoder with properties: CodewordLength: 255 MessageLength: 239 PrimitivePolynomialSource: 'Auto' PuncturePatternSource: 'None' BSource: 'Property' B: 0 ```

Inject errors at random locations in the encoded message. Reed-Solomon can correct up to (NK)/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 ```

Write a function that creates and calls a `HDLRSDecoder` System object™. This object must have the same code and polynomial as the encoder. You can generate HDL from this function.

```function [dataOut,startOut,endOut,validOut,err] = HDLRSDec80216(dataIn,startIn,endIn,validIn) %HDLRSDec80216 % Processes one sample of data using the comm.HDLRSDecoder System object(TM) % dataIn is a uint8 scalar, representing 8 bits of binary data. % startIn, endIn, and validIn are logical scalar values. % You can generate HDL code from this function. persistent rsDec80216; if isempty(rsDec80216) rsDec80216 = comm.HDLRSDecoder(255,239,'BSource','Property','B',0) end [dataOut,startOut,endOut,validOut,err] = rsDec80216(dataIn,startIn,endIn,validIn); end ```

Call the function to detect errors in the encoded message.

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

Select the valid decoder output and compare the 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. ```

## Extended Capabilities

### Blocks

Introduced in R2012b
{"ft_lang_label":"__label__en","ft_lang_prob":0.6297648,"math_prob":0.89919025,"size":4377,"snap":"2021-21-2021-25","text_gpt3_token_len":1273,"char_repetition_ratio":0.13583352,"word_repetition_ratio":0.16911764,"special_character_ratio":0.27713045,"punctuation_ratio":0.21327014,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9711955,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-05-09T18:58:40Z\",\"WARC-Record-ID\":\"<urn:uuid:73028510-70f8-454e-b373-48b0a2e82b61>\",\"Content-Length\":\"117286\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0c6b9dfc-73e7-4317-b90e-cb4a9c466b1e>\",\"WARC-Concurrent-To\":\"<urn:uuid:91e78b58-119a-4783-8c75-c598e6495c1f>\",\"WARC-IP-Address\":\"23.223.252.57\",\"WARC-Target-URI\":\"https://in.mathworks.com/help/comm/ref/comm.hdlrsencoder-system-object.html\",\"WARC-Payload-Digest\":\"sha1:ISRFV57RALASLFHAV74GMDWM53APAY6K\",\"WARC-Block-Digest\":\"sha1:5MG7FDWQAYHAZ57X3NNLZTMFFIN6ZUJ7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-21/CC-MAIN-2021-21_segments_1620243989012.26_warc_CC-MAIN-20210509183309-20210509213309-00552.warc.gz\"}"}