Communications Toolbox 
Syntax
msg = decode(code,n,k,'
hamming/
format
'
,primpoly); msg = decode(code,n,k,'
linear/
format
'
,genmat,trt); msg = decode(code,n,k,'
cyclic/
format
'
,genpoly,trt); msg = decode(code,n,k,'
bch/
format
'
,errorcorr,primpoly); msg = decode(code,n,k,'
rs/
format
'
,field); msg = decode(code,n,k); [msg,err] = decode(...); [msg,err,ccode] = decode(...); [msg,err,ccode,cerr] = decode(...);
Optional Inputs
Input 
Default Value 

binary 
primpoly 
gfprimdf(m) where n = 2^{m}1 
genpoly 
cyclpoly (n,k) 
trt 
Uses syndtable to create the syndrome decoding table associated with the method's paritycheck matrix. 
For All Syntaxes
The decode
function aims to recover messages that were encoded using an errorcorrection coding technique. The technique and the defining parameters must match those that were used to encode the original signal.
The For All Syntaxes section on the reference page for the encode
function explains the meanings of n
and k
, the possible values of format
, and the possible formats for code
and msg
. You should be familiar with the conventions described there before reading the rest of this section. Using the decode
function with an input argument code
that was not created by the encode
function may cause errors.
For Specific Syntaxes
msg = decode(code,n,k,'
decodes hamming/
format
',primpoly)
code
using the Hamming method. For this syntax, n
must have the form 2^{m}1 for some integer m greater than or equal to 3, and k
must equal n
m. primpoly
is a row vector that gives the binary coefficients, in order of ascending powers, of the primitive polynomial for GF(2^{m}) that is used in the encoding process. The default value of primpoly
is gfprimdf
(
m)
. The decoding table that the function uses to correct a single error in each codeword is syndtable
(hammgen
(m)).
msg = decode(code,n,k,'
decodes linear/
format
',genmat,trt)
code
, which is a linear block code determined by the k
byn
generator matrix genmat
. genmat
, a k
byn
matrix, is required as input. decode
tries to correct errors using the decoding table trt
, where trt
is a 2^{nk}byn
matrix.
msg = decode(code,n,k,'
decodes the cyclic code cyclic/
format
',genpoly,trt)
code
and tries to correct errors using the decoding table trt
, where trt
is a 2^{nk}byn
matrix. genpoly
is a row vector that gives the coefficients, in order of ascending powers, of the binary generator polynomial of the code. The default value of genpoly
is cyclpoly
(n,k)
. By definition, the generator polynomial for an [n,k] cyclic code must have degree nk and must divide x^{n}1.
msg = decode(code,n,k,'
decodes bch/
format
',errorcorr,primpoly)
code
using the BCH method. primpoly
is a row vector that gives the coefficients, in order of ascending powers, of the primitive polynomial for GF(2^{m}) that will be used during processing. The default value of primpoly
is gfprimdf
(
m)
. For this syntax, n
must have the form 2^{m}1 for some integer m greater than or equal to 3. k
and errorcorr
must be a valid message length and errorcorrection capability, respectively, as reported in the second and third columns of a row of params
in the command
params = bchpoly(n)
msg = decode(code,n,k,'
decodes rs/
format
',field)
code
using the ReedSolomon method. n
must have the form 2^{m}1 for some integer m greater than or equal to 3. field
is a matrix that lists all elements of GF(2^{m}) in the format described in List of All Elements of a Galois Field. The default value of field
is gftuple
([1:2^
m2]',
m)
.
msg = decode(code,n,k)
is the same as msg = decode(code,n,k,'
hamming/binary
')
.
[msg,err] = decode(...)
returns a column vector err
that gives information about error correction. If the code is a convolutional code, then err
contains the metric calculations used in the decoding decision process. For other types of codes, a nonnegative integer in the rth row of err
(or the rth row of vec2mat
(err,k)
if code
is a column vector) indicates the number of errors corrected in the rth message word; a negative integer indicates that there are more errors in the rth word than can be corrected.
[msg,err,ccode] = decode(...)
returns the corrected code in ccode
.
[msg,err,ccode,cerr] = decode(...)
returns a column vector cerr
whose meaning depends on the format of code
:
code
is a binary vector, then a nonnegative integer in the rth row of vec2mat
(cerr,n)
indicates the number of errors corrected in the rth codeword; a negative integer indicates that there are more errors in the rth codeword than can be corrected.code
is not a binary vector, then cerr = err
.Examples
On the reference page for encode
, some of the example code illustrates the use of the decode
function.
The example below illustrates the use of err
and cerr
when the coding method is not convolutional code and the code is a binary vector. The script encodes two fivebit messages using BCH code. Each codeword has fifteen bits. Errors are added to the first two bits of the first codeword and the first bit of the second codeword. Then decode
is used to recover the original message. As a result, the errors are corrected. err
is the same size as msg
and cerr
is the same size as code
. err
reflects the fact that the first message was recovered after correcting two errors, while the second message was recovered after correcting one error. cerr
reflects the fact that the first codeword was decoded after correcting two errors, while the second codeword was decoded after correcting one error.
m = 4; n = 2^m1; % Codeword length is 15. k = 5; % Valid message length for BCH code when n = 15 t = 3; % Corresponding errorcorrection capability msg = ones(10,1); % Two messages, five bits each code = encode(msg,n,k,'bch'); % Encode the message. % Now place two errors in first word and one error % in the second word. Create errors by reversing bits. noisycode = code; noisycode(1:2) = bitxor(noisycode(1:2),[1 1]'); noisycode(16) = bitxor(noisycode(16),1); % Decode and try to correct the errors. [newmsg,err,ccode,cerr] = decode(noisycode,n,k,'bch',t); disp('Transpose of err is'); disp(err') disp('Transpose of cerr is'); disp(cerr')
Transpose of err is 2 2 2 2 2 1 1 1 1 1 Transpose of cerr is Columns 1 through 12 2 2 2 2 2 2 2 2 2 2 2 2 Columns 13 through 24 2 2 2 1 1 1 1 1 1 1 1 1 Columns 25 through 30 1 1 1 1 1 1
Algorithm
Depending on the decoding method, decode
relies on such lowerlevel functions as hammgen
, syndtable
, cyclgen
, bchdeco
, and rsdeco
.
See Also
encode
, hammgen
, syndtable
, cyclpoly
, cyclgen
, bchpoly
, bchdeco
, rspoly
, rsdeco
, rsdecode
, vitdec
de2bi  demodmap 