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.

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.

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 (*N* – *K*)/2 errors in each *N* symbols. So, in this example, the error correction capability is (255 – 239)/2=8 symbols.

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.

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.

All 188 message symbols were correctly decoded.