Error Detection and Correction

Cyclic Redundancy Check Codes

CRC-Code Features

Cyclic redundancy check (CRC) coding is an error-control coding
technique for detecting errors that occur when a message is transmitted.
Unlike block or convolutional codes, CRC codes do not have a built-in
error-correction capability. Instead, when a communications system
detects an error in a received message word, the receiver requests
the sender to retransmit the message word.

In CRC coding, the transmitter applies a rule to each message
word to create extra bits, called the checksum, or syndrome,
and then appends the checksum to the message word. After receiving
a transmitted word, the receiver applies the same rule to the received
word. If the resulting checksum is nonzero, an error has occurred,
and the transmitter should resend the message word.

Open the Error Detection and Correction library by double-clicking
its icon in the main Communications Toolbox™ block library. Open
the CRC sublibrary by double-clicking on its icon in the Error Detection
and Correction library.

CRC Non-Direct Algorithm

The CRC non-direct algorithm accepts a binary data vector, corresponding
to a polynomial M, and appends a checksum of r bits, corresponding
to a polynomial C. The concatenation of the input vector and the checksum
then corresponds to the polynomial T = M*xr + C,
since multiplying by xr corresponds
to shifting the input vector r bits to the left.
The algorithm chooses the checksum C so that T is
divisible by a predefined polynomial P of degree r,
called the generator polynomial.

The algorithm divides T by P,
and sets the checksum equal to the binary vector corresponding to
the remainder. That is, if T = Q*P + R,
where R is a polynomial of degree less than r,
the checksum is the binary vector corresponding to R.
If necessary, the algorithm prepends zeros to the checksum so that
it has length r.

The CRC generation feature, which implements the transmission
phase of the CRC algorithm, does the following:

Left shifts the input data vector by r bits
and divides the corresponding polynomial by P.

Sets the checksum equal to the binary vector of length r,
corresponding to the remainder from step 1.

Appends the checksum to the input data vector. The
result is the output vector.

The CRC detection feature computes the checksum for its entire
input vector, as described above.

Note

The implementation described in this section is one of many
valid implementations of the CRC algorithm. Different implementations
can yield different numerical results.

Bits enter the linear feedback shift register (LFSR) from the
lowest index bit to the highest index bit. The sequence of input
message bits represents the coefficients of a message polynomial in
order of decreasing powers. The message vector is augmented with r zeros
to flush out the LFSR, where r is the degree of
the generator polynomial. If the output from the leftmost register
stage d(1) is a 1, then the bits in the shift register are XORed with
the coefficients of the generator polynomial. When the augmented message
sequence is completely sent through the LFSR, the register contains
the checksum [d(1) d(2) . . . d(r)]. This is an implementation of
binary long division, in which the message sequence is the divisor
(numerator) and the polynomial is the dividend (denominator). The
CRC checksum is the remainder of the division operation.

Example Using CRC Non-Direct Algorithm

Suppose the input frame is [1 1 0 0 1 1 0]',
corresponding to the polynomial M = x6 +x5 + x2 + x,
and the generator polynomial is P = x3 + x2 +
1, of degree r = 3. By polynomial
division, M*x3 =
(x6 + x3 + x)*P
+ x. The remainder is R = x,
so that the checksum is then [0 1 0]'. An extra
0 is added on the left to make the checksum have length 3.

CRC Direct Algorithm

The initial step of the direct CRC encoding occurs with the
three switches in position X. The algorithm feeds k message
bits to the encoder. These bits are the first k bits
of the code word output. Simultaneously, the algorithm sends k bits
to the linear feedback shift register (LFSR). When the system completely
feeds the kth message bit to the LFSR, the switches
move to position Y. Here, the LFSR contains the mathematical remainder
from the polynomial division. These bits are shifted out of the LFSR
and they are the remaining bits (checksum) of the code word output.

Block-Coding Features

Error-control coding techniques detect, and possibly correct, errors that
occur when messages are transmitted in a digital communication system. To
accomplish this, the encoder transmits not only the information symbols but also
extra redundant symbols. The decoder interprets what it receives, using the
redundant symbols to detect and possibly correct whatever errors occurred during
transmission. You might use error-control coding if your transmission channel is
very noisy or if your data is very sensitive to noise. Depending on the nature
of the data or noise, you might choose a specific type of error-control
coding.

Block coding is a special case of error-control coding. Block-coding
techniques map a fixed number of message symbols to a fixed number of code
symbols. A block coder treats each block of data independently and is a
memoryless device. Communications Toolbox contains block-coding capabilities by providing Simulink blocks, System objects, and MATLAB functions.

The class of block-coding techniques includes categories shown in the diagram
below.

Communications Toolbox supports general linear block codes. It also process cyclic, BCH,
Hamming, and Reed-Solomon codes (which are all special kinds of linear block
codes). Blocks in the product can encode or decode a message using one of the
previously mentioned techniques. The Reed-Solomon and BCH decoders indicate how
many errors they detected while decoding. The Reed-Solomon coding blocks also
let you decide whether to use symbols or bits as your data.

Note

The blocks and functions in Communications Toolbox are designed for error-control codes that use an alphabet
having 2 or 2m symbols.

Communications Toolbox Support Functions. Functions in Communications Toolbox can support simulation blocks by

Determining characteristics of a technique, such as
error-correction capability or possible message lengths

Performing lower-level computations associated with a technique,
such as

Computing a truth table

Computing a generator or parity-check matrix

Converting between generator and parity-check
matrices

Computing a generator polynomial

For more information about error-control coding capabilities, see Block Codes.

Terminology

Throughout this section, the information to be encoded consists of
message symbols and the code that is produced consists
of codewords.

Each block of K message symbols is encoded into a codeword
that consists of N message symbols. K is
called the message length, N is called the codeword length,
and the code is called an [N,K]
code.

Data Formats for Block Coding

Each message or codeword is an ordered grouping of symbols. Each block in the
Block Coding sublibrary processes one word in each time step, as described in
the following section, Binary Format (All Coding Methods). Reed-Solomon coding blocks also
let you choose between binary and integer data, as described in Integer Format (Reed-Solomon Only).

Binary Format (All Coding Methods). You can structure messages and codewords as binary
vector signals, where each vector represents a
message word or a codeword. At a given time, the encoder receives an entire
message word, encodes it, and outputs the entire codeword. The message and
code signals operate over the same sample time.

This example illustrates the encoder receiving a four-bit message and
producing a five-bit codeword at time 0. It repeats this process with a new
message at time 1.

For all coding techniques except Reed-Solomon using
binary input, the message vector must have length K and the corresponding
code vector has length N. For Reed-Solomon codes with binary input, the
symbols for the code are binary sequences of length M, corresponding to
elements of the Galois field GF(2M). In this
case, the message vector must have length M*K and the corresponding code
vector has length M*N. The Binary-Input RS Encoder block and the
Binary-Output RS Decoder block use this format for messages and
codewords.

If the input to a block-coding block is a frame-based vector, it must be a
column vector instead of a row vector.

To produce sample-based messages in the binary format, you can configure
the Bernoulli Binary Generator block
so that its Probability of a zero parameter is a vector
whose length is that of the signal you want to create. To produce
frame-based messages in the binary format, you can configure the same block
so that its Probability of a zero parameter is a scalar
and its Samples per frame parameter is the length of
the signal you want to create.

Using Serial Signals

If you prefer to structure messages and codewords as scalar signals, where
several samples jointly form a message word or codeword, you can use the
Buffer and Unbuffer blocks. Buffering
involves latency and multirate processing. If your model computes error
rates, the initial delay in the coding-buffering combination influences the
Receive delay parameter in the Error Rate Calculation block. If
you are unsure about the sample times of signals in your model, click the
Display menu and select Sample
Time > Colors.
Alternatively, you can attach Probe blocks to connector
lines to help evaluate sample timing, buffering and delays.

Integer Format (Reed-Solomon Only). A message word for an [N,K] Reed-Solomon code consists of M*K bits, which
you can interpret as K symbols from 0 to 2M. The
symbols are binary sequences of length M, corresponding to elements of the
Galois field GF(2M), in descending order of
powers. The integer format for Reed-Solomon codes lets you structure
messages and codewords as integer signals instead of
binary signals. (The input must be a frame-based column vector.)

Note

In this context, Simulink expects the first bit
to be the most significant bit in the symbol. “First”
means the smallest index in a vector or the smallest time for a series
of scalars.

The following figure illustrates the equivalence between binary and
integer signals for a Reed-Solomon encoder. The case for the decoder is
similar.

To produce sample-based messages in the integer format, you can configure
the Random Integer Generator block so that M-ary number
and Initial seed parameters are vectors of
the desired length and all entries of the M-ary number
vector are 2M. To produce frame-based messages in
the integer format, you can configure the same block so that its
M-ary number and Initial seed
parameters are scalars and its Samples per frame
parameter is the length of the signal you want to create.

Using Block Encoders and Decoders Within a Model

Once you have configured the coding blocks, a few tips can help you place them
correctly within your model:

If a block has multiple outputs, the first one is always the stream of
coding data.

The Reed-Solomon and BCH blocks have an error counter as a second
output.

Be sure that the signal sizes are appropriate for the mask parameters.
For example, if you use the Binary Cyclic Encoder block and set
Message length K to 4,
the input signal must be a vector of length 4.

If you are unsure about the size of signals in your model, clicking
the Display menu select Signals and
Ports >Signal
Dimension.

Examples of Block Coding

Example: Reed-Solomon Code in Integer Format. This example uses a Reed-Solomon code in integer format. It illustrates
the appropriate vector lengths of the code and message signals for the
coding blocks. It also exhibits error correction, using a simple way of
introducing errors into each codeword.

Open the model by typing
doc_rscoding at the MATLAB command line. To build
the model, gather and configure these blocks:

Connect the blocks as in the preceding figure. From the
Simulation menu of the model window, select
Model Configuration Parameters. In the
Configuration Parameters dialog box, set Stop Time to
500.

The vector length numbers appear on the connecting lines only if you click
the Display menu and select Signals &
Ports > Signal Dimensions.
The encoder accepts a vector of length 5 (which is K in this case) and
produces a vector of length 15 (which is N in this case). The decoder does
the opposite.

Running the model produces the following scope images. The plotted error
count will vary based on the Initial Seed value used in
the Random Integer Generator block. (To make the axis range exactly match
that of the first scope, right-click the plot area in the scope and select
Axes Properties.)

Number of Errors Before Correction

The second plot is the number of errors that the decoder detected while
trying to recover the message. Often the number is five because the Gain
block replaces the first five symbols in each codeword with zeros. However,
the number of errors is less than five whenever a correct codeword contains
one or more zeros in the first five places.

The first plot is the difference between the original message and the
recovered message; since the decoder was able to correct all errors that
occurred, each of the five data streams in the plot is zero.

Notes on Specific Block-Coding Techniques

Although the Block Coding sublibrary is somewhat uniform in its look and feel,
the various coding techniques are not identical. This section describes special
options and restrictions that apply to parameters and signals for the coding
technique categories in this sublibrary. Coding techniques discussed below
include - Generic Linear Block code, Cyclic code, Hamming code, BCH code, and
Reed-Solomon code.

Generic Linear Block
Codes

Encoding a message using a generic linear block code requires a generator
matrix. Decoding the code requires the generator matrix and possibly a truth
table. To use the Binary Linear Encoder and Binary Linear Decoder blocks, you
must understand the Generator matrix and
Error-correction truth table parameters.

Generator Matrix - The process of encoding a message into
an [N,K] linear block code is determined by a K-by-N generator matrix
G. Specifically, a 1-by-K message vector
v is encoded into the 1-by-N codeword vector
vG. If G has the form
[Ik, P] or
[P, Ik],
where P is some K-by-(N-K) matrix and
Ik is the
K-by-K identity matrix, G is said
to be in standard form. (Some authors, such as Clark and
Cain [2], use the first standard form, while others, such as Lin and Costello [3], use the second.) The linear block-coding blocks in this product require the
Generator matrix mask parameter to be in standard
form.

Decoding Table - A decoding table tells a decoder how to
correct errors that may have corrupted the code during transmission. Hamming
codes can correct any single-symbol error in any codeword. Other codes can
correct, or partially correct, errors that corrupt more than one symbol in a
given codeword.

The Binary Linear Decoder block allows
you to specify a decoding table in the Error-correction truth
table parameter. Represent a decoding table as a matrix with N
columns and 2N-K rows. Each row gives a correction
vector for one received codeword vector.

You can avoid specifying a decoding table explicitly, by setting the
Error-correction truth table parameter to
0. When Error-correction truth table
is 0, the block computes a decoding table using the syndtable function.

Cyclic Codes

For cyclic codes, the codeword length N must have the form
2M-1, where M is an integer greater than or equal
to 3.

Generator Polynomials - Cyclic codes have special
algebraic properties that allow a polynomial to determine the coding process
completely. This so-called generator polynomial is a degree-(N-K) divisor of the
polynomial xN-1. Van Lint [5] explains how a generator polynomial determines a cyclic code.

The Binary Cyclic Encoder and Binary Cyclic Decoder blocks allow
you to specify a generator polynomial as the second mask parameter, instead of
specifying K there. The blocks represent a generator polynomial using a vector
that lists the coefficients of the polynomial in order of
ascending powers of the variable. You can find
generator polynomials for cyclic codes using the cyclpoly function.

If you do not want to specify a generator polynomial, set the second mask
parameter to the value of K.

Hamming Codes

For Hamming codes, the codeword length N must have the form
2M-1, where M is an integer greater than or equal
to 3. The message length K must equal N-M.

Primitive Polynomials - Hamming codes rely on algebraic
fields that have 2M elements (or, more generally,
pM elements for a prime
number p). Elements of such fields are named
relative to a distinguished element of the field that
is called a primitive element. The minimal polynomial of a
primitive element is called a primitive polynomial. The
Hamming Encoder and Hamming Decoder blocks allow you to
specify a primitive polynomial for the finite field that they use for
computations. If you want to specify this polynomial, do so in the second mask
parameter field. The blocks represent a primitive polynomial using a vector that
lists the coefficients of the polynomial in order of
ascending powers of the variable. You can find
generator polynomials for Galois fields using the gfprimfd function.

If you do not want to specify a primitive polynomial, set the second mask
parameter to the value of K.

BCH Codes

BCH codes are cyclic error-correcting codes that are constructed using finite
fields. For these codes, the codeword length N must have the form
2M-1, where M is an integer from 3 to 9. The
message length K is restricted to particular values that depend on N. To see
which values of K are valid for a given N, see the comm.BCHEncoder
System
object™ reference page. No known analytic formula describes the
relationship among the codeword length, message length, and error-correction
capability for BCH codes.

m_i(x) represents the minimum polynomial corresponding to
αi, α is a root of the default primitive
polynomial for the field GF(n+1),

and t represents the error-correcting capability of the code.

Reed-Solomon Codes

Reed-Solomon codes are useful for correcting errors that occur in bursts. In
the simplest case, the length of codewords in a Reed-Solomon code is of the form
N= 2M-1, where the 2M is
the number of symbols for the code. The error-correction capability of a
Reed-Solomon code is floor((N-K)/2), where K is the length of
message words. The difference N-K must be even.

It is sometimes convenient to use a shortened Reed-Solomon code in which N is
less than 2M-1. In this case, the encoder appends
2M-1-N zero symbols to each message word and
codeword. The error-correction capability of a shortened Reed-Solomon code is
also floor((N-K)/2). The Communications Toolbox Reed-Solomon blocks can implement shortened Reed-Solomon
codes.

Effect of Nonbinary Symbols - One difference between
Reed-Solomon codes and the other codes supported in this product is that
Reed-Solomon codes process symbols in GF(2M) instead
of GF(2). M bits specify each symbol. The nonbinary nature of the Reed-Solomon
code symbols causes the Reed-Solomon blocks to differ from other coding blocks
in these ways:

The binary format expects the vector lengths to be an integer multiple
of M*K (not K) for messages and the same integer M*N (not N) for
codewords.

Error Information - The Reed-Solomon decoding blocks
(Binary-Output RS Decoder and Integer-Output RS Decoder) return
error-related information during the simulation. The second output signal
indicates the number of errors that the block detected in the input codeword. A
-1 in the second output indicates that the block detected more errors than it
could correct using the coding scheme.

Shortening, Puncturing, and Erasures

A punctured codeword has only parity symbols removed, and
a shortened codeword has only information symbols removed.
A codeword with erasures can have those erasures in either information symbols
or parity symbols.

Reed Solomon Examples with Shortening, Puncturing, and
Erasures

In this section, a representative example of Reed Solomon coding with
shortening, puncturing, and erasures is built with increasing complexity of
error correction.

Encoder Example with Shortening and
Puncturing

The following figure shows a representative example of a (7,3) Reed Solomon
encoder with shortening and puncturing.

In this figure, the message source outputs two information symbols, designated
by I1I2. (For a BCH example, the
symbols are binary bits.) Because the code is a shortened (7,3) code, a zero
must be added ahead of the information symbols, yielding a three-symbol message
of 0I1I2. The modified message
sequence is RS encoded, and the added information zero is then removed, which
yields a result of
I1I2P1P2P3P4.
(In this example, the parity bits are at the end of the codeword.)

The puncturing operation is governed by the puncture vector, which, in this
case, is 1011. Within the puncture vector, a 1 means that the
symbol is kept, and a 0 means that the symbol is thrown away.
In this example, the puncturing operation removes the second parity symbol,
yielding a final vector of
I1I2P1P3P4.

Decoder Example with Shortening and
Puncturing

The following figure shows how the RS decoder operates on a shortened and
punctured codeword.

This case corresponds to the encoder operations shown in the figure of the RS
encoder with shortening and puncturing. As shown in the preceding figure, the
encoder receives a (5,2) codeword, because it has been shortened from a (7,3)
codeword by one symbol, and one symbol has also been punctured.

As a first step, the decoder adds an erasure, designated by E, in the second
parity position of the codeword. This corresponds to the puncture vector 1011.
Adding a zero accounts for shortening, in the same way as shown in the preceding
figure. The single erasure does not exceed the erasure-correcting capability of
the code, which can correct four erasures. The decoding operation results in the
three-symbol message DI1I2. The
first symbol is truncated, as in the preceding figure, yielding a final output
of I1I2.

Decoder Example with Shortening, Puncturing, and
Erasures

The following figure shows the decoder operating on the punctured, shortened
codeword, while also correcting erasures generated by the receiver.

In this figure, demodulator receives the
I1I2P1P3P4
vector that the encoder sent. The demodulator declares that two of the five
received symbols are unreliable enough to be erased, such that symbols 2 and 5
are deemed to be erasures. The 01001 vector, provided by an external source,
indicates these erasures. Within the erasures vector, a 1 means that the symbol
is to be replaced with an erasure symbol, and a 0 means that the symbol is
passed unaltered.

The decoder blocks receive the codeword and the erasure vector, and perform
the erasures indicated by the vector 01001. Within the erasures vector, a 1
means that the symbol is to be replaced with an erasure symbol, and a 0 means
that the symbol is passed unaltered. The resulting codeword vector is
I1EP1P3E,
where E is an erasure symbol.

The codeword is then depunctured, according to the puncture vector used in the
encoding operation (i.e., 1011). Thus, an erasure symbol is inserted between
P1 and P3, yielding a codeword
vector of
I1EP1EP3E.

Just prior to decoding, the addition of zeros at the beginning of the
information vector accounts for the shortening. The resulting vector is
0I1EP1EP3E,
such that a (7,3) codeword is sent to the Berlekamp algorithm.

This codeword is decoded, yielding a three-symbol message of
DI1I2 (where D refers to a
dummy symbol). Finally, the removal of the D symbol from the message vector
accounts for the shortening and yields the original
I1I2 vector.

Some pairs of message length and codeword length do not uniquely determine the
generator polynomial. The syntaxes for functions in the example above also
include options for retrieving generator polynomials that satisfy certain
constraints that you specify. See the functions' reference pages for details
about syntax options.

Algebraic Expression for Generator
Polynomials

The generator polynomials produced by bchgenpoly and
rsgenpoly have the form
(X - Ab)(X - Ab+1)...(X - Ab+2t-1),
where A is a primitive element for an appropriate Galois field, and b and t are
integers. See the functions' reference pages for more information about this
expression.

Performing Other Block Code Tasks

This section describes functions that compute typical parameters associated
with linear block codes, as well as functions that convert information from one
format to another.

Error Correction Versus Error Detection for
Linear Block Codes

You can use a linear block code to detect
dmin -1 errors or to
correct t = [12(dmin−1)] errors.

If you compromise the error correction capability of a code, you
can detect more than t errors. For example, a
code with dmin = 7 can
correct t = 3 errors or it can detect up to 4
errors and correct up to 2 errors.

Finding the Error-Correction
Capability

The bchgenpoly and
rsgenpoly functions can return an optional
second output argument that indicates the error-correction
capability of a BCH or Reed-Solomon code. For example, the
commands

[g,t] = bchgenpoly(31,16);
t
t =
3

find that a [31, 16] BCH code can correct up to three errors in
each codeword.

Finding Generator and Parity-Check
Matrices

To find a parity-check and generator matrix for a Hamming code
with codeword length 2^m-1, use the
hammgen function as below.
m must be at least three.

[parmat,genmat] = hammgen(m); % Hamming

To find a parity-check and generator matrix for a cyclic code, use
the cyclgen function. You must provide the
codeword length and a valid generator polynomial. You can use the
cyclpoly function to produce one possible
generator polynomial after you provide the codeword length and
message length. For example,

[parmat,genmat] = cyclgen(7,cyclpoly(7,4)); % Cyclic

Converting Between Parity-Check and
Generator Matrices

The gen2par function converts a generator
matrix into a parity-check matrix, and vice versa. The reference
page for gen2par contains examples to
illustrate this.

Convolutional Code Features

Convolutional coding is a special case of error-control coding. Unlike a block
coder, a convolutional coder is not a memoryless device. Even though a
convolutional coder accepts a fixed number of message symbols and produces a
fixed number of code symbols, its computations depend not only on the current
set of input symbols but on some of the previous input symbols.

Communications Toolbox provides convolutional coding capabilities as Simulink blocks, System objects, and MATLAB functions. This product supports feedforward and feedback
convolutional codes that can be described by a trellis structure or a set of
generator polynomials. It uses the Viterbi algorithm to implement hard-decision
and soft-decision decoding.

The product also includes an a posteriori probability
decoder, which can be used for soft output decoding of convolutional
codes.

To process convolutional codes, use the Convolutional Encoder, Viterbi Decoder, and/or APP Decoder blocks in the
Convolutional sublibrary. If a mask parameter is required in both the encoder
and the decoder, use the same value in both blocks.

The blocks in the Convolutional sublibrary assume that you use one of two
different representations of a convolutional encoder:

If you design your encoder using a diagram with shift registers and
modulo-2 adders, you can compute the code generator polynomial matrix
and subsequently use the poly2trellis function (in
Communications Toolbox) to generate the corresponding trellis structure mask
parameter automatically. For an example, see Design a Rate 2/3 Feedforward Encoder Using Simulink.

If you design your encoder using a trellis diagram, you can construct
the trellis structure in MATLAB and use it as the mask parameter.

To use the polynomial description with the Convolutional Encoder, Viterbi Decoder, or APP Decoder blocks, use the utility
function poly2trellis from Communications Toolbox. This function accepts a polynomial description and converts it
into a trellis description. For example, the following command computes the
trellis description of an encoder whose constraint length is 5 and whose
generator polynomials are 35 and 31:

trellis = poly2trellis(5,[35 31]);

To use this encoder with one of the convolutional-coding blocks, simply place
a poly2trellis command such as

poly2trellis(5,[35 31]);

in the Trellis structure parameter field.

Polynomial Description of a Convolutional Code

A polynomial description of a convolutional encoder describes the connections
among shift registers and modulo 2 adders. For example, the figure below depicts
a feedforward convolutional encoder that has one input, two outputs, and two
shift registers.

A polynomial description of a convolutional encoder has either two or three
components, depending on whether the encoder is a feedforward or feedback
type:

Constraint Lengths. The constraint lengths of the encoder form a vector whose length is the
number of inputs in the encoder diagram. The elements of this vector
indicate the number of bits stored in each shift register,
including the current input bits.

In the figure above, the constraint length is three. It is a scalar
because the encoder has one input stream, and its value is one plus the
number of shift registers for that input.

Generator Polynomials. If the encoder diagram has k inputs and n outputs, the code generator
matrix is a k-by-n matrix. The element in the ith row and jth column
indicates how the ith input contributes to the jth output.

For systematic bits of a systematic feedback encoder,
match the entry in the code generator matrix with the corresponding element
of the feedback connection vector. See Feedback Connection Polynomials below for details.

In other situations, you can determine the (i,j) entry in the matrix as
follows:

Build a binary number representation by placing a 1 in each spot
where a connection line from the shift register feeds into the
adder, and a 0 elsewhere. The leftmost spot in the binary number
represents the current input, while the rightmost spot represents
the oldest input that still remains in the shift register.

Convert this binary representation into an octal representation by
considering consecutive triplets of bits, starting from the
rightmost bit. The rightmost bit in each triplet is the least
significant. If the number of bits is not a multiple of three, place
zero bits at the left end as necessary. (For example, interpret
1101010 as 001 101 010 and convert it to 152.)

For example, the binary numbers corresponding to the upper and lower
adders in the figure above are 110 and 111, respectively. These binary
numbers are equivalent to the octal numbers 6 and 7, respectively, so the
generator polynomial matrix is [6 7].

Note

You can perform the binary-to-octal conversion in MATLAB by using code
like str2num(dec2base(bin2dec('110'),8)).

Feedback Connection Polynomials. If you are representing a feedback encoder, you need a vector of feedback
connection polynomials. The length of this vector is the number of inputs in
the encoder diagram. The elements of this vector indicate the feedback
connection for each input, using an octal format. First build a binary
number representation as in step 1 above. Then convert the binary
representation into an octal representation as in step 2 above.

If the encoder has a feedback configuration and is also systematic, the
code generator and feedback connection parameters corresponding to the
systematic bits must have the same values.

For example, the diagram below shows a rate 1/2 systematic encoder with
feedback.

This encoder has a constraint length of 5, a generator polynomial matrix
of [37 33], and a feedback connection polynomial of 37.

The first generator polynomial matches the feedback connection polynomial
because the first output corresponds to the systematic bits. The feedback
polynomial is represented by the binary vector [1 1 1 1 1], corresponding to
the upper row of binary digits in the diagram. These digits indicate
connections from the outputs of the registers to the adder. The initial 1
corresponds to the input bit. The octal representation of the binary number
11111 is 37.

The second generator polynomial is represented by the binary vector [1 1 0
1 1], corresponding to the lower row of binary digits in the diagram.
The octal number corresponding to the binary number 11011 is 33.

Using the Polynomial Description in MATLAB. To use the polynomial description with the functions
convenc and vitdec, first
convert it into a trellis description using the
poly2trellis function. For example, the command
below computes the trellis description of the encoder pictured in the
section Polynomial
Description of a Convolutional Code.

trellis = poly2trellis(3,[6 7]);

The MATLAB structure trellis is a suitable input
argument for convenc and
vitdec.

Trellis Description of a Convolutional Code

A trellis description of a convolutional encoder shows how each possible input
to the encoder influences both the output and the state transitions of the
encoder. This section describes trellises, and how to represent
trellises in MATLAB, and gives an example of a MATLAB
trellis.

The figure below depicts a trellis for the convolutional encoder from the
previous section. The encoder has four states (numbered in binary from 00 to
11), a one-bit input, and a two-bit output. (The ratio of input bits to output
bits makes this encoder a rate-1/2 encoder.) Each solid arrow shows how the
encoder changes its state if the current input is zero, and each dashed arrow
shows how the encoder changes its state if the current input is one. The octal
numbers above each arrow indicate the current output of the encoder.

As an example of interpreting this trellis diagram, if the encoder is in the
10 state and receives an input of zero, it outputs the code symbol 3 and changes
to the 01 state. If it is in the 10 state and receives an input of one, it
outputs the code symbol 0 and changes to the 11 state.

Note that any polynomial description of a convolutional encoder is equivalent
to some trellis description, although some trellises have no corresponding
polynomial descriptions.

Specifying a Trellis in MATLAB. To specify a trellis in MATLAB, use a specific form of a MATLAB structure
called a trellis structure. A trellis structure must have five fields, as in
the table below.

Fields of a Trellis Structure for a Rate k/n Code

Field in Trellis
Structure

Dimensions

Meaning

numInputSymbols

Scalar

Number of input symbols to the
encoder: 2k

numOutputsymbols

Scalar

Number of output symbols from
the encoder: 2n

numStates

Scalar

Number of states in the encoder

nextStates

numStates-by-2k
matrix

Next states for all
combinations of current state and current input

outputs

numStates-by-2k
matrix

Outputs (in octal) for all
combinations of current state and current input

Note

While your trellis structure can have any name, its fields must have
the exact names as in the table. Field names are
case sensitive.

In the nextStates matrix, each entry is an integer
between 0 and numStates-1. The element in the ith row and
jth column denotes the next state when the starting state is i-1 and the
input bits have decimal representation j-1. To convert the input bits to a
decimal value, use the first input bit as the most significant bit (MSB).
For example, the second column of the nextStates matrix
stores the next states when the current set of input values is {0,...,0,1}.
To learn how to assign numbers to states, see the reference page for
istrellis.

In the outputs matrix, the element in the ith row and
jth column denotes the encoder's output when the starting state is i-1 and
the input bits have decimal representation j-1. To convert to decimal value,
use the first output bit as the MSB.

How to Create a MATLAB Trellis Structure. Once you know what information you want to put into each field, you can
create a trellis structure in any of these ways:

Define each of the five fields individually, using
structurename.fieldname notation. For
example, set the first field of a structure called
s using the command below. Use additional
commands to define the other fields.

s.numInputSymbols = 2;

The reference page for the istrellis function
illustrates this approach.

Collect all field names and their values in a single
struct command. For example:

The number of input symbols is 2 because the trellis diagram has two types
of input path: the solid arrow and the dashed arrow. The number of output
symbols is 4 because the numbers above the arrows can be either 0, 1, 2, or
3. The number of states is 4 because there are four bullets on the left side
of the trellis diagram (equivalently, four on the right side). To compute
the matrix of next states, create a matrix whose rows correspond to the four
current states on the left side of the trellis, whose columns correspond to
the inputs of 0 and 1, and whose elements give the next states at the end of
the arrows on the right side of the trellis. To compute the matrix of
outputs, create a matrix whose rows and columns are as in the next states
matrix, but whose elements give the octal outputs shown above the arrows in
the trellis.

Create and Decode Convolutional Codes

The functions for encoding and decoding convolutional codes are
convenc and vitdec. This section
discusses using these functions to create and decode convolutional codes.

Encoding. A simple way to use convenc to create a convolutional
code is shown in the commands below.

Define a trellis.

t = poly2trellis([4 3],[4 5 17;7 4 2]);

Encode a vector of ones.

x = ones(100,1);
code = convenc(x,t);

The first command converts a polynomial description of a feedforward
convolutional encoder to the corresponding trellis description. The second
command encodes 100 bits, or 50 two-bit symbols. Because the code rate in
this example is 2/3, the output vector code contains 150
bits (that is, 100 input bits times 3/2).

To check whether your trellis corresponds to a catastrophic convolutional
code, use the iscatastrophic function.

Hard-Decision Decoding. To decode using hard decisions, use the vitdec
function with the flag 'hard' and with
binary input data. Because the output of
convenc is binary, hard-decision decoding can use
the output of convenc directly, without additional
processing. This example extends the previous example and implements
hard-decision decoding.

Define a trellis.

t = poly2trellis([4 3],[4 5 17;7 4 2]);

Encode a vector of ones.

code = convenc(ones(100,1),t);

Set the traceback length for decoding and decode using vitdec.

tb = 2;
decoded = vitdec(code,t,tb,'trunc','hard');

Verify that the decoded data is a vector of 100 ones.

isequal(decoded,ones(100,1))

ans = logical
1

Soft-Decision Decoding. To decode using soft decisions, use the vitdec
function with the flag 'soft'. Specify the number,
nsdec, of soft-decision bits and use input data
consisting of integers between 0 and 2^nsdec-1.

An input of 0 represents the most confident 0, while an input of
2^nsdec-1 represents the most confident 1. Other
values represent less confident decisions. For example, the table below
lists interpretations of values for 3-bit soft decisions.

Input Values for 3-bit Soft Decisions

Input Value

Interpretation

0

Most confident 0

1

Second most confident 0

2

Third most confident 0

3

Least confident 0

4

Least confident 1

5

Third most confident 1

6

Second most confident 1

7

Most confident 1

Implement Soft-Decision Decoding Using
MATLAB

The script below illustrates decoding with 3-bit soft decisions. First it
creates a convolutional code with convenc and adds
white Gaussian noise to the code with awgn. Then, to
prepare for soft-decision decoding, the example uses
quantiz to map the noisy data values to appropriate
decision-value integers between 0 and 7. The second argument in
quantiz is a partition vector that determines which
data values map to 0, 1, 2, etc. The partition is chosen so that values near
0 map to 0, and values near 1 map to 7. (You can refine the partition to
obtain better decoding performance if your application requires it.)
Finally, the example decodes the code and computes the bit error rate. When
comparing the decoded data with the original message, the example must take
the decoding delay into account. The continuous operation mode of
vitdec causes a delay equal to the traceback
length, so msg(1) corresponds to
decoded(tblen+1) rather than to
decoded(1).

Implement Soft-Decision Decoding Using Simulink. This example creates a rate 1/2 convolutional code. It uses a quantizer
and the Viterbi Decoder block to perform soft-decision decoding. To open the model, enter
doc_softdecision at the MATLAB command line. For
a description of the model, see Overview of
the Simulation.

Defining the Convolutional Code

The feedforward convolutional encoder in this example is depicted
below.

The encoder's constraint length is a scalar since the encoder has one
input. The value of the constraint length is the number of bits stored in
the shift register, including the current input. There are six memory
registers, and the current input is one bit. Thus the constraint length of
the code is 7.

The code generator is a 1-by-2 matrix of octal numbers because the encoder
has one input and two outputs. The first element in the matrix indicates
which input values contribute to the first output, and the second element in
the matrix indicates which input values contribute to the second
output.

For example, the first output in the encoder diagram is the modulo-2 sum
of the rightmost and the four leftmost elements in the diagram's array of
input values. The seven-digit binary number 1111001 captures this
information, and is equivalent to the octal number 171. The octal number 171
thus becomes the first entry of the code generator matrix. Here, each
triplet of bits uses the leftmost bit as the most significant bit. The
second output corresponds to the binary number 1011011, which is equivalent
to the octal number 133. The code generator is therefore
[171 133].

The Trellis structure parameter in the Convolutional
Encoder block tells the block which code to use when processing data. In
this case, the poly2trellis function, in
Communications Toolbox, converts the constraint length and the pair of octal numbers
into a valid trellis structure.

While the message data entering the Convolutional Encoder block is a
scalar bit stream, the encoded data leaving the block is a stream of binary
vectors of length 2.

Mapping the Received Data

The received data, that is, the output of the AWGN Channel block, consists
of complex numbers that are close to -1 and 1. In order to reconstruct the
original binary message, the receiver part of the model must decode the
convolutional code. The Viterbi Decoder block in this model expects its
input data to be integers between 0 and 7. The demodulator, a custom
subsystem in this model, transforms the received data into a format that the
Viterbi Decoder block can interpret properly. More specifically, the
demodulator subsystem

Converts the received data signal to a real signal by removing its
imaginary part. It is reasonable to assume that the imaginary part
of the received data does not contain essential information, because
the imaginary part of the transmitted data is zero (ignoring small
roundoff errors) and because the channel noise is not very
powerful.

Normalizes the received data by dividing by the standard deviation
of the noise estimate and then multiplying by -1.

Quantizes the normalized data using three bits.

The combination of this mapping and the Viterbi Decoder block's decision
mapping reverses the BPSK modulation that the BPSK Modulator Baseband block
performs on the transmitting side of this model. To examine the demodulator
subsystem in more detail, double-click the icon labeled Soft-Output BPSK
Demodulator.

Decoding the Convolutional Code

After the received data is properly mapped to length-2 vectors of 3-bit
decision values, the Viterbi Decoder block decodes it. The block uses a
soft-decision algorithm with 23 different input
values because the Decision type parameter is
Soft Decision and the Number of
soft decision bits parameter is 3.

Soft-Decision Interpretation of
Data

When the Decision type parameter is set to
Soft Decision, the Viterbi Decoder block
requires input values between 0 and 2b-1, where
b is the Number of soft decision
bits parameter. The block interprets 0 as the most confident
decision that the codeword bit is a 0 and interprets
2b-1 as the most confident decision that the
codeword bit is a 1. The values in between these extremes represent less
confident decisions. The following table lists the interpretations of the
eight possible input values for this example.

Decision Value

Interpretation

0

Most confident 0

1

Second most confident 0

2

Third most confident 0

3

Least confident 0

4

Least confident 1

5

Third most confident 1

6

Second most confident 1

7

Most confident 1

Traceback and Decoding Delay

The Traceback depth parameter in the Viterbi Decoder
block represents the length of the decoding delay. Typical values for a
traceback depth are about five or six times the constraint length, which
would be 35 or 42 in this example. However, some hardware implementations
offer options of 48 and 96. This example chooses 48 because that is closer
to the targets (35 and 42) than 96 is.

Delay in Received Data

The Receive delay parameter of the Error Rate Calculation block is
nonzero because a given message bit and its corresponding recovered bit are
separated in time by a nonzero amount of simulation time. The
Receive delay parameter tells the block which
elements of its input signals to compare when checking for errors.

In this case, the Receive delay value is equal to the Traceback depth
value (48).

Comparing Simulation Results with Theoretical
Results

This section describes how to compare the bit error rate in this
simulation with the bit error rate that would theoretically result from
unquantized decoding. The process includes these steps

Computing Theoretical Bounds for the Bit
Error Rate

To calculate theoretical bounds for the bit error rate
Pb of the
convolutional code in this model, you can use this estimate
based on unquantized-decision decoding:

Pb<∑d=f∞cdPd

In this estimate,
cd is the sum of
bit errors for error events of distance d,
and f is the free distance of the code. The
quantity Pd is the
pairwise error probability, given by

Pd=12erfc(dREbN0)

where R is the code rate of 1/2, and
erfc is the
MATLAB complementary error function, defined by

erfc(x)=2π∫x∞e−t2dt

Values for the coefficients
cd and the free
distance f are in published articles such
as Frenger, P., P. Orten, and T. Ottosson, "Convolution Codes
with Optimum Distance Spectrum," IEEE Communications
Letters, vol. 3, pp. 317-319, November 1999.
[3]. The free distance for this code is
f = 10.

The following commands calculate the values of
Pb for
Eb/N0
values in the range from 1 to 4, in increments of 0.5:

You can efficiently vary the simulation parameters by using
the sim function to
run the simulation from the MATLAB command line. For example,
the following code calculates the bit error rate at bit
energy-to-noise ratios ranging from 1 dB to 4 dB, in increments
of 0.5 dB. It collects all bit error rates from these
simulations in the matrix BERVec. It also
plots the bit error rates in a figure window along with the
theoretical bounds computed in the preceding code
fragment.

Note

First open the model by clicking here in the
MATLAB Help browser. Then execute these commands, which
might take a few minutes.

Note

The estimate for
Pb assumes
that the decoder uses unquantized data, that is, an
infinitely fine quantization. By contrast, the simulation in
this example uses 8-level (3-bit) quantization. Because of
this quantization, the simulated bit error rate is not quite
as low as the bound when the signal-to-noise ratio is
high.

Design a Rate-2/3 Feedforward Encoder Using MATLAB

The example below uses the rate 2/3 feedforward encoder depicted in this
schematic. The accompanying description explains how to determine the trellis
structure parameter from a schematic of the encoder and then how to perform
coding using this encoder.

Determining Coding Parameters. The convenc and vitdec functions
can implement this code if their parameters have the appropriate
values.

The encoder's constraint length is a vector of length 2 because the
encoder has two inputs. The elements of this vector indicate the number of
bits stored in each shift register, including the current input bits.
Counting memory spaces in each shift register in the diagram and adding one
for the current inputs leads to a constraint length of [5 4].

To determine the code generator parameter as a 2-by-3 matrix of octal
numbers, use the element in the ith row and jth column to indicate how the
ith input contributes to the jth output. For example, to compute the element
in the second row and third column, the leftmost and two rightmost elements
in the second shift register of the diagram feed into the sum that forms the
third output. Capture this information as the binary number 1011, which is
equivalent to the octal number 13. The full value of the code generator
matrix is [23 35 0; 0 5 13].

To use the constraint length and code generator parameters in the
convenc and vitdec functions,
use the poly2trellis function to convert those
parameters into a trellis structure. The command to do this is below.

convenc accepts a vector containing 2-bit symbols and
produces a vector containing 3-bit symbols, while
vitdec does the opposite. Also notice that
biterr ignores the first 68 elements of
decoded. That is, the decoding delay is 68, which is
the number of bits per symbol (2) of the recovered message times the
traceback depth value (34) in the vitdec function. The
first 68 elements of decoded are 0s, while subsequent
elements represent the decoded messages.

Design a Rate 2/3 Feedforward Encoder Using Simulink

This example uses the rate 2/3 feedforward convolutional encoder depicted in
the following figure. The description explains how to determine the coding
blocks' parameters from a schematic of a rate 2/3 feedforward encoder. This
example also illustrates the use of the Error Rate Calculation block with a
receive delay.

The encoder's constraint length is a vector of length 2 since the encoder
has two inputs. The elements of this vector indicate the number of bits
stored in each shift register, including the current input bits. Counting
memory spaces in each shift register in the diagram and adding one for the
current inputs leads to a constraint length of [5 4].

To determine the code generator parameter as a 2-by-3 matrix of octal
numbers, use the element in the ith row and jth column to indicate how the
ith input contributes to the jth output. For example, to compute the element
in the second row and third column, notice that the leftmost and two
rightmost elements in the second shift register of the diagram feed into the
sum that forms the third output. Capture this information as the binary
number 1011, which is equivalent to the octal number 13. The full value of
the code generator matrix is [27 33 0; 0 5 13].

To use the constraint length and code generator parameters in the
Convolutional Encoder and Viterbi Decoder blocks, use the
poly2trellis function to convert those parameters
into a trellis structure.

How to Simulate the Encoder. The following model simulates this encoder.

To open the completed
model, enter doc_convcoding at the MATLAB
command line. To build the model, gather and configure these blocks:

Drag the bottom edge of the icon to make the display big
enough for three entries.

Connect the blocks as in the figure. In the model window, select Simulation > Model Configuration parametersto open the Configuration Parameters dialog box. Set
Stop time to inf.

Notes on the Model. The matrix size annotations appear on the connecting lines only if you
click the Display menu and select
Signals & Ports > Signal
Dimensions. The encoder accepts a 2-by-1 column vector and
produces a 3-by-1 column vector, while the decoder does the opposite. The
Samples per frame parameter in the Bernoulli Binary
Generator block is 2 because the block must generate a message word of
length 2.

The Receive delay parameter in the Error Rate
Calculation block is 68, which is the vector length (2) of the recovered
message times the Traceback depth value (34) in the
Viterbi Decoder block. If you examine the transmitted and received signals
as matrices in the MATLAB workspace, you see that the first 34 rows of the
recovered message consist of zeros, while subsequent rows are the decoded
messages. Thus the delay in the received signal is 34 vectors of length 2,
or 68 samples.

Running the model produces display output consisting of three numbers: the
error rate, the total number of errors, and the total number of comparisons
that the Error Rate Calculation block makes during the simulation. (The
first two numbers vary depending on your Initial seed
values in the Bernoulli Binary Generator and Binary Symmetric Channel
blocks.) The simulation stops after 100 errors occur, because
Target number of errors is set to
100 in the Error Rate Calculation block. The error
rate is much less than 0.02, the Error
probability in the Binary Symmetric Channel block.

Puncture a Convolutional Code Using MATLAB

This example processes a punctured convolutional code. It begins by generating
30,000 random bits and encoding them using a rate-3/4 convolutional encoder with
a puncture pattern of [1 1 1 0 0 1]. The resulting vector contains 40,000 bits,
which are mapped to values of -1 and 1 for transmission. The punctured code,
punctcode, passes through an additive white Gaussian
noise channel. Then vitdec decodes the noisy vector using
the 'unquant' decision type.

Finally, the example computes the bit error rate and the number of bit
errors.

Implement a Systematic Encoder with Feedback Using Simulink

This section explains how to use the Convolutional Encoder block to implement
a systematic encoder with feedback. A code is systematic if
the actual message words appear as part of the codewords. The following diagram
shows an example of a systematic encoder.

To implement this encoder, set the Trellis structure
parameter in the Convolutional Encoder block to poly2trellis(5, [37
33], 37). This setting corresponds to

Constraint length: 5

Generator polynomial pair: [37 33]

Feedback polynomial: 37

The feedback polynomial is represented by the binary vector [1 1 1 1 1],
corresponding to the upper row of binary digits. These digits indicate
connections from the outputs of the registers to the adder. The initial 1
corresponds to the input bit. The octal representation of the binary number
11111 is 37.

To implement a systematic code, set the first generator polynomial to be the
same as the feedback polynomial in the Trellis structure
parameter of the Convolutional Encoder block. In this example, both polynomials
have the octal representation 37.

The second generator polynomial is represented by the binary vector [1 1 0
1 1], corresponding to the lower row of binary digits. The octal number
corresponding to the binary number 11011 is 33.

Overview of the Simulation. The model is in the following figure. To open the model, enter
doc_softdecision at the MATLAB command line. The
simulation creates a random binary message signal, encodes the message into
a convolutional code, modulates the code using the binary phase shift keying
(BPSK) technique, and adds white Gaussian noise to the modulated data in
order to simulate a noisy channel. Then, the simulation prepares the
received data for the decoding block and decodes. Finally, the simulation
compares the decoded information with the original message signal in order
to compute the bit error rate. The Convolutional encoder is configured as a
rate 1/2 encoder. For every 2 bits, the encoder adds another 2 redundant
bits. To accommodate this, and add the correct amount of noise, the
Eb/No (dB) parameter of the AWGN block is in effect
halved by subtracting 10*log10(2). The simulation ends after processing 100
bit errors or 107 message bits, whichever comes
first.

Defining the Convolutional Code. The feedforward convolutional encoder in this example is depicted
below.

The encoder's constraint length is a scalar since the encoder has one
input. The value of the constraint length is the number of bits stored in
the shift register, including the current input. There are six memory
registers, and the current input is one bit. Thus the constraint length of
the code is 7.

The code generator is a 1-by-2 matrix of octal numbers because the encoder
has one input and two outputs. The first element in the matrix indicates
which input values contribute to the first output, and the second element in
the matrix indicates which input values contribute to the second
output.

For example, the first output in the encoder diagram is the modulo-2 sum
of the rightmost and the four leftmost elements in the diagram's array of
input values. The seven-digit binary number 1111001 captures this
information, and is equivalent to the octal number 171. The octal number 171
thus becomes the first entry of the code generator matrix. Here, each
triplet of bits uses the leftmost bit as the most significant bit. The
second output corresponds to the binary number 1011011, which is equivalent
to the octal number 133. The code generator is therefore
[171 133].

The Trellis structure parameter in the Convolutional
Encoder block tells the block which code to use when processing data. In
this case, the poly2trellis function, in
Communications Toolbox, converts the constraint length and the pair of octal numbers
into a valid trellis structure.

While the message data entering the Convolutional Encoder block is a
scalar bit stream, the encoded data leaving the block is a stream of binary
vectors of length 2.

Mapping the Received Data. The received data, that is, the output of the AWGN Channel block, consists
of complex numbers that are close to -1 and 1. In order to reconstruct the
original binary message, the receiver part of the model must decode the
convolutional code. The Viterbi Decoder block in this model expects its
input data to be integers between 0 and 7. The demodulator, a custom
subsystem in this model, transforms the received data into a format that the
Viterbi Decoder block can interpret properly. More specifically, the
demodulator subsystem

Converts the received data signal to a real signal by removing its
imaginary part. It is reasonable to assume that the imaginary part
of the received data does not contain essential information, because
the imaginary part of the transmitted data is zero (ignoring small
roundoff errors) and because the channel noise is not very
powerful.

Normalizes the received data by dividing by the standard deviation
of the noise estimate and then multiplying by -1.

Quantizes the normalized data using three bits.

The combination of this mapping and the Viterbi Decoder block's decision
mapping reverses the BPSK modulation that the BPSK Modulator Baseband block
performs on the transmitting side of this model. To examine the demodulator
subsystem in more detail, double-click the icon labeled Soft-Output BPSK
Demodulator.

Decoding the Convolutional Code. After the received data is properly mapped to length-2 vectors of 3-bit
decision values, the Viterbi Decoder block decodes it. The block uses a
soft-decision algorithm with 23 different input
values because the Decision type parameter is
Soft Decision and the Number of
soft decision bits parameter is 3.

Soft-Decision Interpretation of
Data

When the Decision type parameter is set to
Soft Decision, the Viterbi Decoder block
requires input values between 0 and 2b-1, where
b is the Number of soft decision
bits parameter. The block interprets 0 as the most confident
decision that the codeword bit is a 0 and interprets
2b-1 as the most confident decision that the
codeword bit is a 1. The values in between these extremes represent less
confident decisions. The following table lists the interpretations of the
eight possible input values for this example.

Decision Value

Interpretation

0

Most confident 0

1

Second most confident 0

2

Third most confident 0

3

Least confident 0

4

Least confident 1

5

Third most confident 1

6

Second most confident 1

7

Most confident 1

Traceback and Decoding Delay

The Traceback depth parameter in the Viterbi Decoder
block represents the length of the decoding delay. Typical values for a
traceback depth are about five or six times the constraint length, which
would be 35 or 42 in this example. However, some hardware implementations
offer options of 48 and 96. This example chooses 48 because that is closer
to the targets (35 and 42) than 96 is.

Delay in Received Data. The Receive delay parameterof the Error Rate Calculation block is
nonzero because a given message bit and its corresponding recovered bit are
separated in time by a nonzero amount of simulation time. The
Receive delay parameter tells the block which
elements of its input signals to compare when checking for errors.

In this case, the Receive delay value is equal to the Traceback depth
value (48).

Comparing Simulation Results with Theoretical Results. This section describes how to compare the bit error rate in this
simulation with the bit error rate that would theoretically result from
unquantized decoding. The process includes a few steps, described in these
sections:

Computing Theoretical Bounds for the Bit Error
Rate

To calculate theoretical bounds for the bit error rate
Pb of the convolutional code
in this model, you can use this estimate based on unquantized-decision
decoding:

Pb<∑d=f∞cdPd

In this estimate, cd is the
sum of bit errors for error events of distance d, and
f is the free distance of the code. The quantity
Pd is the pairwise error
probability, given by

Pd=12erfc(dREbN0)

where R is the code rate of 1/2, and erfc is the MATLAB
complementary error function, defined by

erfc(x)=2π∫x∞e−t2dt

Values for the coefficients cd
and the free distance f are in published articles such
as Frenger, P., P. Orten, and Ottosson, “Convolutional Codes with
Optimum Distance Spectrum,” IEEE
Communications vol. 3, pp. 317-319, November 1999. The free
distance for this code is f = 10.

The following commands calculate the values of
Pb for
Eb/N0
values in the range from 1 to 4, in increments of 0.5:

You can efficiently vary the simulation parameters by using the sim function to run the
simulation from the MATLAB command line. For example, the following code
calculates the bit error rate at bit energy-to-noise ratios ranging from 1
dB to 4 dB, in increments of 0.5 dB. It collects all bit error rates from
these simulations in the matrix BERVec. It also plots the
bit error rates in a figure window along with the theoretical bounds
computed in the preceding code fragment.

Note

First open the model by clicking here in the MATLAB Help
browser. Then execute these commands, which might take a few
minutes.

Note

The estimate for Pb
assumes that the decoder uses unquantized data, that is, an infinitely
fine quantization. By contrast, the simulation in this example uses
8-level (3-bit) quantization. Because of this quantization, the
simulated bit error rate is not quite as low as the bound when the
signal-to-noise ratio is high.

Tailbiting Encoding Using Feedback Encoders

This example demonstrates Tailbiting encoding using feedback encoders. For
feedback encoders, the ending state depends on the entire block of data. To
accomplish tailbiting, you must calculate for a given information vector (of N
bits), the initial state, that leads to the same ending state after the block of
data is encoded.

This is achieved in two steps:

The first step is to determine the zero-state response for a given
block of data. The encoder starts in the all-zeros state. The whole
block of data is input and the output bits are ignored. After N
bits, the encoder is in a state
XN[zs]. From this state, we calculate the
corresponding initial state
X0 and initialize the
encoder with X0.

The second step is the actual encoding. The encoder starts with
the initial state X0, the
data block is input and a valid codeword is output which conforms to
the same state boundary condition.

Refer to [8] for a theoretical calculation of the initial state
X0 from
XN[zs] using state-space formulation. This is a
one-time calculation which depends on the block length and in practice could be
implemented as a look-up table. Here we determine this mapping table by
simulating all possible entries for a chosen trellis and block length.

To open the model,
type doc_mtailbiting_wfeedback at the MATLAB command
line.

function mapStValues = getMapping(blkLen, trellis)
% The function returns the mapping value for the given block
length and trellis to be used for determining the initial
state from the zero-state response.
% All possible combinations of the mappings
mapStValuesTab = perms(0:trellis.numStates-1);
% Loop over all the combinations of the mapping entries:
for i = 1:length(mapStValuesTab)
mapStValues = mapStValuesTab(i,:);
% Model parameterized for the Block length
sim('mtailbiting_wfeedback');
% Check the boundary condition for each run
% if ending and starting states match, choose that mapping set
if unique(out)==0
return
end
end

Selecting the returned mapStValues for the Table
data parameter of the Direct Lookup Table
(n-D) block in the Lookup subsystem will perform tailbiting
encoding for the chosen block length and trellis.

Use MATLAB to Create Messages and Codewords in Binary Vector
Format. Your messages and codewords can take the form of vectors containing 0s and
1s. For example, messages and codes might look like msg
and code in the lines below.

In this example, msg consists of 12 entries, which are
interpreted as three 4-digit (because k = 4)
messages. The resulting vector code comprises three
6-digit (because n = 6) codewords, which are
concatenated to form a vector of length 18. The parity bits are at the
beginning of each codeword.

Use MATLAB to Create Messages and Codewords in Binary Matrix
Format. You can organize coding information so as to emphasize the grouping of
digits into messages and codewords. If you use this approach, each message
or codeword occupies a row in a binary matrix. The example below illustrates
this approach by listing each 4-bit message on a distinct row in
msg and each 6-bit codeword on a distinct row in
code.

Note

In the binary matrix format, the message matrix must have
k columns. The corresponding code matrix has
n columns. The parity bits are at the beginning
of each row.

Use MATLAB to Create Messages and Codewords in Decimal Vector
Format. Your messages and codewords can take the form of vectors containing
integers. Each element of the vector gives the decimal representation of the
bits in one message or one codeword.

Note

If 2^n or 2^k is very large, you
should use the default binary format instead of the decimal format. This
is because the function uses a binary format internally, while the
roundoff error associated with converting many bits to large decimal
numbers and back might be substantial.

Note

When you use the decimal vector format, encode
expects the leftmost bit to be the least
significant bit.

The syntax for the encode command must mention the
decimal format explicitly, as in the example below. Notice that
/decimal is appended to the fourth argument in the
encode command.

Note

The three examples above used cyclic coding. The formats for messages
and codes are similar for Hamming and generic linear block codes.

Configure Parameters for Linear Block Codes

This subsection describes the items that you might need in order
to process [n,k] cyclic, Hamming, and generic linear block codes.
The table below lists the items and the coding techniques for which
they are most relevant.

Generator Matrix. The process of encoding a message into an [n,k] linear block code is
determined by a k-by-n generator matrix G. Specifically, the 1-by-k message
vector v is encoded into the 1-by-n codeword vector vG. If G has the form
[Ik P] or
[P Ik], where P is some k-by-(n-k) matrix and
Ik is the k-by-k identity matrix, G is said to be
in standard form. (Some authors, e.g., Clark and Cain
[2], use the first standard form, while others, e.g., Lin and Costello [3], use the second.) Most functions in this toolbox assume that a generator
matrix is in standard form when you use it as an input argument.

Parity-Check Matrix. Decoding an [n,k] linear block code requires an (n-k)-by-n parity-check
matrix H. It satisfies
GHtr= 0 (mod
2), where Htr denotes the matrix transpose of H,
G is the code's generator matrix, and this zero matrix is k-by-(n-k). If
G = [Ik P] then
H = [-Ptr In-k].
Most functions in this product assume that a parity-check matrix is in
standard form when you use it as an input argument.

The table below summarizes the standard forms of the generator and
parity-check matrices for an [n,k] binary linear block code.

Type of
Matrix

Standard
Form

Dimensions

Generator

[Ik P]
or [P Ik]

k-by-n

Parity-check

[-P'
In-k] or
[In-k -P'
]

(n-k)-by-n

Ik is the identity matrix of size k and the
' symbol indicates matrix transpose. (For
binary codes, the minus signs in the parity-check
form listed above are irrelevant; that is, -1 = 1 in the binary
field.)

Examples

In the command below, parmat is a parity-check matrix
and genmat is a generator matrix for a Hamming code in
which
[n,k] = [23-1, n-3] = [7,4].
genmat has the standard form
[P Ik].

The same function gen2par can also convert a
parity-check matrix into a generator matrix.

Generator Polynomial. Cyclic codes have algebraic properties that allow a polynomial to
determine the coding process completely. This so-called generator
polynomial is a degree-(n-k) divisor of the polynomial
xn-1. Van Lint [5] explains how a generator polynomial determines a cyclic code.

The cyclpoly function produces generator polynomials
for cyclic codes. cyclpoly represents a generator
polynomial using a row vector that lists the polynomial's coefficients in
order of ascending powers of the variable. For example,
the command

Decoding Table. A decoding table tells a decoder how to correct errors that might have
corrupted the code during transmission. Hamming codes can correct any
single-symbol error in any codeword. Other codes can correct, or partially
correct, errors that corrupt more than one symbol in a given
codeword.

This toolbox represents a decoding table as a matrix with
n columns and 2^(n-k) rows. Each
row gives a correction vector for one received codeword vector. A Hamming
decoding table has n+1 rows. The
syndtable function generates a decoding table for a
given parity-check matrix.

Use a Decoding Table in MATLAB

The script below shows how to use a Hamming decoding table to correct an
error in a received message. The hammgen function
produces the parity-check matrix, while the syndtable
function produces the decoding table. The transpose of the parity-check
matrix is multiplied on the left by the received codeword, yielding the
syndrome. The decoding table helps determine the
correction vector. The corrected codeword is the sum (modulo 2) of the
correction vector and the received codeword.

Create and Decode Linear Block Codes

The functions for encoding and decoding cyclic, Hamming, and generic linear
block codes are encode and decode.
This section discusses how to use these functions to create and decode generic linear block
codes, cyclic codes, and
Hamming
codes.

Generic Linear Block Codes. Encoding a message using a generic linear block code requires a generator
matrix. If you have defined variables msg,
n, k, and
genmat, either of the commands

encodes the information in msg using the
[n,k] code that the generator
matrix genmat determines. The /decimal
option, suitable when 2^n and 2^k are
not very large, indicates that msg contains nonnegative
decimal integers rather than their binary representations. See Represent Words for Linear Block Codes or the reference page
for encode for a description of
the formats of msg and code.

Decoding the code requires the generator matrix and possibly a decoding
table. If you have defined variables code,
n, k, genmat,
and possibly also trt, then the commands

decode the information in code, using the
[n,k] code that the generator
matrix genmat determines. decode
also corrects errors according to instructions in the decoding table that
trt represents.

Example: Generic Linear Block
Coding

The example below encodes a message, artificially adds some noise, decodes
the noisy code, and keeps track of errors that the decoder detects along the
way. Because the decoding table contains only zeros, the decoder does not
correct any errors.

The output indicates that errors occurred in the first and second words.
Your results might vary because this example uses random numbers as
errors.

err_words =
1
2

Cyclic Codes. A cyclic code is a linear block code with the property that cyclic shifts
of a codeword (expressed as a series of bits) are also codewords. An
alternative characterization of cyclic codes is based on its generator
polynomial, as mentioned in Generator Polynomial and discussed in [5].

Encoding a message using a cyclic code requires a generator polynomial. If
you have defined variables msg, n,
k, and genpoly, then either of the
commands

encodes the information in msg using the
[n,k] code determined by the
generator polynomial genpoly. genpoly
is an optional argument for encode. The default
generator polynomial is cyclpoly(n,k). The
/decimal option, suitable when 2^n
and 2^k are not very large, indicates that
msg contains nonnegative decimal integers rather than
their binary representations. See Represent Words for Linear Block Codes or the reference page
for encode for a description of
the formats of msg and code.

Decoding the code requires the generator polynomial and possibly a
decoding table. If you have defined variables code,
n, k, genpoly,
and trt, then the commands

decode the information in code, using the
[n,k] code that the generator
matrix genmat determines. decode
also corrects errors according to instructions in the decoding table that
trt represents. genpoly is an
optional argument in the first two syntaxes above. The default generator
polynomial is cyclpoly(n,k).

Example

You can modify the example in Generic Linear Block Codes
so that it uses the cyclic coding technique, instead of the linear block
code with the generator matrix genmat. Make the changes
listed below:

Replace the second line by

genpoly = [1 0 1]; % generator poly is 1 + x^2

In the fifth and ninth lines (encode and
decode commands), replace
genmat by genpoly and
replace 'linear' by
'cyclic'.

Another example of encoding and decoding a cyclic code is on the reference
page for encode.

Hamming Codes

Create a Hamming Code in Binary Format Using Simulink

This example shows very simply how to use an encoder and decoder.
It illustrates the appropriate vector lengths of the code and message
signals for the coding blocks. Because the Error
Rate Calculation block accepts only scalars or frame-based
column vectors as the transmitted and received signals, this example
uses frame-based column vectors throughout. (It thus avoids having
to change signal attributes using a block such as Convert
1-D to 2-D.)

Open this model by entering doc_hamming at
the MATLAB command line. To build the model, gather and configure
these blocks:

Connect the blocks as in the preceding figure. Click the Display menu
and select Signals & Ports > Signal
Dimensions. After updating the diagram if necessary
(Simulation > Update Diagram),
the connector lines show relevant signal attributes. The connector
lines are double lines to indicate frame-based signals, and the annotations
next to the lines show that the signals are column vectors of appropriate
sizes.

Reduce the Error Rate Using a Hamming Code

This section describes how to reduce the error rate by adding an
error-correcting code. This figure shows model that uses a Hamming code.

To open a complete version of the model, type doc_hamming at
the MATLAB prompt.

Building the Hamming Code Model

You can build the Hamming code model by following these steps:

Type doc_channel at the MATLAB
prompt to open the channel noise model.

Then save the model as
my_hamming in the folder where you keep your
work files.

From the Simulink Library Browser drag the Hamming Encoder and
Hamming Decoder blocks
from the Error Detection and Correction/Block sublibrary into the
model window.

Click the right border of the model and drag it to the right to
widen the model window.

Click and drag the Hamming Encoder block on top of
the line between the Bernoulli Binary Generator
block and the Binary Symmetric Channel block, to the
right of the branch point, as shown in the following figure. Then
release the mouse button. The Hamming Encoder block should
automatically connect to the line from the Bernoulli Binary
Generator block to the Binary Symmetric
Channel block.

Click and drag the Hamming Decoder block on top of
the line between the Binary Symmetric Channel block
and the Error Rate Calculation block.

The model should now resemble this figure.

Using the Hamming Encoder and Decoder
Blocks

The Hamming Encoder block encodes the data before it is sent
through the channel. The default code is the [7,4] Hamming code, which encodes
message words of length 4 into codewords of length 7. As a result, the block
converts frames of size 4 into frames of size 7. The code can correct one error
in each transmitted codeword.

For an [n,k] code, the input to the Hamming Encoder block must consist of
vectors of size k. In this example, k = 4.

The Hamming Decoder block decodes the data after it is sent
through the channel. If at most one error is created in a codeword by the
channel, the block decodes the word correctly. However, if more than one error
occurs, the Hamming Decoder block might decode
incorrectly.

Double-click the Bernoulli Binary Generator block and make the
following changes to the parameter settings in the block's dialog box, as shown
in the following figure:

Set Samples per frame to
4. This converts the output of the block into
frames of size 4, in order to meet the input requirement of the
Hamming Encoder Block. See Sample-Based and Frame-Based Processing for more
information about frames.

Note

Many blocks, such as the Hamming Encoder block,
require their input to be a vector of a specific size. If you
connect a source block, such as the Bernoulli Binary
Generator block, to one of these blocks, set
Samples per frame to the required
value. For this model the Samples per frame
parameter of the Bernoulli Binary Generator block
must be a multiple of the Message Length K
parameter of the Hamming Encoder block.

Labeling the Display Block

You can change the label that appears below a block to make it more
informative. For example, to change the label below the Display block to
'Error Rate Display', first select the label with the
mouse. This causes a box to appear around the text. Enter the changes to the
text in the box.

Running the Hamming Code Model

To run the model, select Simulation > Start. The
model terminates after 100 errors occur. The error rate, displayed in the top
window of the Display block, is approximately .001. You get slightly different
results if you change the Initial seed parameters in the
model or run a simulation for a different length of time.

You expect an error rate of approximately .001 for the following reason: The
probability of two or more errors occurring in a codeword of length 7 is

1 – (0.99)7 –
7(0.99)6(0.01) = 0.002

If the codewords with two or more errors are decoded randomly, you expect
about half the bits in the decoded message words to be incorrect. This indicates
that .001 is a reasonable value for the bit error rate.

To obtain a lower error rate for the same probability of error, try using a
Hamming code with larger parameters. To do this, change the parameters
Codeword length and Message length
in the Hamming Encoder and Hamming
Decoder block dialog boxes. You also have to make the appropriate
changes to the parameters of the Bernoulli Binary Generator block
and the Binary Symmetric Channel block.

Displaying Frame Sizes

You can display the sizes of data frames in different parts of the model by
clicking the Display menu and selecting
Signals & Ports > Signal
Dimensions. The line leading out of the Bernoulli
Binary Generator block is labeled [4x1],
indicating that its output consists of column vectors of size 4. Because the
Hamming Encoder block uses a [7,4] code, it converts frames
of size 4 into frames of size 7, so its output is labeled
[7x1].

Adding a Scope to the Model

To display the channel errors produced by the Binary Symmetric
Channel block, add a Scope block to the model. This
is a good way to see whether your model is functioning correctly. The example
shown in the following figure shows where to insert the Scope
block into the model.

Widen the scope window until it is roughly
three times as wide as it is high. You can do this
by clicking the right border of the window and
dragging the border to the right, while pressing
the left-mouse button.

Relational Operator – Set
Relational Operator to
~= in the block's dialog box. The
Relational Operator block compares the transmitted signal, coming
from the Bernoulli Random Generator block, with the received signal,
coming from the Hamming Decoder block. The block outputs a 0 when
the two signals agree and a 1 when they disagree.

Observing Channel Errors with the
Scope

When you run the model, the scope displays the error data. At the end of each
5000 time steps, the scope appears as shown this figure. The scope then clears
the displayed data and displays the next 5000 data points.

The upper scope shows the channel errors generated by the Binary Symmetric Channel block. The
lower scope shows errors that are not corrected by channel coding.

Click the Stop button on the toolbar at the top of the
model window to stop the scope.

You can see individual errors by zooming in on the scope. First click the
middle magnifying glass button at the top left of the Scope
window. Then click one of the lines in the lower scope. This zooms in
horizontally on the line. Continue clicking the lines in the lower scope until
the horizontal scale is fine enough to detect individual errors. A typical
example of what you might see is shown in the figure below.

The wider rectangular pulse in the middle of the upper scope represents two
1s. These two errors, which occur in a single codeword, are not corrected. This
accounts for the uncorrected errors in the lower scope. The narrower rectangular
pulse to the right of the upper scope represents a single error, which is
corrected.

When you are done observing the errors, select Simulation >
Stop.

Export Data to MATLAB explains how to send the error data to the MATLAB
workspace for more detailed analysis.

BCH Codes

Represent Words for BCH Codes

A message for an [n,k] BCH code must be
a k-column binary Galois field array. The code that
corresponds to that message is an n-column binary Galois
field array. Each row of these Galois field arrays represents one word.

The example below illustrates how to represent words for a [15, 11] BCH
code.

Parameters for BCH Codes

BCH codes use special values of n and k:

n, the codeword length, is an integer
of the form 2m-1 for some integer m > 2.

k, the message length, is a positive
integer less than n. However, only some positive
integers less than n are valid choices for k.
See the BCH Encoder block reference
page for a list of some valid values of k corresponding
to values of n up to 511.

Detect and Correct Errors in a BCH Code Using MATLAB. The following example illustrates the decoding results for a
corrupted code. The example encodes some data, introduces errors in
each codeword, and attempts to decode the noisy code using the BCH
Decoder System
object.

Notice that the array of noise values contains binary values, and that the addition operation
c + noise takes place in the Galois field
GF(2) because c is a Galois field array in GF(2).

The output from the example is below. The nonzero value of ans indicates
that the decoder was able to correct the corrupted codewords and recover
the original message. The values in the vector nerrs indicate
that the decoder corrected t errors in each codeword.

chk2 =
1
nerrs =
3
3
3
3

Excessive Noise in BCH Codewords

In the previous example, the BCH Decoder
System
object corrected all the errors. However, each BCH code has a finite
error-correction capability. To learn more about how the BCH Decoder
System
object behaves when the noise is excessive, see the analogous
discussion for Reed-Solomon codes in Excessive Noise in Reed-Solomon Codewords.

Algorithms for BCH and RS Errors-only Decoding

Overview. The errors-only decoding algorithm used for BCH and RS codes
can be described by the following steps (sections 5.3.2, 5.4, and
5.6 in [2]).

Calculate the first 2t terms of
the infinite degree syndrome polynomial, S(z).

If those 2t terms of S(z) are all equal to 0, then the
code has no errors , no correction needs to be performed, and the
decoding algorithm ends.

If one or more terms of S(z) are nonzero, calculate the error
locator polynomial, Λ(z), via the Berlekamp
algorithm.

Calculate the error evaluator polynomial, Ω(z), via

Λ(z)S(z)=Ω(z)modz2t

Correct an error in the codeword according to

eim=Ω(α−im)Λ'(α−im)

where eim is the error magnitude in the imth position in the codeword, m is
a value less than the error-correcting capability of the code, Ω(z) is the error magnitude polynomial, Λ'(z) is the formal derivative [5] of
the error locator polynomial, Λ(z),
and α is the primitive element of
the Galois field of the code.

Further description of several of the steps is given in the
following sections.

Syndrome Calculation. For narrow-sense codes, the 2t terms of S(z) are calculated by evaluating
the received codeword at successive powers of α (the field’s primitive
element) from 0 to 2t-1. In other words, if we
assume one-based indexing of codewords C(z) and the syndrome polynomial S(z), and that codewords are of the
form [c1c1...cN], then each term Si of S(z) is given as

Si=∑i=1NciαN−1−i

Error Locator Polynomial Calculation. The error locator polynomial, Λ(z),
is found using the Berlekamp algorithm. A complete description of
this algorithm is found in [2],
but we summarize the algorithm as follows.

We define the following variables.

Variable

Description

n

Iterator variable

k

Iterator variable

L

Length of the feedback register used to generate the first
2t terms of S(z)

D(z)

Correction polynomial

d

Discrepancy

The following diagram shows the iterative procedure (i.e., the
Berlekamp algorithm) used to find Λ(z).

Reed-Solomon Codes

Represent Words for Reed-Solomon Codes

This toolbox supports Reed-Solomon codes that use m-bit symbols instead of bits. A message for
an [n,k] Reed-Solomon code must be a
k-column Galois field array in the field
GF(2m). Each array entry must be an integer
between 0 and 2m-1. The code corresponding to that
message is an n-column Galois field array in
GF(2m). The codeword length n
must be between 3 and 2m-1.

Parameters for Reed-Solomon Codes

This section describes several integers related to Reed-Solomon
codes and discusses how to find generator polynomials.

Allowable Values of Integer Parameters. The table below summarizes the meanings and allowable values
of some positive integer quantities related to Reed-Solomon codes
as supported in this toolbox. The quantities n and k are
input parameters for Reed-Solomon functions in this toolbox.

Symbol

Meaning

Value
or Range

m

Number
of bits per symbol

Integer
between 3 and 16

n

Number
of symbols per codeword

Integer
between 3 and 2m-1

k

Number
of symbols per message

Positive
integer less than n, such that n-k is
even

t

Error-correction
capability of the code

(n-k)/2

Generator Polynomial. The rsgenpoly function produces generator
polynomials for Reed-Solomon codes. rsgenpoly represents
a generator polynomial using a Galois row vector that lists the polynomial's
coefficients in order of descending powers of
the variable. If each symbol has m bits, the Galois row vector is
in the field GF(2m). For example, the command

finds that one generator polynomial for a [15,13] Reed-Solomon
code is X2 + (A2 + A)X + (A3),
where A is a root of the default primitive polynomial for GF(16).

Algebraic Expression for Generator
Polynomials

The generator polynomials that rsgenpoly produces
have the form
(X - Ab)(X - Ab+1)...(X - Ab+2t-1),
where b is an integer, A is a root of the primitive polynomial for the
Galois field, and t is (n-k)/2. The default value of b is
1. The output from rsgenpoly is the result of
multiplying the factors and collecting like powers of X. The example below
checks this formula for the case of a [15,13] Reed-Solomon code, using
b = 1.

Detect and Correct Errors in a Reed-Solomon Code Using MATLAB. The example below illustrates the decoding results for a corrupted
code. The example encodes some data, introduces errors in each codeword,
and attempts to decode the noisy code using the RS Decoder System
object.

The array of noise values contains integers between 1 and 2^m, and the
addition operation c + noise takes place in the
Galois field GF(2^m) because c is a
Galois field array in GF(2^m).

The output from the example is below. The nonzero value of ans indicates
that the decoder was able to correct the corrupted codewords and recover
the original message. The values in the vector nerrs indicates
that the decoder corrected t errors in each codeword.

ans =
1

nerrs =
2
2
2
2

Excessive Noise in Reed-Solomon Codewords. In the previous example, RS Encoder System
object corrected
all of the errors. However, each Reed-Solomon code has a finite error-correction
capability. If the noise is so great that the corrupted codeword is
too far in Hamming distance from the correct codeword, that means
either

The corrupted codeword is close to a valid codeword other
than the correct codeword. The decoder returns the message
that corresponds to the other codeword.

The corrupted codeword is not close enough to any
codeword for successful decoding. This situation is called a decoding
failure. The decoder removes the symbols in parity positions
from the corrupted codeword and returns the remaining symbols.

In both cases, the decoder returns the wrong message. However,
you can tell when a decoding failure occurs because RS Decoder System
object also
returns a value of -1 in its second output.

To examine cases in which codewords are too noisy for successful
decoding, change the previous example so that the definition of noise is

noise = (1+randi([0 n-1],nw,n)).*randerr(nw,n,t+1); % t+1 errors/row

Create Shortened Reed-Solomon Codes. Every Reed-Solomon encoder uses a codeword length that equals
2m-1 for an integer m. A shortened Reed-Solomon
code is one in which the codeword length is not 2m-1.
A shortened [n,k] Reed-Solomon
code implicitly uses an [n1,k1]
encoder, where

n1 = 2m -
1, where m is the number of bits per symbol

k1 = k + (n1 -
n)

The RS Encoder System
object supports shortened
codes using the same syntaxes it uses for nonshortened codes. You
do not need to indicate explicitly that you want to use a shortened
code.

Nonuniqueness of Generator Polynomials. Some pairs of message length and codeword length do not uniquely determine
the generator polynomial. The syntaxes for functions in the example above
also include options for retrieving generator polynomials that satisfy
certain constraints that you specify. See the functions' reference pages for
details about syntax options.

Algebraic Expression for Generator Polynomials. The generator polynomials produced by bchgenpoly and
rsgenpoly have the form
(X - Ab)(X - Ab+1)...(X - Ab+2t-1),
where A is a primitive element for an appropriate Galois field, and b and t
are integers. See the functions' reference pages for more information about
this expression.

Reed Solomon Examples with Shortening, Puncturing, and Erasures

In this section, a representative example of Reed Solomon coding
with shortening, puncturing, and erasures is built with increasing
complexity of error correction.

Encoder Example with Shortening and Puncturing. The following figure shows a representative example of a (7,3)
Reed Solomon encoder with shortening and puncturing.

In this figure, the message source outputs two information symbols,
designated by I1I2.
(For a BCH example, the symbols are simply binary bits.) Because
the code is a shortened (7,3) code, a zero must be added ahead of
the information symbols, yielding a three-symbol message of 0I1I2.
The modified message sequence is then RS encoded, and the added information
zero is subsequently removed, which yields a result of I1I2P1P2P3P4.
(In this example, the parity bits are at the end of the codeword.)

The puncturing operation is governed by the puncture vector,
which, in this case, is 1011. Within the puncture vector, a 1 means
that the symbol is kept, and a 0 means that the symbol is thrown away.
In this example, the puncturing operation removes the second parity
symbol, yielding a final vector of I1I2P1P3P4.

Decoder Example with Shortening and Puncturing. The following figure shows how the RS encoder operates on a
shortened and punctured codeword.

This case corresponds to the encoder operations shown in the
figure of the RS encoder with shortening and puncturing. As shown
in the preceding figure, the encoder receives a (5,2) codeword, because
it has been shortened from a (7,3) codeword by one symbol, and one
symbol has also been punctured.

As a first step, the decoder adds an erasure, designated by
E, in the second parity position of the codeword. This corresponds
to the puncture vector 1011. Adding a zero accounts for shortening,
in the same way as shown in the preceding figure. The single erasure
does not exceed the erasure-correcting capability of the code, which
can correct four erasures. The decoding operation results in the three-symbol
message DI1I2. The first
symbol is truncated, as in the preceding figure, yielding a final
output of I1I2.

Encoder Example with Shortening, Puncturing, and Erasures. The following figure shows the decoder operating on the punctured,
shortened codeword, while also correcting erasures generated by the
receiver.

In this figure, demodulator receives the I1I2P1P3P4 vector
that the encoder sent. The demodulator declares that two of the five
received symbols are unreliable enough to be erased, such that symbols
2 and 5 are deemed to be erasures. The 01001 vector, provided by an
external source, indicates these erasures. Within the erasures vector,
a 1 means that the symbol is to be replaced with an erasure symbol,
and a 0 means that the symbol is passed unaltered.

The decoder blocks receive the codeword and the erasure vector,
and perform the erasures indicated by the vector 01001. Within the
erasures vector, a 1 means that the symbol is to be replaced with
an erasure symbol, and a 0 means that the symbol is passed unaltered.
The resulting codeword vector is I1EP1P3E,
where E is an erasure symbol.

The codeword is then depunctured, according to the puncture
vector used in the encoding operation (i.e., 1011). Thus, an erasure
symbol is inserted between P1 and P3,
yielding a codeword vector of I1EP1EP3E.

Just prior to decoding, the addition of zeros at the beginning
of the information vector accounts for the shortening. The resulting
vector is 0I1EP1EP3E,
such that a (7,3) codeword is sent to the Berlekamp algorithm.

This codeword is decoded, yielding a three-symbol message of
DI1I2 (where D refers
to a dummy symbol). Finally, the removal of the D symbol from the
message vector accounts for the shortening and yields the original
I1I2 vector.

The decoding process is done iteratively. If the number of iterations
is too small, the algorithm may not converge. You may need to experiment
with the number of iterations to find an appropriate value for your
model. For details on the decoding algorithm, see Decoding
Algorithm.

Unlike some other codecs, you cannot connect an LDPC decoder directly to the output
of an LDPC encoder, because the decoder requires log-likelihood ratios
(LLR). Thus, you may use a demodulator to compute the LLRs.

Also, unlike other decoders, it is possible (although rare)
that the output of the LDPC decoder does not satisfy all parity checks.

Galois Field Computations

A Galois field is an algebraic field that has a finite number
of members. Galois fields having 2m members are used in
error-control coding and are denoted GF(2m). This chapter
describes how to work with fields that have 2m members,
where m is an integer between 1 and 16. The sections in this chapter are as follows.

For more details about specific functions that process arrays of Galois field
elements, see the online reference pages in the documentation for MATLAB or for Communications Toolbox software.

Note

Please note that the Galois field objects do not support the copy method.

MATLAB functions whose generalization to Galois fields is straightforward to
describe do not have reference pages in this manual because the entries would be
identical to those in the MATLAB documentation.

Galois Field Terminology

The discussion of Galois fields in this document uses a few terms that are not
used consistently in the literature. The definitions adopted here appear in van
Lint [4]:

A primitive element of
GF(2m) is a cyclic generator of the group
of nonzero elements of GF(2m). This means
that every nonzero element of the field can be expressed as the
primitive element raised to some integer power.

A primitive polynomial for
GF(2m) is the minimal polynomial of some
primitive element of GF(2m). It is the
binary-coefficient polynomial of smallest nonzero degree having a
certain primitive element as a root in
GF(2m). As a consequence, a primitive
polynomial has degree m and is irreducible.

The definitions imply that a primitive element is a root of a corresponding
primitive polynomial.

Section Overview. This section describes how to create a Galois field
array, which is a MATLAB expression that represents the elements of a Galois field.
This section also describes how MATLAB technical computing software interprets the numbers that you
use in the representation, and includes several examples.

Creating a Galois field array. To begin working with data from a Galois field GF(2^m),
you must set the context by associating the data with crucial information
about the field. The gf function performs this
association and creates a Galois field array in MATLAB. This function accepts as inputs

The Galois field data, x, which is a
MATLAB array whose elements are integers between 0 and
2^m-1.

(Optional) An integer, m,
that indicates x is in the field
GF(2^m). Valid values of m
are between 1 and 16. The default is 1, which means that the field
is GF(2).

(Optional) A positive integer that indicates
which primitive polynomial for GF(2^m) you are
using in the representations in x. If you omit
this input argument, gf uses a default
primitive polynomial for GF(2^m). For information
about this argument, see Specifying the Primitive Polynomial.

The output of the gf function is a variable that
MATLAB recognizes as a Galois field array, rather than an array of
integers. As a result, when you manipulate the variable, MATLAB works within the Galois field you have specified. For example,
if you apply the log function to a Galois field array,
MATLAB computes the logarithm in the Galois field and
not in the field of real or complex numbers.

When MATLAB Implicitly Creates a Galois field
array

Some operations on Galois field arrays require multiple arguments. If you
specify one argument that is a Galois field array and another that is an
ordinary MATLAB array, MATLAB interprets both as Galois field arrays in the same field. It
implicitly invokes the gf function on the ordinary
MATLAB array. This implicit invocation simplifies your syntax because
you can omit some references to the gf function. For an
example of the simplification, see Example: Addition and Subtraction.

Example: Creating Galois Field Variables. The code below creates a row vector whose entries are in the field GF(4),
and then adds the row to itself.

The output shows the values of the Galois field arrays named
a and b. Each output section
indicates

The field containing the variable, namely, GF(2^2) = GF(4).

The primitive polynomial for the field. In this case, it is the
toolbox's default primitive polynomial for GF(4).

The array of Galois field values that the variable contains. In
particular, the array elements in a are exactly
the elements of the vector x, and the array
elements in b are four instances of the zero
element in GF(4).

The command that creates b shows how, having defined
the variable a as a Galois field array, you can add
a to itself by using the ordinary
+ operator. MATLAB performs the vectorized addition operation in the field GF(4).
The output shows that

Compared to a, b is in the
same field and uses the same primitive polynomial. It is not
necessary to indicate the field when defining the sum,
b, because MATLAB remembers that information from the definition of the
addends, a.

The array elements of b are zeros because the
sum of any value with itself, in a Galois field of
characteristic two, is zero. This result differs from
the sum x + x, which represents an addition
operation in the infinite field of integers.

Example: Representing Elements of GF(8). To illustrate what the array elements in a Galois field array mean, the
table below lists the elements of the field GF(8) as integers and as
polynomials in a primitive element, A. The table should help you interpret a
Galois field array like

gf8 = gf([0:7],3); % Galois vector in GF(2^3)

Integer Representation

Binary Representation

Element of GF(8)

0

000

0

1

001

1

2

010

A

3

011

A + 1

4

100

A2

5

101

A2 + 1

6

110

A2 + A

7

111

A2 + A + 1

How Integers Correspond to Galois Field Elements. Building on the GF(8) example
above, this section explains the interpretation of array elements
in a Galois field array in greater generality. The field
GF(2^m) has 2^m distinct elements,
which this toolbox labels as 0, 1, 2,..., 2^m-1. These
integer labels correspond to elements of the Galois field via a polynomial
expression involving a primitive element of the field. More specifically,
each integer between 0 and 2^m-1 has a binary
representation in m bits. Using the bits in the binary
representation as coefficients in a polynomial, where the least significant
bit is the constant term, leads to a binary polynomial whose order is at
most m-1. Evaluating the binary polynomial at a primitive
element of GF(2^m) leads to an element of the
field.

Conversely, any element of GF(2^m) can be expressed as
a binary polynomial of order at most m-1, evaluated at a
primitive element of the field. The m-tuple of
coefficients of the polynomial corresponds to the binary representation of
an integer between 0 and 2^m.

Below is a symbolic illustration of the correspondence of an integer
X, representable in binary form, with a Galois field
element. Each bk is either zero or
one, while A is a primitive element.

X=bm−1⋅2m−1+⋯+b2⋅4+b1⋅2+b0↔bm−1⋅Am−1+⋯+b2⋅A2+b1⋅A+b0

Example: Representing a Primitive Element. The code below defines a variable alph that represents
a primitive element of the field GF(24).

The Galois field array alph represents a primitive
element because of the correspondence among

The integer 2, specified in the gf
syntax

The binary representation of 2, which is 10 (or 0010 using four
bits)

The polynomial A + 0, where A is a primitive element in this field
(or 0A3 + 0A2
+ A + 0 using the four lowest powers of A)

Primitive Polynomials and Element Representations. This section builds on the discussion in Creating a Galois field array by describing how to specify your own
primitive polynomial when you create a Galois field array. The topics
are

The discussion in How Integers Correspond to Galois Field Elements refers to
a primitive element, which is a root of a primitive polynomial of the
field. When you use the gf function to create a
Galois field array, the function interprets the integers in the array
with respect to a specific default primitive polynomial for that field,
unless you explicitly provide a different primitive polynomial. A list
of the default primitive polynomials is on the reference page for the
gf function.

To specify your own primitive polynomial when creating a Galois field
array, use a syntax like

c = gf(5,4,25) % 25 indicates the primitive polynomial for GF(16).

instead of

c1= gf(5,4); % Use default primitive polynomial for GF(16).

The extra input argument, 25 in this case,
specifies the primitive polynomial for the field
GF(2^m) in a way similar to the representation
described in How Integers Correspond to Galois Field Elements. In this
case, the integer 25 corresponds to a binary representation of 11001,
which in turn corresponds to the polynomial
D4 + D3 + 1.

Note

When you specify the primitive polynomial, the input argument must
have a binary representation using exactly m+1
bits, not including unnecessary leading zeros. In other words, a
primitive polynomial for GF(2^m) always has order
m.

When you use an input argument to specify the primitive polynomial,
the output reflects your choice by showing the integer value as well as
the polynomial representation.

Most fields offer multiple choices for the primitive polynomial that
helps define the representation of members of the field. When you use
the gf function, changing the primitive polynomial
changes the interpretation of the array elements and, in turn, changes
the results of some subsequent operations on the Galois field array. For
example, exponentiation of a primitive element makes it easy to see how
the primitive polynomial affects the representations of field
elements.

The output below shows that when the primitive polynomial has integer
representation 13, the Galois field array satisfies a
certain equation. By contrast, when the primitive polynomial has integer
representation 11, the Galois field array fails to
satisfy the equation.

The output when you try this example might also include a warning
about lookup tables. This is normal if you did not use the
gftable function to optimize computations
involving a nondefault primitive polynomial of 13.

Arithmetic in Galois Fields

Section Overview. You can perform arithmetic operations on Galois field arrays by using
familiar MATLAB operators, listed in the table below. Whenever you operate on
a pair of Galois field arrays, both arrays must be in the same Galois field.

Example: Addition and Subtraction. The code below adds two Galois field arrays to create an addition table
for GF(8). Addition uses the ordinary + operator. The
code below also shows how to index into the array addtb
to find the result of adding 1 to the elements of GF(8).

As an example of reading this addition table, the (7,4) entry in the
addtb array shows that gf(6,3)
plus gf(3,3) equals gf(5,3).
Equivalently, the element A2+A plus the element
A+1 equals the element A2+1. The equivalence
arises from the binary representation of 6 as 110, 3 as 011, and 5 as
101.

The subtraction table, which you can obtain by replacing
+ by -, is the same as
addtb. This is because subtraction and addition are
identical operations in a field of characteristic two.
In fact, the zeros along the main diagonal of addtb
illustrate this fact for GF(8).

Simplifying the Syntax

The code below illustrates scalar expansion and the implicit creation of a
Galois field array from an ordinary MATLAB array. The Galois field arrays h and
h1 are identical, but the creation of
h uses a simpler syntax.

Notice that 1+5 is reported as 4 in the Galois field. This is true because
the 5 represents the polynomial expression A2+1,
and 1+(A2+1) in GF(16) is
A2. Furthermore, the integer that represents
the polynomial expression A2 is 4.

Example: Multiplication. The example below multiplies individual elements in a Galois field array
using the .* operator. It then performs matrix
multiplication using the * operator. The elementwise
multiplication produces an array whose size matches that of the inputs. By
contrast, the matrix multiplication produces a Galois scalar because it is
the matrix product of a row vector with a column vector.

Example: Division. The examples below illustrate the four division operators in a Galois
field by computing multiplicative inverses of individual elements and of an
array. You can also compute inverses using inv or using
exponentiation by -1.

Elementwise Division

This example divides 1 by each of the individual elements in a Galois
field array using the ./ and .\
operators. These two operators differ only in their sequence of input
arguments. Each quotient vector lists the multiplicative inverses of the
nonzero elements of the field. In this example, MATLAB expands the scalar 1 to the size of nz
before computing; alternatively, you can use as arguments two arrays of the
same size.

This example divides the identity array by the square Galois field array
mat using the / and
\ operators. Each quotient matrix is the
multiplicative inverse of mat. Notice how the transpose
operator (') appears in the equivalent operation using
\. For square matrices, the sequence of transpose
operations is unnecessary, but for nonsquare matrices, it is
necessary.

Example: Exponentiation. The examples below illustrate how to compute integer powers of a Galois
field array. To perform matrix exponentiation on a Galois field array, you
must use a square Galois field array as the base and an ordinary (not
Galois) integer scalar as the exponent.

Elementwise Exponentiation

This example computes powers of a primitive element, A, of a Galois field.
It then uses these separately computed powers to evaluate the default
primitive polynomial at A. The answer of zero shows that A is a root of the
primitive polynomial. The .^ operator exponentiates each
array element independently.

Example: Elementwise Logarithm. The code below computes the logarithm of the elements of a Galois field
array. The output indicates how to express each nonzero
element of GF(8) as a power of the primitive element. The logarithm of the
zero element of the field is undefined.

Logical Operations in Galois Fields

Section Overview. You can apply logical tests to Galois field arrays and obtain a logical
array. Some important types of tests are testing for the equality of two
Galois field arrays and testing for nonzero
values in a Galois field array.

Testing for Equality. To compare corresponding elements of two Galois field arrays that have the
same size, use the operators == and
~=. The result is a logical array, each element of which
indicates the truth or falsity of the corresponding elementwise comparison.
If you use the same operators to compare a scalar with a Galois field array,
MATLAB technical computing software compares the scalar with each
element of the array, producing a logical array of the same size.

To compare entire arrays and obtain a logical scalar
result rather than a logical array, use the built-in
isequal function. However,
isequal uses strict rules for its comparison, and
returns a value of 0 (false) if you compare

A Galois field array with an ordinary MATLAB array, even if the values of the underlying array
elements match

A scalar with a nonscalar array, even if all elements in the array
match the scalar

Testing for Nonzero Values. To test for nonzero values in a Galois vector, or in the columns of a
Galois field array that has more than one row, use the
any or all function. These two
functions behave just like the ordinary MATLAB functions any and
all, except that they consider only the underlying
array elements while ignoring information about which Galois field the
elements are in. Examples are below.

Matrix Manipulation in Galois Fields

Basic Manipulations of Galois Field Arrays. Basic array operations on Galois field arrays are in the table below. The
functionality of these operations is analogous to the MATLAB operations having the same syntax.

Operation

Syntax

Index into array, possibly
using colon operator instead of a vector of explicit
indices

a(vector) or
a(vector,vector1), where
vector and/or
vector1 can be
":" instead of a vector

Basic Information About Galois Field Arrays. You can determine the length of a Galois vector or the size of any Galois
field array using the length and
size functions. The functionality for Galois field
arrays is analogous to that of the MATLAB operations on ordinary arrays, except that the output
arguments from size and length are
always integers, not Galois field arrays. The code below illustrates the use
of these functions.

Another type of information you might want to determine from a Galois
field array are the positions of nonzero elements. For an ordinary
MATLAB array, you might use the find function.
However, for a Galois field array, you should use find
in conjunction with the ~= operator, as
illustrated.

Linear Algebra in Galois Fields

Inverting Matrices and Computing Determinants. To invert a square Galois field array, use the inv
function. Related is the det function, which computes
the determinant of a Galois field array. Both inv and
det behave like their ordinary MATLAB counterparts, except that they perform computations in the
Galois field instead of in the field of complex numbers.

Note

A Galois field array is singular if and only if its determinant is
exactly zero. It is not necessary to consider roundoff errors, as in the
case of real and complex arrays.

The output from this example is either of these two messages, depending on
whether the randomly generated matrix is nonsingular or singular.

inv found the correct matrix inverse.
The matrix is not invertible.

Computing Ranks. To compute the rank of a Galois field array, use the
rank function. It behaves like the ordinary
MATLAB
rank function when given exactly one input argument.
The example below illustrates how to find the rank of square and nonsquare
Galois field arrays.

The values of r1 and r2 indicate
that asquare has less than full rank but that
anonsquare has full rank.

Factoring Square Matrices. To express a square Galois field array (or a permutation of it) as the
product of a lower triangular Galois field array and an upper triangular
Galois field array, use the lu function. This function
accepts one input argument and produces exactly two or three output
arguments. It behaves like the ordinary MATLAB
lu function when given the same syntax. The example
below illustrates how to factor using lu.

Solving Linear Equations. To find a particular solution of a linear equation in a Galois field, use
the \ or / operator on Galois field
arrays. The table below indicates the equation that each operator addresses,
assuming that A and B are previously
defined Galois field arrays.

Operator

Linear Equation

Syntax

Equivalent Syntax Using \

Backslash (\)

A * x = B

x = A \ B

Not applicable

Slash (/)

x * A = B

x = B / A

x = (A'\B')'

The results of the syntax in the table depend on characteristics of the
Galois field array A:

If A is square and nonsingular, the output
x is the unique solution to the linear
equation.

If A is square and singular, the syntax in the
table produces an error.

If A is not square, MATLAB attempts to find a particular solution. If
A'*A or A*A' is a singular
array, or if A is a matrix, where the rows
outnumber the columns, that represents an overdetermined system, the
attempt might fail.

Note

An error message does not necessarily indicate that the linear
equation has no solution. You might be able to find a solution by
rephrasing the problem. For example, gf([1 2; 0 0],3) \ gf([1;
0],3) produces an error but the mathematically equivalent
gf([1 2],3) \ gf([1],3) does not. The first
syntax fails because gf([1 2; 0 0],3) is a singular
square matrix.

Example: Solving Linear Equations

The examples below illustrate how to find particular solutions of linear
equations over a Galois field.

Use the conv function, as described in Multiplication and Division of Polynomials. This works
because convolving two vectors is equivalent to multiplying the two
polynomials whose coefficients are the entries of the
vectors.

Use the convmtx function to compute the
convolution matrix of one of the vectors, and then multiply that
matrix by the other vector. This works because convolving two
vectors is equivalent to filtering one of the vectors by the other.
The equivalence permits the representation of a digital filter as a
convolution matrix, which you can then multiply by any Galois vector
of appropriate length.

Tip

If you need to convolve large Galois vectors, multiplying by the
convolution matrix might be faster than using
conv.

Example

Computes the convolution matrix for a vector b in
GF(4). Represent the numerator coefficients for a digital filter, and then
illustrate the two equivalent ways to convolve b with
x over the Galois field.

Discrete Fourier Transform. The discrete Fourier transform is an important tool in digital signal
processing. This toolbox offers these tools to help you process discrete
Fourier transforms:

fft, which transforms a Galois vector

ifft, which inverts the discrete Fourier
transform on a Galois vector

dftmtx, which returns a Galois field array
that you can use to perform or invert the discrete Fourier transform
on a Galois vector

In all cases, the vector being transformed must be a Galois vector of
length 2m-1 in the field
GF(2m). The following example illustrates the
use of these functions. You can check, using the
isequal function, that y equals
y1, z equals
z1, and z equals
x.

Polynomials over Galois Fields

Section Overview. You can use Galois vectors to represent polynomials in an indeterminate
quantity x, with coefficients in a Galois field. Form the representation by
listing the coefficients of the polynomial in a vector in order of
descending powers of x. For example, the vector

gf([2 1 0 3],4)

represents the polynomial Ax3 +
1x2 + 0x + (A+1), where

A is a primitive element in the field
GF(24).

x is the indeterminate quantity in the polynomial.

You can then use such a Galois vector to perform arithmetic with, evaluate, and
find roots of
polynomials. You can also find minimal
polynomials of elements of a Galois field.

Addition and Subtraction of Polynomials. To add and subtract polynomials, use + and
- on equal-length Galois vectors that represent the
polynomials. If one polynomial has lower degree than the other, you must pad
the shorter vector with zeros at the beginning so the two vectors have the
same length. The example below shows how to add a degree-one and a
degree-two polynomial.

Multiplication and Division of Polynomials. To multiply and divide polynomials, use conv and
deconv on Galois vectors that represent the
polynomials. Multiplication and division of polynomials is equivalent to
convolution and deconvolution of vectors. The deconv
function returns the quotient of the two polynomials as well as the
remainder polynomial. Examples are below.

Evaluating Polynomials. To evaluate a polynomial at an element of a Galois field, use
polyval. It behaves like the ordinary MATLAB
polyval function when given exactly two input
arguments. The example below evaluates a polynomial at several elements in a
field and checks the results using .^ and
.* in the field.

The first element of y evaluates the polynomial at
0 and, therefore, returns the polynomial's constant
term of 3.

Roots of Polynomials. To find the roots of a polynomial in a Galois field, use the
roots function on a Galois vector that represents
the polynomial. This function finds roots that are in the same field that
the Galois vector is in. The number of times an entry appears in the output
vector from roots is exactly its multiplicity as a root
of the polynomial.

Note

If the Galois vector is in GF(2m), the
polynomial it represents might have additional roots in some extension
field GF((2m)k).
However, roots does not find those additional roots
or indicate their existence.

Roots of Binary Polynomials. In the special case of a polynomial having binary coefficients, it is also
easy to find roots that exist in an extension field. This is because the
elements 0 and 1 have the same
unambiguous representation in all fields of characteristic two. To find
roots of a binary polynomial in an extension field, apply the
roots function to a Galois vector in the extension
field whose array elements are the binary coefficients of the
polynomial.

The example below seeks the roots of a binary polynomial in various
fields.

The roots of the polynomial do not exist in GF(2), so
noroots is an empty array. However, the roots of the
polynomial exist in GF(4) as well as in GF(16), so roots4
and roots16 are nonempty.

Notice that roots4 and roots16 are
not equal to each other. They differ in these ways:

roots4 is a GF(4) array, while
roots16 is a GF(16) array. MATLAB keeps track of the underlying field of a Galois field
array.

The array elements in roots4 and
roots16 differ because they use
representations with respect to different primitive polynomials. For
example, 2 (which represents a primitive element)
is an element of the vector roots4 because the
default primitive polynomial for GF(4) is the same polynomial that
gf4poly represents. On the other hand,
2 is not an element of
roots16 because the primitive element of
GF(16) is not a root of the polynomial that
gf16poly represents.

Minimal Polynomials. The minimal polynomial of an element of GF(2m)
is the smallest degree nonzero binary-coefficient polynomial having that
element as a root in GF(2m). To find the minimal
polynomial of an element or a column vector of elements, use the
minpol function.

The code below finds that the minimal polynomial of
gf(6,4) is
D2 + D + 1 and then checks
that gf(6,4) is indeed among the roots of that polynomial
in the field GF(16).

Manipulating Galois Variables

Section Overview. This section describes techniques for manipulating Galois variables or for
transferring information between Galois field arrays and ordinary
MATLAB arrays.

Note

These techniques are particularly relevant if you write MATLAB file functions that process Galois field arrays. For an
example of this type of usage, enter edit gf/conv in
the Command Window and examine the first several lines of code in the
editor window.

Determining Whether a Variable Is a Galois Field Array. To find out whether a variable is a Galois field array rather than an
ordinary MATLAB array, use the isa function. An
illustration is below.

Extracting Information from a Galois Field Array. To extract the array elements, field order, or primitive polynomial from a
variable that is a Galois field array, append a suffix to the name of the
variable. The table below lists the exact suffixes, which are independent of
the name of the variable.

Information

Suffix

Output Value

Array elements

.x

MATLAB array of type uint16 that
contains the data values from the Galois field
array.

Field order

.m

Integer of type double that indicates
that the Galois field array is in
GF(2^m).

Note

If the output value is an integer data type and you want to convert it
to double for later manipulation, use the
double function.

The code below illustrates the use of these suffixes. The definition of
empr uses a vector of binary coefficients of a
polynomial to create a Galois field array in an extension field. Another
part of the example retrieves the primitive polynomial for the field and
converts it to a binary vector representation having the appropriate number
of bits.

Speed and Nondefault Primitive Polynomials

Specifying the Primitive Polynomial describes how to represent
elements of a Galois field with respect to a primitive polynomial of your
choice. This section describes how you can increase the speed of computations
involving a Galois field array that uses a primitive polynomial other than the
default primitive polynomial. The technique is recommended if you perform many
such computations.

The mechanism for increasing the speed is a data file,
userGftable.mat, that some computational functions use to
avoid performing certain computations repeatedly. To take advantage of this
mechanism for your combination of field order (m) and
primitive polynomial (prim_poly):

Navigate in the MATLAB application to a folder to which you have write
permission. You can use either the cd function or
the Current Folder feature to navigate.

Define m and prim_poly as
workspace variables. For example:

m = 3; prim_poly = 13; % Examples of valid values

Invoke the gftable function:

gftable(m,prim_poly); % If you previously defined m and prim_poly

The function revises or creates userGftable.mat in your
current working folder to include data relating to your combination of field
order and primitive polynomial. After you initially invest the time to invoke
gftable, subsequent computations using those values of
m and prim_poly should be
faster.

Note

If you change your current working directory after invoking
gftable, you must place
userGftable.mat on your MATLAB path to ensure that MATLAB can see it. Do this by using the addpath
command to prefix the directory containing
userGftable.mat to your MATLAB path. If you have multiple copies of
userGftable.mat on your path, use
which('userGftable.mat','-all') to find out where
they are and which one MATLAB is using.

To see how much gftable improves the speed of your
computations, you can surround your computations with the
tic and toc functions. See the
gftable reference page for an
example.

Galois Fields of Odd Characteristic

A Galois field is an algebraic field having
pm elements, where p is prime and m is a positive
integer. This chapter describes how to work with Galois fields in which p is
odd. To work with Galois fields having an even number of
elements, see Galois Field
Computations. The sections in this chapter are as follows.

Galois Field Terminology

Throughout this section, p is an odd prime number and m is a positive integer.

Also, this document uses a few terms that are not used consistently in the
literature. The definitions adopted here appear in van Lint [5].

A primitive element of
GF(pm) is a cyclic generator of the group
of nonzero elements of GF(pm). This means
that every nonzero element of the field can be expressed as the
primitive element raised to some integer power. Primitive elements are
called A throughout this section.

A primitive polynomial for
GF(pm) is the minimal polynomial of some
primitive element of GF(pm). As a
consequence, it has degree m and is irreducible.

Representing Elements of Galois Fields

Section Overview. This section discusses how to represent Galois field elements using this
toolbox's exponential
format and polynomial
format. It also describes a way to list all
elements of the Galois field, because some functions use such a
list as an input argument. Finally, it discusses the nonuniqueness of
representations of Galois field elements.

The elements of GF(p) can be represented using the integers from 0 to
p-1.

When m is at least 2, GF(pm) is called an
extension field. Integers alone cannot represent the elements of
GF(pm) in a straightforward way. MATLAB technical computing software uses two main conventions for
representing elements of GF(pm): the exponential
format and the polynomial format.

Note

Both the exponential format and the polynomial format are relative to
your choice of a particular primitive element A of
GF(pm).

Exponential Format. This format uses the property that every nonzero element of
GF(pm) can be expressed as
Ac for some integer c between 0 and
pm-2. Higher exponents are not needed,
because the theory of Galois fields implies that every nonzero element of
GF(pm) satisfies the equation
xq-1 = 1 where
q = pm.

The use of the exponential format is shown in the table below.

Element of
GF(pm)

MATLAB Representation of the Element

0

-Inf

A0 = 1

0

A1

1

...

...

Aq-2 where
q = pm

q-2

Although -Inf is the standard exponential
representation of the zero element, all negative integers are equivalent to
-Inf when used as input
arguments in exponential format. This equivalence can be useful; for
example, see the concise line of code at the end of the section Default Primitive Polynomials.

Note

The equivalence of all negative integers and -Inf
as exponential formats means that, for example, -1 does
not represent A-1,
the multiplicative inverse of A. Instead, -1 represents the zero element
of the field.

Polynomial Format. The polynomial format uses the property that every element of
GF(pm) can be expressed as a polynomial in A
with exponents between 0 and m-1, and coefficients in GF(p). In the
polynomial format, the element

A(1) + A(2) A + A(3) A2 + ... + A(m) Am-1

is represented in MATLAB by the vector

[A(1) A(2) A(3) ... A(m)]

Note

The Galois field functions in this toolbox represent a polynomial as a
vector that lists the coefficients in order of
ascending powers of the variable. This is the
opposite of the order that other MATLAB functions use.

List of All Elements of a Galois Field. Some Galois field functions in this toolbox require an argument that lists
all elements of an extension field GF(pm). This
is again relative to a particular primitive element A of
GF(pm). The proper format for the list of
elements is that of a matrix having pm rows, one
for each element of the field. The matrix has m columns, one for each
coefficient of a power of A in the polynomial format shown in Polynomial Format above. The first row contains only zeros
because it corresponds to the zero element in
GF(pm). If k is between 2 and
pm, then the kth row specifies the polynomial
format of the element Ak-2.

The minimal polynomial of A aids in the computation of this matrix,
because it tells how to express Am in terms of
lower powers of A. For example, the table below lists the elements of
GF(32), where A is a root of the primitive
polynomial 2 + 2x + x2. This
polynomial allows repeated use of the substitution

A2 = -2 - 2A = 1 + A

when performing the computations in the middle column of the table.

Elements of GF(9)

Exponential Format

Polynomial Format

Row of MATLAB Matrix of Elements

A-Inf

0

0 0

A0

1

1 0

A1

A

0 1

A2

1+A

1 1

A3

A + A2 = A + 1 + A = 1 + 2A

1 2

A4

A + 2A2 = A + 2 + 2A = 2

2 0

A5

2A

0 2

A6

2A2 = 2 + 2A

2 2

A7

2A + 2A2 = 2A + 2 + 2A = 2 + A

2 1

Example

An automatic way to generate the matrix whose rows are in the third column
of the table above is to use the code below.

Nonuniqueness of Representations. A given field has more than one primitive element. If two primitive
elements have different minimal polynomials, then the corresponding matrices
of elements will have their rows in a different order. If the two primitive
elements share the same minimal polynomial, then the matrix of elements of
the field is the same.

Note

You can use whatever primitive element you want, as long as you
understand how the inputs and outputs of Galois field functions depend
on the choice of some primitive polynomial. It is
usually best to use the same primitive polynomial throughout a given
script or function.

Other ways in which representations of elements are not unique arise from
the equations that Galois field elements satisfy. For example, an
exponential format of 8 in GF(9) is really the same as an exponential format
of 0, because
A8 = 1 = A0
in GF(9). As another example, the substitution mentioned just before the
table Elements of GF(9) shows that the
polynomial format [0 0 1] is really the same as the polynomial format [1
1].

Default Primitive Polynomials

This toolbox provides a default primitive polynomial for
each extension field. You can retrieve this polynomial using the
gfprimdf function. The command

prim_poly = gfprimdf(m,p); % If m and p are already defined

produces the standard row-vector representation of the default minimal
polynomial for GF(pm).

Converting and Simplifying Element Formats

Converting to Simplest Polynomial Format. The gftuple function produces the simplest polynomial
representation of an element of GF(pm), given
either an exponential representation or a polynomial representation of that
element. This can be useful for generating the list of elements of
GF(pm) that other functions require.

Using gftuple requires three arguments: one
representing an element of GF(pm), one indicating
the primitive polynomial that MATLAB technical computing software should use when computing the
output, and the prime p. The table below indicates how
gftuple behaves when given the first two arguments
in various formats.

Behavior of gftuple Depending on Format of
First Two Inputs

How to Specify Element

How to Indicate Primitive
Polynomial

What gftuple Produces

Exponential format; c = any integer

Integer m > 1

Polynomial format of Ac,
where A is a root of the default
primitive polynomial for
GF(pm)

Example: tp =
gftuple(6,2,3); % c = 6
here

Exponential format; c = any integer

Vector of coefficients of primitive polynomial

Polynomial format of Ac,
where A is a root of the given
primitive polynomial

The four examples that appear in the table above all produce the same
vector tp = [2, 1], but their different inputs to
gftuple correspond to the lines of the table. Each
example expresses the fact that
A6 = 2+A, where A is a root of the
(default) primitive polynomial
2 + x+ x2 for
GF(32).

Example

This example shows how gfconv and
gftuple combine to multiply two polynomial-format
elements of GF(34). Initially,
gfconv multiplies the two polynomials, treating the
primitive element as if it were a variable. This produces a high-order
polynomial, which gftuple simplifies using the
polynomial equation that the primitive element satisfies. The final result
is the simplest polynomial format of the product.

Example: Generating a List of Galois Field Elements. This example applies the conversion functionality to the task of
generating a matrix that lists all elements of a Galois field. A matrix that
lists all field elements is an input argument in functions such as
gfadd and gfmul. The variables
field1 and field2 below have the
format that such functions expect.

Converting to Simplest Exponential Format. The same function gftuple also produces the simplest
exponential representation of an element of
GF(pm), given either an exponential
representation or a polynomial representation of that element. To retrieve
this output, use the syntax

[polyformat, expformat] = gftuple(...)

The input format and the output polyformat are as in
the table Behavior of gftuple Depending on Format of First Two Inputs. In
addition, the variable expformat contains the simplest
exponential format of the element represented in
polyformat. It is simplest in
the sense that the exponent is either -Inf or a number
between 0 and pm-2.

Example

To recover the exponential format of the element 2 + A that the
previous section considered, use the commands below. In this case,
polyformat contains redundant information, while
expformat contains the desired result.

[polyformat, expformat] = gftuple([2 1],2,3)

polyformat =
2 1
expformat =
6

This output appears at first to contradict the information in the table
Elements of GF(9) , but in fact it does
not. The table uses a different primitive element; two plus that primitive
element has the polynomial and exponential formats shown below.

Arithmetic in Galois Fields

Section Overview. You can add, subtract, multiply, and divide elements of Galois fields
using the functions gfadd, gfsub,
gfmul, and gfdiv,
respectively. Each of these functions has a mode for prime fields and
a mode for extension
fields.

Arithmetic in Prime Fields. Arithmetic in GF(p) is the same as arithmetic modulo p. The functions
gfadd, gfmul,
gfsub, and gfdiv accept two
arguments that represent elements of GF(p) as integers between 0 and p-1.
The third argument specifies p.

Example: Addition Table for GF(5)

The code below constructs an addition table for GF(5). If
a and b are between 0 and 4, then
the element gfp_add(a+1,b+1) represents the sum
a+b in GF(5). For example, gfp_add(3,5) =
1 because 2+4 is 1 modulo 5.

Other values of p produce tables for different prime
fields GF(p). Replacing gfadd by
gfmul, gfsub, or
gfdiv produces a table for the corresponding
arithmetic operation in GF(p).

Arithmetic in Extension Fields. The same arithmetic functions can add elements of
GF(pm) when m > 1, but the
format of the arguments is more complicated than in the case above. In
general, arithmetic in extension fields is more complicated than arithmetic
in prime fields; see the works listed in Selected Bibliography for Galois Fields for details about how the arithmetic operations work.

When working in extension fields, the functions
gfadd, gfmul,
gfsub, and gfdiv use the first
two arguments to represent elements of GF(pm) in
exponential format. The third argument, which is required, lists all
elements of GF(pm) as described in List of All Elements of a Galois Field. The result is in
exponential format.

Example: Addition Table for GF(9)

The code below constructs an addition table for
GF(32), using exponential formats relative to
a root of the default primitive polynomial for GF(9). If
a and b are between -1 and 7, then
the element gfpm_add(a+2,b+2) represents the sum of
Aa and Ab in
GF(9). For example, gfpm_add(4,6) = 5 because

A2 + A4 = A5

Using the fourth and sixth rows of the matrix field,
you can verify that

Note

If you used a different primitive polynomial, then the tables would
look different. This makes sense because the ordering of the rows and
columns of the tables was based on that particular choice of primitive
polynomial and not on any natural ordering of the elements of
GF(9).

Other values of p and m produce
tables for different extension fields GF(p^m). Replacing
gfadd by gfmul,
gfsub, or gfdiv produces a
table for the corresponding arithmetic operation in
GF(p^m).

Note

The Galois field functions in this toolbox represent a
polynomial over GF(p) for odd values of p as a vector that lists
the coefficients in order of ascending
powers of the variable. This is the opposite of the order that
other MATLAB functions use.

Cosmetic Changes of Polynomials. To display the traditionally formatted polynomial that corresponds to a
row vector containing coefficients, use gfpretty. To
truncate a polynomial by removing all zero-coefficient terms that have
exponents higher than the degree of the polynomial, use
gftrunc. For example,

Note

If you do not use a fixed-width font, then the spacing in the display
might not look correct.

Polynomial Arithmetic. The functions gfadd and gfsub
add and subtract, respectively, polynomials over GF(p). The
gfconv function multiplies polynomials over GF(p).
The gfdeconv function divides polynomials in GF(p),
producing a quotient polynomial and a remainder polynomial. For example, the
commands below show that
2 + x + x2 times
1 + x over the field GF(3) is
2 + 2x2 + x3.

a = gfconv([2 1 1],[1 1],3)
[quot, remd] = gfdeconv(a,[2 1 1],3)

The output is below.

a =
2 0 2 1
quot =
1 1
remd =
0

The previously discussed functions gfadd and
gfsub add and subtract, respectively, polynomials.
Because it uses a vector of coefficients to represent a polynomial,
MATLAB does not distinguish between adding two polynomials and adding
two row vectors elementwise.

Characterization of Polynomials. Given a polynomial over GF(p), the gfprimck function
determines whether it is irreducible and/or primitive. By definition, if it
is primitive then it is irreducible; however, the reverse is not necessarily
true. The gfprimdf and gfprimfd
functions return primitive polynomials.

Given an element of GF(pm), the
gfminpol function computes its minimal polynomial
over GF(p).

Example

For example, the code below reflects the irreducibility of all minimal
polynomials. However, the minimal polynomial of a nonprimitive element is
not a primitive polynomial.

Roots of Polynomials. Given a polynomial over GF(p), the gfroots function
finds the roots of the polynomial in a suitable extension field
GF(pm). There are two ways to tell
MATLAB the degree m of the extension field
GF(pm), as shown in the following table.

Formats for Second Argument of
gfroots

Second Argument

Represents

A positive integer

m as in GF(pm).
MATLAB uses the default primitive polynomial in
its computations.

A row vector

A primitive polynomial for
GF(pm). Here m is the
degree of this primitive polynomial.

Example: Roots of a Polynomial in
GF(9)

The code below finds roots of the polynomial
1 + x2 + x3
in GF(9) and then checks that they are indeed roots. The exponential format
of elements of GF(9) is used throughout.