Main Content

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.

To learn more about how System objects work, see What Are System Objects?

Creation

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.

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.

example

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.

For more information on changing property values, see System Design in MATLAB Using System Objects.

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 CodewordLengthMessageLength 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 CodewordLengthMessageLength 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(log2(CodewordLength+1)), which is the result of int2bit(primpoly(M),bpi)', where bpi is the number of bits per integer.

  • 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 CodewordLengthMessageLength. 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

Description

[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.

example

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

stepRun System object algorithm
releaseRelease resources and allow changes to System object property values and input characteristics
resetReset 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.

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

% Copyright 2019-2022 The MathWorks, Inc.

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

% Copyright 2019-2022 The MathWorks, Inc.

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

Version History

Introduced in R2012b