comm.RSDecoder
Decode data using ReedSolomon decoder
Description
The comm.RSDecoder
System object™ recovers a message vector from a ReedSolomon (RS) codeword vector. For proper
decoding, the property values for this object must match the property values in the
corresponding comm.RSEncoder
object. For more information, see
Algorithms.
To decode data using an RScoding scheme:
Create the
comm.RSDecoder
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 RS decoder
System object, dec
= comm.RSDecoderdec
, that applies RS decoding.
sets dec
= comm.RSDecoder(N
,K
)CodewordLength
to
N
and MessageLength
to
K
. For example, comm.RSDecoder(7,3)
creates a
(7,3) RS decoder object.
additionally sets dec
= comm.RSDecoder(N
,K
,GP
)GeneratorPolynomial
to
GP
and GeneratorPolynomialSource
to 'Property'
.
additionally sets dec
= comm.RSDecoder(N
,K
,GP
,S
)ShortMessageLength
to
S
and ShortMessageLengthSource
to 'Property'
.
specifies options using one or more namevalue arguments in addition to the input
arguments in previous syntaxes. For example,
dec
= comm.RSDecoder(___,Name
,Value
)comm.RSDecoder(BitInput="true")
creates an RS decoder object
configured to decode an input column vector of bits.
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.
BitInput
— Option to input bits
false
or 0
(default)  true
or 1
Option to input bits, specified as a logical 0
(false
) or 1
(true
).
When you set this property to
false
, the inputX
must be a numeric, column vector of integers with a length equal to an integer multiple of (CodewordLength
– N_{punctures}). The decoded data output,Y
, is a column vector of integers whose length is an integer multiple ofMessageLength
. Each symbol that forms the input codewords and output message is an integer in the range [0, (2^{M} – 1)]. These integers correspond to an element of the finite Galois fieldgf
(2^{M}).When you set
ErasuresInputPort
totrue
andBitInput
tofalse
, the erasures input,erasures
, must be of length equal to the encoded data input vectorX
. Values of1
in theerasures
vector correspond to erased symbols in the same position as the input codewords, and values of0
correspond to nonerased symbols.
When you set this property to true, the input
X
must be a numeric, column vector of bits with a length equal to an integer multiple of (CodewordLength
– N_{punctures})×M. The data type of inputX
can be numeric, logical, or unsigned fixed point of word length 1 (fi
(FixedPoint Designer) object). The decoded output,Y
, is a column vector of bits with length equal to an integer multiple ofMessageLength
×M. A group of M bits represents an integer in the range [0, (2^{M} – 1)]. These integers correspond to an element of the finite Galois fieldgf
(2^{M}).When you set
ErasuresInputPort
totrue
andBitInput
totrue
, the erasures input,erasures
, must be of length 1/M times the length of the input encoded data vectorX
. Values of1
in theerasures
vector correspond to erased symbols and values of0
correspond to nonerased symbols. In this case a symbol corresponds to M bits.
N_{punctures} is the number of punctures. You
specify the number of punctures with the PuncturePatternSource
and PuncturePattern
properties.
M is the degree of the primitive polynomial that you specify with
the PrimitivePolynomialSource
and PrimitivePolynomial
properties.
Data Types: logical
CodewordLength
— Codeword length of RS code
7 (default)  positive integer
Codeword length of RS code
Codeword length of the RS code in symbols, specified as a positive integer.
If you set
PrimitivePolynomialSource
to"Auto"
,CodewordLength
must be in the range (3, 2^{16} – 1].If you set
PrimitivePolynomialSource
to"Property"
,CodewordLength
must be in the range [3, 2^{M} – 1], where M is the degree of the primitive polynomial that you specify withPrimitivePolynomialSource
andPrimitivePolynomial
. M must be in the range [3, 16]. The difference (CodewordLength
–MessageLength
) must be an even integer.
For a fulllength RS code, CodewordLength
must be
(2^{M} – 1). If
CodewordLength
is less than
(2^{M} – 1), the object assumes a shortened
RS code.
MessageLength
— Message length
3 (default)  positive integer
Message length in symbols, specified as a positive integer.
ShortMessageLengthSource
— Source of short message length
"Auto"
(default)  "Property"
Source of the short message length, specified as "Auto"
or
"Property"
.
When you set this property to
"Auto"
, shortening is specified viaCodewordLength
andMessageLength
. The object defines the RS code based on theCodewordLength
,MessageLength
,GeneratorPolynomial
, andPrimitivePolynomial
properties.When you set this property to
"Property"
, the object usesShortMessageLength
.
ShortMessageLength
— Shortened message length
3
(default)  positive integer
Shortened message length in symbols, specified as a positive integer whose value
must be less than or equal to MessageLength
. When
ShortMessageLength
< MessageLength
, the object
shortens the RS code.
Dependencies
This property applies when you set ShortMessageLengthSource
to
"Property"
.
GeneratorPolynomialSource
— Source of generator polynomial
"Auto"
(default)  "Property"
Source of the generator polynomial, specified as Auto
or
"Property"
.
When you set this property to
"Auto"
, the object calculates the generator polynomial based on the value ofPrimitivePolynomialSource
andPrimitivePolynomial
.When you set
PrimitivePolynomialSource
to"Auto"
, the object computes the generator polynomial asrsgenpoly
(CodewordLength
+ SL,MessageLength
+ SL).When you set
PrimitivePolynomialSource
to"Property"
, the object computes generator polynomial asrsgenpoly
(CodewordLength
+ SL,MessageLength
+ SL,PrimitivePolynomial
).In both cases, SL = (2^{M} – 1) –
CodewordLength
is the shortening length and M is the degree of the primitive polynomial you specify withPrimitivePolynomialSource
andPrimitivePolynomial
.
When you set this property to
"Property"
, the object usesGeneratorPolynomial
to define the generator polynomial.
GeneratorPolynomial
— Generator polynomial
[1 3 1 2 3]
(default)  integer row vector  gf
row vector
Generator polynomial, specified as a polynomial string, integer row vector, or
Galois field row vector. The length of the generator polynomial must be (CodewordLength
– MessageLength
+ 1).
Galois field row vector entries must be in the range [0,
2^{M} – 1] and represent a generator
polynomial in descending order of powers. Each coefficient must be an element of the
Galois field gf
(2^{M}), represented in
integer format.
The default is the result of
, which corresponds to
rsgenpoly
(7,3,[],[],'double')[1 3 1 2 3]
.
Dependencies
This property applies when you set GeneratorPolynomialSource
to Property
.
CheckGeneratorPolynomial
— Option to check generator polynomial
true
or 1
(default)  false
or 0
Option to check the generator polynomial, specified as a logical
1
(true
) or 0
(false
). Set this property to true
to confirm
that X^{N} + 1 is divisible by the generator polynomial you specify in GeneratorPolynomial
.
N is CodewordLength
.
A valid generator polynomial is given by (X – α^{B})×(X –
α^{(B +
1)})×...×(X –
α^{(B + N –
K – 1)}). Where α is a root of the primitive polynomial, B is an integer, N is CodewordLength
, and
K is MessageLength
. If the value
of B is 1, then you can set this property to
false
. For more information about B, see the rsgenpoly
.
Tips
For codes with generator polynomials of high degree, disabling the check speeds up
processing. As a best practice, perform the check at least once before setting this
property to false
.
Dependencies
This property applies when you set GeneratorPolynomialSource
to "Property"
.
Data Types: logical
PrimitivePolynomialSource
— Source of primitive polynomial
"Auto"
(default)  "Property"
Source of the primitive polynomial, specified as "Auto"
or
"Property"
.
When you set this property to
"Auto"
, the object uses a primitive polynomial of degree M =ceil
(log2(CodewordLength
+ 1)).When you set this property to
"Property"
, the object usesPrimitivePolynomial
to define the primitive polynomial.
PrimitivePolynomial
— Primitive polynomial
[1 0 1 1]
(default)  binary row vector  character vector  string scalar
Primitive polynomial, specified as a binary row vector that defines the finite field
gf
(2^{M}) in descending order
of powers corresponding to the integers that form messages and codewords.
Express the polynomial as one of these options:
Character vector or string scalar of a polynomial whose constant term is
1
. For more information, see Representation of Polynomials in Communications Toolbox.Binaryvalued row vector that represents the coefficients of the polynomial in order of descending powers. The length of this vector must be M + 1, where M is the degree of the polynomial. The first and last entries must be
1
, indicating the leading term with degree M and a constant term of 1.Integervalued row vector of elements that represent the exponents for the nonzero terms of the polynomial in order of descending powers. The last entry must be
0
, indicating a constant term of 1.
The default is the result of
, which is
int2bit
(primpoly
(3),4)'[1 0 1 1]
or the polynomial x^{3} + x +
1.
Dependencies
This property applies when you set PrimitivePolynomialSource
to Property
.
PuncturePatternSource
— Source of puncture pattern
"None"
(default)  "Property"
Source of the puncture pattern, specified as "None"
or
"Property"
.
When you set this property to
"None"
, the object does not apply puncturing to the code.When you set this property to
"Property"
, the object punctures the code using the puncture pattern vector defined byPuncturePattern
.
PuncturePattern
— Puncture pattern
[ones(2,1); zeros(2,1)]
(default)  binary column vector
Puncture pattern, specified as a binary column vector with a length of
(N – K), where N is CodewordLength
and
K is MessageLength
. 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 PuncturePatternSource
to "Property"
.
ErasuresInputPort
— Option to enable erasures input
false
or 0
(default)  true
or 1
Option to enable erasures input, specified as a logical 0
(false
) or 1
(true
).
When you set this property to true
, you must provide the
erasures
vector input when running the object. The length of the erasures vector is explained in
Input and Output Signal Lengths in BCH and RS System Objects.
When you set this property to false
, the object assumes no
erasures.
Data Types: logical
NumCorrectedErrorsOutputPort
— Option to output number of corrected errors
true
or 1
(default)  false
or 0
Option to output number of corrected errors, specified as logical
1
(true
) or 0
(false
).
Set this property to true
to obtain the number of corrected
errors as an output when running the object. A nonnegative value in the
ith element of the error output vector, denotes the number of
corrected errors in the ith input codeword. A value of
1
in the ith element of the error output vector
indicates that a decoding error occurred for that codeword. A decoding error occurs when
an input codeword has more errors than the error correction capability of the RS
code.
Data Types: logical
OutputDataType
— Output data type
"Same as input"
(default)  "double"
 "logical"
Output data type, specified as "Same as input"
,
"double"
, or "logical"
.
Dependencies
This property applies when you set BitInput
to
true
.
Usage
Description
decodes the
encoded input data using a (Y
= dec(X
)CodewordLength
,MessageLength
) RS decoder. This
syntax applies when you set NumCorrectedErrorsOutputPort
to false
. For information
about the input and output lengths when you run the object, see Input and Output Signal Lengths in BCH and RS System Objects.
uses the Y
= dec(X
,erasures
)erasures
input vector to erase symbols of the input
codewords in X
. This syntax
applies when you set ErasuresInputPort
to
true
.
[
decodes using inputs from either of the previous syntaxes, and returns the RSdecoded
output vector Y
,err
] = dec(___)Y
and the
number of corrected symbols in output vector err
. A value of
1
in the ith element of the error output vector
indicates that a decoding error occurred for that codeword. This syntax applies when you
set NumCorrectedErrorsOutputPort
to true
.
Input Arguments
X
— Input codeword
column vector
Input codeword, specified as a column vector. For information about the input and output lengths when you run the object, see Input and Output Signal Lengths in BCH and RS System Objects.
Data Types: double
 single
 logical
 int8
 int16
 int32
 uint8
 uint16
 uint32
 fi object
erasures
— Symbols to erase
binary column vector
Symbols to erase from the input codewords, specified as a binary column input
vector. Values of 1 in the erasures
vector correspond to erased
symbols, and values of 0 correspond to nonerased symbols.
Data Types: double
 logical
Output Arguments
Y
— RSdecoded data
column vector
RSdecoded data, returned as a column vector. The output vector length and data
type depends on BitInput
and OutputDataType
.
err
— Number of corrected symbols
integer
Number of corrected symbols when decoding the input codeword, returned as an integer.
A nonnegative value indicates the number of corrected errors in the current input codeword.
A value of
1
indicates that a decoding error occurred for that codeword. A decoding error occurs when an input codeword has more errors than the error correction capability of the RS code.
Data Types: double
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
Transmit RSEncoded, 8DPSKModulated Symbol Stream
Transmit an RSencoded, 8DPSKmodulated symbol stream through an AWGN channel. After channel filtering demodulate and decode the received signal, and then count errors.
M = 8; rsEnc = comm.RSEncoder; rsDec = comm.RSDecoder; errorRate = comm.ErrorRate(ComputationDelay=3); for counter = 1:20 data = randi([0 7], 30, 1); encodedData = rsEnc(data); modSignal = dpskmod(encodedData,M); receivedSignal = awgn(modSignal,10); demodSignal = dpskdemod(receivedSignal,M); receivedSymbols = rsDec(demodSignal); errorStats = errorRate(data,receivedSymbols); end fprintf('Error rate = %f\nNumber of errors = %d\n', ... errorStats(1), errorStats(2))
Error rate = 0.128978 Number of errors = 77
Estimate BER of QPSK in AWGN with ReedSolomon Coding
Transmit ReedSolomon encoded data using QPSK over an AWGN channel. Demodulate and decode the received signal and collect error statistics. Compute the theoretical bit error rate (BER) for coded and noncoded data. Plot the BER results to compare performance.
Define the example parameters.
rng(1993); % Seed random number generator for repeatable results M = 4; % Modulation order bps = log2(M); % Bits per symbol N = 7; % RS codeword length K = 5; % RS message length
Create AWGN channel and error rate objects.
awgnChannel = comm.AWGNChannel( ...
BitsPerSymbol=bps);
errorRate = comm.ErrorRate;
Create a (7,5) ReedSolomon encoder and decoder pair that accepts bit inputs.
rsEncoder = comm.RSEncoder( ... BitInput=true, ... CodewordLength=N, ... MessageLength=K); rsDecoder = comm.RSDecoder( ... BitInput=true, ... CodewordLength=N, ... MessageLength=K);
Set the range of $${E}_{b}/{N}_{0}$$ values and account for RS coding gain. Initialize the error statistics matrix.
ebnoVec = (3:0.5:8)'; ebnoVecCodingGain = ... ebnoVec + 10*log10(K/N); % Account for RS coding gain errorStats = zeros(length(ebnoVec),3);
Estimate the bit error rate for each $${E}_{b}/{N}_{0}$$ value. The simulation runs until either 100 errors or $$1{0}^{7}$$ bits is encountered. The simulation loop encodes and modulates random data, filters the signal through an AWGN channel, demodulate and decodes the signal, and then computes error statistics.
for i = 1:length(ebnoVec) awgnChannel.EbNo = ebnoVecCodingGain(i); reset(errorRate) while errorStats(i,2) < 100 && errorStats(i,3) < 1e7 data = randi([0 1],1500,1); encData = rsEncoder(data); modData = pskmod(encData,M,InputType='bit'); rxSig = awgnChannel(modData); rxData = pskdemod(rxSig,M,OutputType='bit'); decData = rsDecoder(rxData); errorStats(i,:) = errorRate(data,decData); end end
Fit a curve to the BER data using berfit
. Generate an estimate of QPSK performance with and without coding by using the bercoding
and berawgn
functions.
berCurveFit = berfit(ebnoVecCodingGain,errorStats(:,1)); berwCoding = bercoding(ebnoVec,'RS','hard',N,K,'psk',M,'nondiff'); berNoCoding = berawgn(ebnoVec,'psk',M,'nondiff');
Plot the RScoded BER data, curve fit of the BER data, theoretical performance with RS coding, and theoretical performance without RS coding. The (7,5) RS code improves the $${E}_{b}/{N}_{0}$$ required to achieve a $$1{0}^{2}$$ bit error rate by approximately 1.2 dB.
semilogy(ebnoVecCodingGain,errorStats(:,1),'b*', ... ebnoVecCodingGain,berCurveFit,'c', ... ebnoVecCodingGain,berwCoding,'r', ... ebnoVec,berNoCoding) ylabel('BER') xlabel('Eb/No (dB)') legend( ... 'RScoded BER','Curve fit', ... 'Theory with coding','Theory no coding') grid
Transmit Shortened, RSEncoded, 256QAMModulated Symbol Stream
Transmit a shortened, RSencoded, 256QAMmodulated symbol stream through an AWGN channel. Then demodulate, decode, and count errors.
Set the parameters for the ReedSolomon code, where N
is the codeword length, K
is the nominal message length, and S
is the shortened message length. Set the modulation order, M
, and the number of frames, L
.
N = 255; K = 239; S = 188; M = 256; L = 50; bps = log2(M);
Create an AWGN channel System object and an error rate System object.
awgnChan = comm.AWGNChannel( ... NoiseMethod='Signal to noise ratio (Eb/No)', ... EbNo=15, ... BitsPerSymbol=bps); errorRate = comm.ErrorRate(ComputationDelay=3);
Create the ReedSolomon generator polynomial from the DVBT standard.
gp = rsgenpoly(N,K,[],0);
Create a ReedSolomon encoder and decoder pair using the shortened message length, S
, and the DVBT generator polynomial, gp
.
enc = comm.RSEncoder(N,K,gp,S); dec = comm.RSDecoder(N,K,gp,S);
Generate random symbol frames whose length equals one message block. Encode, modulate, apply AWGN, demodulate, decode, and collect statistics.
for counter = 1:L data = randi([0 1],S*bps,1); encodedData = enc(bit2int(data,bps)); modSignal = qammod(encodedData,M,UnitAveragePower=true); rxSignal = awgnChan(modSignal); demodSignal = qamdemod(rxSignal,M,UnitAveragePower=true); rxBits = dec(demodSignal); dataOut = int2bit(rxBits,bps); errorStats = errorRate(data(:),dataOut(:)); end
Display the error rate and number of errors.
fprintf('Error rate = %5.2e\nNumber of errors = %d\n', ... errorStats(1), errorStats(2))
Error rate = 2.01e02 Number of errors = 1514
ReedSolomon Coding with Erasures
This example shows how to configure the comm.RSEncoder
and comm.RSDecoder
System objects to perform ReedSolomon (RS) block coding with erasures when simulating a communications system. A codeword with erasures can have those erasures in either message symbols or parity symbols. A punctured codeword has only parity symbols removed, and a shortened codeword has only message symbols removed.
RS decoders can correct both errors and erasures. A receiver that identifies the most unreliable symbols in a given codeword can generate symbol erasures and replace those symbols with zeros. When sending a symbol to the decoder, the receiver passes a flag to the decoder to indicate if the symbol is an erasure rather than a valid code symbol. In a similar fashion, the encoder can apply a puncture pattern that always removes specific parity symbols from its output. Given the puncture pattern, the decoder inserts zeros in the puncture positions, and treats those symbols the same way it treats erasures. Punctured codes offer code rate flexibility, but at the expense of some error correction capability. For the same demodulator input energy per bit to noise power spectral density ratio (${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$), shortened codes, however, achieve the same code rate flexibility without degrading the error correction performance.
In this example, you simulate a communications system that consists of a random source, an RS encoder, a 64QAM modulator, an AWGN channel, a 64QAM demodulator, and an RS decoder. You analyze the RS coding with erasures by comparing the channel bit error rate (BER) performance versus the coded BER performance. You obtain the channel BER by comparing inputs for the QAM modulator to outputs from the QAM demodulator and the coded BER by comparing inputs for the RS encoder to outputs from the RS decoder.
Initialization
The helperRSCodingConfig.m helper function initializes the simulation parameters and configures the comm.AWGNChannel
and comm.ErrorRate
System objects that simulate the communications system. The helper function also sets the uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio (EbNoUncoded
) to 15 dB and the simulation stop criteria for 500 errors or a maximum $5\times {10}^{6}$ bits are transmitted.
helperRSCodingConfig;
Configure RS Encoder/Decoder
This example uses a (63,53) RS code operating with a 64QAM modulation scheme. This code can correct (63 – 53)/2 = 5 errors, or it can alternatively correct (63 – 53) = 10 erasures. For each codeword at the output of the 64QAM demodulator, the receiver determines the six least reliable symbols using the helperRSCodingGetErasures.m helper function. The indices that point to the location of these unreliable symbols are an input to the RS decoder. The RS decoder treats these symbols as erasures resulting in an error correction capability of (10 – 6)/2 = 2 errors per codeword.
Create a comm.RSEncoder
System object and set the BitInput
property to false
so that the encoder inputs and outputs are integer symbols.
N = 63; % Codeword length K = 53; % Message length rsEncoder = comm.RSEncoder(N,K,BitInput=false); numErasures = 6;
Create a comm.RSDecoder
System object that matches the configuration of the comm.RSEncoder
object.
rsDecoder = comm.RSDecoder(N,K,BitInput=false);
Set the ErasuresInputPort
property to true
to specify erasures as an input to the decoder object.
rsDecoder.ErasuresInputPort = true;
Set the NumCorrectedErrorsOutputPort
property to true
so that the decoder outputs the number of corrected errors. A nonnegative value in the error output denotes the number of corrected errors in the input codeword. A value of –1 in the error output indicates a decoding error. A decoding error occurs when the input codeword has more errors than the error correction capability of the RS code.
rsDecoder.NumCorrectedErrorsOutputPort = true;
Stream Processing Loop
Simulate the communications system for an uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio of 15 dB. The uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ is the ratio that would be measured at the input of the channel if there were no coding in the system.
Since the signal going into the AWGN channel is the encoded signal, you must convert the uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values so that they correspond to the energy ratio at the encoder output. This ratio is the coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio. If you input K
symbols to the encoder and obtain N
output symbols, then the energy relation is given by the K/N
rate. Set the EbNo
property of the AWGN channel object to the computed coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ value.
EbNoCoded = EbNoUncoded + 10*log10(K/N); channel.EbNo = EbNoCoded;
Loop the simulation until it reaches the target number of errors or the maximum number of transmissions.
chanErrorStats = zeros(3,1); codedErrorStats = zeros(3,1); correctedErrors = 0; while (codedErrorStats(2) < targetErrors) && ... (codedErrorStats(3) < maxNumTransmissions)
The data symbols transmit one message word at a time. Each message word has K
symbols in the range [0, N
].
data = randi([0 N],K,1);
Encode the message word. The encoded word, encData
, is (N
– numPunc
) symbols long.
encData = rsEncoder(data);
Modulate encoded data and add noise. Then demodulate channel output.
modData = qammod(encData,M); chanOutput = channel(modData); demodData = qamdemod(chanOutput,M);
Use the helperRSCodingGetErasures.m
helper function to find the six least reliable symbols and generate an erasures vector. The length of the erasures vector must be equal to the number of symbols in the demodulated codeword. A one in the ith element of the vector erases the ith symbol in the codeword. Zeros in the vector indicate no erasures.
erasuresVec = helperRSCodingGetErasures(chanOutput,numErasures);
Decode the data. Log the number of errors corrected by the RS decoder.
[estData,errs] = rsDecoder(demodData,erasuresVec); if (errs >= 0) correctedErrors = correctedErrors + errs; end
When computing the channel and coded BERs, convert integers to bits.
chanErrorStats(:,1) = ... chanBERCalc(int2bit(encData,log2(M)), ... int2bit(demodData,log2(M))); codedErrorStats(:,1) = ... codedBERCalc(int2bit(data,log2(M)), ... int2bit(estData,log2(M))); end
The error rate measurement objects, chanBERCalc
and codedBERCalc
, output 3by1 vectors containing BER measurement updates, the number of errors, and the total number of bit transmissions. Display the channel BER, the coded BER and the total number of errors corrected by the RS decoder.
chanBitErrorRate = chanErrorStats(1)
chanBitErrorRate = 0.0017
codedBitErrorRate = codedErrorStats(1)
codedBitErrorRate = 0
totalCorrectedErrors = correctedErrors
totalCorrectedErrors = 882
To run the simulation for a set of ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values, you can add a for
loop around the processing loop above. For a simulation with uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values in the range 4:15 dB, a target number of errors equal to 5000, and maximum number of transmissions equal to $50\times {10}^{6}$, the results in the figure show that the channel BER is worse than the theoretical 64QAM BER because ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ is reduced by the code rate.
Helper Functions
ReedSolomon Coding with Erasures and Punctures
This example shows how to configure the comm.RSEncoder
and comm.RSDecoder
System objects to perform ReedSolomon (RS) block coding with erasures and puncture codes when simulating a communications system. An encoder can generate punctures to remove specific parity symbols from its output. Given the puncture pattern, the decoder inserts zeros in the puncture positions and treats those symbols as erasures. The decoder treats encodergenerated punctures and receivergenerated erasures in exactly the same way when it decodes. Puncturing provides code rate flexibility, but at the expense of some error correction capability.
In this example, you simulate a communications system that consists of a random source, an RS encoder, a 64QAM modulator, an AWGN channel, a 64QAM demodulator, and an RS decoder. You analyze the RS coding with erasures and puncturing by comparing the channel bit error rate (BER) performance versus the coded BER performance. You obtain the channel BER by comparing inputs for the QAM modulator to outputs from the QAM demodulator and the coded BER by comparing inputs for the RS encoder to outputs from the RS decoder.
Initialization
The helperRSCodingConfig.m helper function initializes the simulation parameters and configures the comm.AWGNChannel
and comm.ErrorRate
System objects that simulate the communications system. The helper function also sets the uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio (EbNoUncoded
) to 15 dB and the simulation stop criteria for 500 errors or a maximum $5\times {10}^{6}$ bits are transmitted.
helperRSCodingConfig;
Configure RS Encoder/Decoder
This example uses the same (63,53) RS code operating with a 64QAM modulation scheme that is configured for erasures and code puncturing. The RS algorithm decodes receivergenerated erasures and corrects encodergenerated punctures. For each codeword, the sum of the punctures and erasures cannot exceed twice the errorcorrecting capability of the code.
Create a comm.RSEncoder
System object and set the BitInput
property to false
so that the encoder inputs and outputs are integer symbols.
N = 63; % Codeword length K = 53; % Message length rsEncoder = comm.RSEncoder(N,K,BitInput=false); numErasures = 6;
Create a comm.RSDecoder
System object that matches the configuration of the comm.RSEncoder
object. Then set the ErasuresInputPort
property to true
to specify erasures as an input to the decoder object.
rsDecoder = comm.RSDecoder(N,K,BitInput=false); rsDecoder.ErasuresInputPort = true;
To enable code puncturing, set the PuncturePatternSource
property to 'Property'
and set the PuncturePattern
property to the desired puncture pattern vector. The same puncture vector must be specified in both the encoder and decoder. This example punctures two symbols from each codeword. Values of 1
in the puncture pattern vector indicate nonpunctured symbols, and values of 0
indicate punctured symbols.
numPuncs = 2; rsEnc.PuncturePatternSource = 'Property'; rsEnc.PuncturePattern = [ones(NKnumPuncs,1); zeros(numPuncs,1)]; rsDec.PuncturePatternSource = 'Property'; rsDec.PuncturePattern = rsEnc.PuncturePattern;
Stream Processing Loop
Simulate the communications system for an uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio of 15 dB. The uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ is the ratio that would be measured at the input of the channel if there were no coding in the system.
Since the signal going into the AWGN channel is the encoded signal, you must convert the uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values so that they correspond to the energy ratio at the encoder output. This ratio is the coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio. If you input K
symbols to the encoder and obtain N
output symbols, then the energy relation is given by the K/N
rate. Since the length of the codewords generated by the RS encoder is reduced by the number of punctures specified in the puncture pattern vector, the value of the coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio needs to be adjusted to account for these punctures. In this example, the number of output symbols is (N
– numPuncs
) and the uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio relates to the coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ as shown below. Set the EbNo
property of the AWGN channel object to the computed coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ value.
EbNoCoded = EbNoUncoded + 10*log10(K/(N  numPuncs)); channel.EbNo = EbNoCoded;
Loop the simulation until it reaches the target number of errors or the maximum number of transmissions.
chanErrorStats = zeros(3,1); codedErrorStats = zeros(3,1); correctedErrors = 0; while (codedErrorStats(2) < targetErrors) && ... (codedErrorStats(3) < maxNumTransmissions)
The data symbols transmit one message word at a time. Each message word has K
symbols in the range [0, N
].
data = randi([0 N],K,1);
Encode the message word. The encoded word, encData
, is (N
– numPunc
) symbols long.
encData = rsEncoder(data);
Modulate encoded data and add noise. Then demodulate channel output.
modData = qammod(encData,M); chanOutput = channel(modData); demodData = qamdemod(chanOutput,M);
Use the helperRSCodingGetErasures.m helper function to find the six least reliable symbols and generate an erasures vector. The length of the erasures vector must be equal to the number of symbols in the demodulated codeword. A one in the ith element of the vector erases the ith symbol in the codeword. Zeros in the vector indicate no erasures.
erasuresVec = helperRSCodingGetErasures(chanOutput,numErasures);
Decode the data. Log the number of errors corrected by the RS decoder.
[estData,errs] = rsDecoder(demodData,erasuresVec); if (errs >= 0) correctedErrors = correctedErrors+errs; end
When computing the channel and coded BERs, convert integers to bits.
chanErrorStats(:,1) = ... chanBERCalc(int2bit(encData,log2(M)), ... int2bit(demodData,log2(M))); codedErrorStats(:,1) = ... codedBERCalc(int2bit(data,log2(M)), ... int2bit(estData,log2(M))); end
The error rate measurement objects, chanBERCalc
and codedBERCalc
, output 3by1 vectors containing BER measurement updates, the number of errors, and the total number of bit transmissions. Display the channel BER, the coded BER and the total number of errors corrected by the RS decoder.
chanBitErrorRate = chanErrorStats(1)
chanBitErrorRate = 0.0015
codedBitErrorRate = codedErrorStats(1)
codedBitErrorRate = 0
totalCorrectedErrors = correctedErrors
totalCorrectedErrors = 632
To run the simulation for a set of ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values, you can add a for
loop around the processing loop above. For a simulation with uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values in the range 4:15 dB, a target number of errors equal to 5000, and maximum number of transmissions equal to $50\times {10}^{6}$, the figure compares results for:
RS coding with only erasures
RS coding with erasures and puncturing
Theoretical BER for 64QAM
The coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ is slightly higher than the channel ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$, so the channel BER is slightly better in the punctured case. On the other hand, the coded BER is worse in the punctured case because the two punctures reduce the error correcting capability of the code by one, leaving it able to correct only (10 – 6 – 2) / 2 = 1 error per codeword.
Help Functions
ReedSolomon Coding with Erasures, Punctures, and Shortening
This example shows how to configure the comm.RSEncoder
and comm.RSDecoder
System objects to perform ReedSolomon (RS) block coding that shortens a (63,53) code to a (28,18) code. You simulate a communications system that consists of a random source, an RS encoder, a 64QAM modulator, an AWGN channel, a 64QAM demodulator, and an RS decoder.
In this example, you analyze the effects of RS coding with erasures, puncturing, and shortening by comparing the channel bit error rate (BER) performance versus the coded BER performance. You obtain the channel BER by comparing inputs for the QAM modulator to outputs from the QAM demodulator and the coded BER by comparing inputs for the RS encoder to outputs from the RS decoder. Puncturing is the removal of parity symbols from a codeword, and shortening is the removal of message symbols from a codeword. Puncturing has the benefit of making the code rate more flexible, at the expense of some error correction capability. Shortened codes achieve the same code rate flexibility without degrading the error correction performance for the same demodulator input ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$.
Initialization
The helperRSCodingConfig.m helper function initializes the simulation parameters and configures the comm.AWGNChannel
and comm.ErrorRate
System objects that simulate the communications system. The helper function also sets the uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio (EbNoUncoded
) to 15 dB and the simulation stop criteria for 500 errors or a maximum $5\times {10}^{6}$ bits are transmitted.
helperRSCodingConfig;
Configure RS Encoder/Decoder
This example uses a (63,53) RS code operating with a 64QAM modulation scheme. The RS coding operation includes erasures, puncturing, and code shortening. This example shows how to shorten the (63,53) code to a (28,18) code.
To shorten a (63,53) code by 10 symbols to a (53,43) code, you can simply enter 53 and 43 for the CodewordLength
and MessageLength
properties, respectively (since $2\lceil {\mathrm{log}}_{2}\left(53+1\right)\rceil 1=63$). However, to shorten it by 35 symbols to a (28,18) code, you must explicitly specify that the symbols belong to the Galois field GF(26). Otherwise, the RS System objects assume that the code is shortened from a (31,21) code (since $2\lceil {\mathrm{log}}_{2}\left(28+1\right)\rceil 1=31$).
Create a pair of comm.RSEncoder
and comm.RSDecoder
System objects so that they perform block coding with a (28,18) code shortened from a (63,53) code that is configured to input and output integer symbols. Configure the decoder to accept an erasure input and two punctures. For each codeword at the output of the 64QAM demodulator, the receiver determines the six least reliable symbols by using the helperRSCodingGetErasures.m helper function. The indices that point to the location of these unreliable symbols are an input to the RS decoder.
N = 63; % Codeword length K = 53; % Message length S = 18; % Shortenened message length numErasures = 6; numPuncs = 2; rsEncoder = comm.RSEncoder(N,K, ... BitInput=false, ... PuncturePatternSource="Property", ... PuncturePattern = [ones(NKnumPuncs,1); zeros(numPuncs,1)]); rsDecoder = comm.RSDecoder(N,K, ... BitInput=false, ... ErasuresInputPort=true, ... PuncturePatternSource="Property", ... PuncturePattern = rsEncoder.PuncturePattern);
Set the shortened codeword length and message length values.
rsEncoder.ShortMessageLength = S; rsDecoder.ShortMessageLength = S;
Specify the field of ${\mathrm{GF}(2}^{6})$ in the RS encoder/decoder System objects, by setting the PrimitivePolynomialSource
property to 'Property'
and the PrimitivePolynomial
property to a sixth degree primitive polynomial.
primPolyDegree = 6; rsEncoder.PrimitivePolynomialSource = 'Property'; rsEncoder.PrimitivePolynomial = ... int2bit(primpoly(primPolyDegree,'nodisplay'),7)'; rsDecoder.PrimitivePolynomialSource = 'Property'; rsDecoder.PrimitivePolynomial = ... int2bit(primpoly(primPolyDegree,'nodisplay'),7)';
Stream Processing Loop
Simulate the communications system for an uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio of 15 dB. The uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ is the ratio that would be measured at the input of the channel if there were no coding in the system.
Since the signal going into the AWGN channel is the encoded signal, you must convert the uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values so that they correspond to the energy ratio at the encoder output. This ratio is the coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio. If you input K
symbols to the encoder and obtain N
output symbols, then the energy relation is given by the K/N
rate. Since the length of the codewords generated by the RS encoder is shortened and reduced by the number of punctures specified in the puncture pattern vector, the value of the coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio needs to be adjusted to account for the shortened code and the punctures. In this example, the number of output symbols is (N
– numPuncs
– S
) and the uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ ratio relates to the coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ as shown below. Set the EbNo
property of the AWGN channel object to the computed coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ value.
EbNoCoded = EbNoUncoded + 10*log10(S/(N  numPuncs  K + S)); channel.EbNo = EbNoCoded;
Loop the simulation until it reaches the target number of errors or the maximum number of transmissions.
chanErrorStats = zeros(3,1); codedErrorStats = zeros(3,1); correctedErrors = 0; while (codedErrorStats(2) < targetErrors) && ... (codedErrorStats(3) < maxNumTransmissions)
The data symbols transmit one message word at a time. Each message word has (K
– S
) symbols in the range [0, (2^primPolyDegree
) – 1].
data = randi([0 2^primPolyDegree1],S,1);
Encode the shortened message word. The encoded word encData
is (N
– numPuncs
– S
) symbols long.
encData = rsEncoder(data);
Modulate encoded data and add noise. Then demodulate channel output.
modData = qammod(encData,M); chanOutput = channel(modData); demodData = qamdemod(chanOutput,M);
Use the helperRSCodingGetErasures.m helper function to find the six least reliable symbols and generate an erasures vector. The length of the erasures vector must be equal to the number of symbols in the demodulated codeword. A one in the ith element of the vector erases the ith symbol in the codeword. Zeros in the vector indicate no erasures.
erasuresVec = helperRSCodingGetErasures(chanOutput,numErasures);
Decode the data. Log the number of errors corrected by the RS decoder.
[estData,errs] = rsDecoder(demodData,erasuresVec); if (errs >= 0) correctedErrors = correctedErrors + errs; end
When computing the channel and coded BERs, convert integers to bits.
chanErrorStats(:,1) = ... chanBERCalc(int2bit(encData,log2(M)), ... int2bit(demodData,log2(M))); codedErrorStats(:,1) = ... codedBERCalc(int2bit(data,log2(M)), ... int2bit(estData,log2(M))); end
The error rate measurement objects, chanBERCalc
and codedBERCalc
, output 3by1 vectors containing BER measurement updates, the number of errors, and the total number of bit transmissions. Display the channel BER, the coded BER, and the total number of errors corrected by the RS decoder.
chanBitErrorRate = chanErrorStats(1)
chanBitErrorRate = 0.0036
codedBitErrorRate = codedErrorStats(1)
codedBitErrorRate = 9.6599e05
totalCorrectedErrors = correctedErrors
totalCorrectedErrors = 1436
To run the simulation for a set of ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values, you can add a for
loop around the processing loop above. For a simulation with uncoded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values in the range 4:15 dB, a target number of errors equal to 5000, and maximum number of transmissions equal to $50\times {10}^{6}$, the figure compares results for:
RS coding with only erasures
RS coding with erasures and puncturing
RS coding with erasures, puncturing, and shortening
Theoretical BER for 64QAM
The BER out of the 64QAM demodulator is worse with shortening than it is without shortening. The code rate of the shortened code is much lower than the code rate of the nonshortened code. Therefore, the coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ into the demodulator is worse with shortening. A shortened code has the same error correcting capability as nonshortened code for the same ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$, but the reduction in ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ manifests in the form of a higher BER out of the RS decoder with shortening than without.
The coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ is slightly higher than the channel ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$, so the channel BER is slightly better than the coded BER in the shortened case. The degraded coded ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ occurs because the code rate of the shortened code is lower than that of the nonshortened code. Shortening results in degraded coded BER, most noticeably at lower ${\mathit{E}}_{\mathrm{b}}/{\mathit{N}}_{0}$ values.
Helper functions
Algorithms
This object implements ReedSolomon (RS) coding as described in [1] and Algorithms for BCH and RS Errorsonly Decoding.
Input and Output Signal Lengths in BCH and RS System Objects
The notation y = c * x
denotes that y
is an integer multiple of x
.
The number of punctures equals the number of zeros in the puncture vector.
M is the degree of the primitive polynomial. Each group of
M bits represents an integer in the range [0,
(2^{M}–1)] that belongs to the finite
Galois field gf
(2^{M}).
ShortMessageLengthSource  "Auto"  "Property" 

and
 Input Length:
Output Length:
 Input Length:
Output Length:

and
 Input Length:
Output Length:
Erasures Length:
 Input Length:
Output Length:
Erasures Length:

 Input Length:
Output Length:
 Input Length:
Output Length:

 Input Length:
Output Length:
Erasures Length:
 Input Length:
Output Length:
Erasures Length:

References
[1] Clark, George C., and J. Bibb Cain. ErrorCorrection Coding for Digital Communications. Applications of Communications Theory. New York: Plenum Press, 1981.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
See System Objects in MATLAB Code Generation (MATLAB Coder).
When you use this object to generate code, you must set the generator polynomial to a doubleprecision integer row vector.
Version History
Introduced in R2012a
See Also
Objects
Functions
Topics
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)