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:
Create the
comm.HDLRSEncoder
object and set its properties.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
Syntax
Description
creates an
HDL-optimized block encoder System object, RSEnc
= comm.HDLRSEncoderRSEnc
, that performs Reed-Solomon encoding in a
streaming fashion for HDL.
sets properties using one or more name-value pairs. Enclose each property name in single
quotes. For example,
RSEnc
= comm.HDLRSEncoder(Name,Value
)
comm.HDLRSEncoder('BSource','Property','B',2)
sets the RSEnc
= comm.HDLRSEncoder(N
,K
,Name,Value
)CodewordLength
property to N
, the
MessageLength
property to K
, and other specified
property names to the specified values.
Properties
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.
B
— Starting power for roots of primitive polynomial
1
(default) | positive integer
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'
.
BSource
— Source of starting power for roots of primitive polynomial
'Auto'
(default) | '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
.
CodewordLength
— Number of symbols, N, in RS codeword
7
(default) | positive integer
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.
MessageLength
— Message length, K
3 (default) | positive integer
Message length, K, specified as a positive integer. The
difference of CodewordLength
– MessageLength
must be an even integer.
PrimitivePolynomialSource
— Source of primitive polynomial
'Auto'
(default) | 'Property'
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 ofint2bit
(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 thePrimitivePolynomial
property.
PrimitivePolynomial
— Primitive polynomial
[1 0 1 1]
(default) | binary row vector
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'
.
PuncturePatternSource
— Source of puncture pattern
'None'
(default) | '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.
PuncturePattern
— Pattern used to puncture encoded data
[ones(2,1); zeros(2,1)]
(default) | binary column vector
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
Description
Input Arguments
X
— Input message symbol
integer
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
startIn
— Start of input data frame
logical scalar
Start of input data frame, specified as a logical scalar.
Data Types: logical
endIn
— End of input data frame
logical scalar
End of input data frame, specified as a logical scalar.
Data Types: logical
validIn
— Validity of input data
logical scalar
Validity of input data, specified as a logical scalar.
Data Types: logical
Output Arguments
Y
— Output message data and parity symbols
integer
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
startOut
— Start of output data frame
logical scalar
Start of output data frame, returned as a logical scalar.
Data Types: logical
endOut
— End of output data frame
logical scalar
End of output data frame, returned as a logical scalar.
Data Types: logical
validOut
— Validity of output data
logical scalar
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)
Examples
Reed-Solomon Coding and Error Detection for HDL
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 (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
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
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
This System object supports C/C++ code generation for accelerating MATLAB® simulations, and for DPI component generation.
HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.
The software supports double
and
single
data types for simulation, but not for HDL code generation.
To generate HDL code from predefined System objects, see HDL Code Generation from Viterbi Decoder System Object (HDL Coder).
Version History
Introduced in R2012b
See Also
Objects
Functions
Blocks
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)