comm.BCHEncoder
Encode data using BCH encoder
Description
The comm.BCHEncoder
System object™ creates a BCH code with message and codeword lengths that you specify. For more
information, see Algorithms.
To encode data using a BCHcoding scheme:
Create the
comm.BCHEncoder
object and set its properties.Call the object with arguments, as if it were a function.
To learn more about how System objects work, see What Are System Objects?
Creation
Syntax
Description
creates a BCH encoder
System object, enc
= comm.BCHEncoderenc
, that applies BCH encoding.
sets enc
= comm.BCHEncoder(N
,K
)CodewordLength
to
N
and MessageLength
to
K
. For example, comm.BCHEncoder(7,3)
creates a
(7,3) BCH encoder object.
additionally sets enc
= comm.BCHEncoder(N
,K
,GP
)GeneratorPolynomial
to
GP
and GeneratorPolynomialSource
to 'Property'
.
additionally sets enc
= comm.BCHEncoder(N
,K
,GP
,S
)ShortMessageLength
to
S
and ShortMessageLengthSource
to 'Property'
.
specifies options using one or more namevalue arguments in addition to the input
arguments in previous syntaxes. For example,
enc
= comm.BCHEncoder(___,Name
=Value
)comm.BCHEncoder(7,3,PuncturePatternSource="Property",PuncturePattern=[ones(7,1);
zeros(3,1)])
creates a (7, 3) BCH encoder object with the puncture pattern set
to [1;1;1;1;1;1;1;0;0;0]
.
Properties
Unless otherwise indicated, properties are nontunable, which means you cannot change their
values after calling the object. Objects lock when you call them, and the
release
function unlocks them.
If a property is tunable, you can change its value at any time.
For more information on changing property values, see System Design in MATLAB Using System Objects.
CodewordLength
— Codeword length of BCH code
15
(default)  positive integer
Codeword length of the BCH code in symbols, specified as a positive integer. The
values of the CodewordLength
and MessageLength
properties must
produce a valid narrowsense BCH code. For a fulllength BCH code, the value of this
property must take the form (2^{M} – 1),
where M is an integer in the range [3, 16].
MessageLength
— Message length
5
(default)  positive integer
Message length in symbols, specified as a positive integer. The values of the
CodewordLength
and MessageLength
properties must produce a valid narrowsense
BCH code.
ShortMessageLengthSource
— Source of short message length
"Auto"
(default)  "Property"
Source of the short message length, specified as "Auto"
or
"Property"
.
When you set this property to
"Auto"
, the object derives the shortening fromCodewordLength
andMessageLength
. The object defines the BCH code based on theCodewordLength
,MessageLength
,GeneratorPolynomial
, andPrimitivePolynomial
properties.When you set this property to
"Property"
, the object usesShortMessageLength
along with the other properties to define the BCH code.
ShortMessageLength
— Shortened message length
5
(default)  positive integer
Shortened message length in symbols, specified as a positive integer whose value
must be less than or equal to MessageLength
. When
ShortMessageLength
< MessageLength
, the object
shortens the BCH code.
Dependencies
This property applies when you set ShortMessageLengthSource
to "Property"
.
GeneratorPolynomialSource
— Source of generator polynomial
"Auto"
(default)  "Property"
Source of the generator polynomial, specified as "Auto"
or
"Property"
.
When you set this property to
"Auto"
, the object calculates the generator polynomial based on the value ofPrimitivePolynomial
.When you set this property to
"Property"
, the object usesGeneratorPolynomial
to define the generator polynomial.
GeneratorPolynomial
— Generator polynomial
'X^10 + X^8 + X^5 + X^4 + X^2 + X + 1'
(default)  integer row vector  gf
row vector  character vector  string scalar
Generator polynomial, specified as a polynomial string, integer row vector, or
Galois field row vector. The specified row vector defines the finite field gf
(2^{M}) in descending order
of powers corresponding to the integers that form messages and codewords. The length of
the generator polynomial must be (N – K + 1),
where N is CodewordLength
and
K is MessageLength
.
Express the polynomial as one of these options:
Character vector or string scalar of a polynomial whose constant term is
1
. For more information, see Representation of Polynomials in Communications Toolbox.Binaryvalued row vector that represents the coefficients of the polynomial in order of descending powers. The length of this vector must be (M + 1), where M is the degree of the polynomial. The first and last entries must be
1
, indicating the leading term with degree M and a constant term of 1.Integervalued row vector of elements that represent the exponents for the nonzero terms of the polynomial in order of descending powers. The last entry must be
0
, indicating a constant term of 1.
The default 'X^10 + X^8 + X^5 + X^4 + X^2 + X + 1'
the result of
bchgenpoly
(15,5,[],'double'
) and corresponds to a
(15,5) code.
Dependencies
This property applies when you set GeneratorPolynomialSource
to "Property"
.
Data Types: double
 string
 char
CheckGeneratorPolynomial
— Option to check generator polynomial
true
or 1
(default)  false
or 0
Option to check the generator polynomial, specified as a logical
1
(true
) or 0
(false
). Set this property to true
to verify the
specified generator polynomial is valid.
Tips
For codes with generator polynomials of high degree, disabling the check speeds up
processing. As a best practice, perform the check at least once before setting this
property to false
.
Dependencies
This property applies when GeneratorPolynomialSource
is set to
"Property"
.
Data Types: logical
PrimitivePolynomialSource
— Source of primitive polynomial
"Auto"
(default)  "Property"
Source of the primitive polynomial, specified as "Auto"
or
"Property"
.
When you set this property to
"Auto"
, the object uses a primitive polynomial of degree M =ceil
(log2(CodewordLength
+ 1)).When you set this property to
"Property"
, the object usesPrimitivePolynomial
to define the primitive polynomial.
PrimitivePolynomial
— Primitive polynomial
'X^4 + X + 1'
(default)  character vector  string scalar  binary row vector
Primitive polynomial, specified as a binary row vector that defines the finite field
gf
(2^{M}) in descending order
of powers corresponding to the integers that form messages and codewords.
Express the polynomial as one of these options:
Character vector or string scalar of a polynomial whose constant term is
1
. For more information, see Representation of Polynomials in Communications Toolbox.Binaryvalued row vector that represents the coefficients of the polynomial in order of descending powers. The length of this vector must be M + 1, where M is the degree of the polynomial. The first and last entries must be
1
, indicating the leading term with degree M and a constant term of 1.Integervalued row vector of elements that represent the exponents for the nonzero terms of the polynomial in order of descending powers. The last entry must be
0
, indicating a constant term of 1.
The default 'X^4 + X + 1'
is the result of int2bit
(primpoly
(4),5)'.
Dependencies
This property applies when you set PrimitivePolynomialSource
to "Property"
.
PuncturePatternSource
— Source of puncture pattern
"None"
(default)  "Property"
Source of the puncture pattern, specified as "None"
or
"Property"
.
When you set this property to
"None"
, the object does not apply puncturing to the code.When you set this property to
"Property"
, the object punctures the code using the puncture pattern vector defined byPuncturePattern
.
PuncturePattern
— Puncture pattern
[ones(8,1); zeros(2,1)]
(default)  binary column vector
Puncture pattern, specified as a binary column vector with a length of
(N – K), where N is CodewordLength
and
K is MessageLength
. Zeros in the
puncture pattern vector indicate the position of the parity symbols that are punctured
or excluded from each codeword.
Dependencies
This property applies when you set PuncturePatternSource
to "Property"
.
Usage
Syntax
Description
encodes the
input message using a (Y
= enc(X
)CodewordLength
,MessageLength
) BCH encoder with
the corresponding narrowsense generator polynomial. For information about the input and
output lengths when you run the object, see Input and Output Signal Lengths in BCH and RS System Objects.
Input Arguments
X
— Message to encode
column vector
Message to encode, specified as numeric or logical column vector with length equal
to an integer multiple of MessageLength
. A group of
MessageLength
input elements represents one message word to be
encoded.
Data Types: double
 single
 logical
 int8
 int16
 int32
 uint8
 uint16
 uint32
Output Arguments
Y
— BCHencoded codeword
binaryvalued column vector
BCHencoded codeword, returned as a binaryvalued column vector. The length of the
encoded data output vector is an integer multiple of (CodewordLength
–
N_{punctures}), where
N_{punctures} is the number of punctures. You
specify the number of punctures with the PuncturePatternSource
and PuncturePattern
properties.
Data Types: single
 logical
Object Functions
To use an object function, specify the
System object as the first input argument. For
example, to release system resources of a System object named obj
, use
this syntax:
release(obj)
Examples
Compute Errors for 8DPSKModulated Signal with BCH Forward Error Correction Applied
Transmit a BCHencoded, 8DPSKmodulated bit stream through an AWGN channel, and then demodulate, decode, and count errors in the received signal.
enc = comm.BCHEncoder; mod = comm.DPSKModulator(BitInput=true); snr = 10; demod = comm.DPSKDemodulator(BitOutput=true); dec = comm.BCHDecoder; errorRate = comm.ErrorRate(ComputationDelay=3); for counter = 1:20 data = randi([0 1],30,1); encodedData = enc(data); modSignal = mod(encodedData); receivedSignal = awgn(modSignal,snr); demodSignal = demod(receivedSignal); receivedBits = dec(demodSignal); errorStats = errorRate(data,receivedBits); end fprintf('Error rate = %f\nNumber of errors = %d\n', ... errorStats(1),errorStats(2))
Error rate = 0.008375 Number of errors = 5
Shorten BCH Code
Shorten a (31,26) BCH code to an (11,6) BCH code and use it to encode and decode random binary data.
Create a BCH encoder and decoder pair for a (31,26) code. Specify the generator polynomial, $${x}^{5}+{x}^{2}+1$$, and a shortened message length of 6
.
N = 31; % Codeqword length K = 26; % Message length S = 6; % Shortened message length gp = 'x5+x2+1' % Generator polynomial
gp = 'x5+x2+1'
enc = comm.BCHEncoder(N,K,gp,S); dec = comm.BCHDecoder(N,K,gp,S);
Encode and decode random binary data and verify that the decoded bit stream matches the original data.
X = randi([0 1],60,1); Y = enc(X); Z = dec(Y); isequal(X,Z)
ans = logical
1
Transmit and Receive BCHCoded and BPSKModulated Signal
Transmit and receive a BPSKmodulated signal encoded with a shortened BCH code, then count errors.
Define variables for coding configuration based on the AMR standard.
N = 255; % Codeword length K = 239; % Message length S = 63; % Shortened message length gp = bchgenpoly(N,K); % BCH (255,239) generator polynomial
Create a BCH encoder and decoder pair.
bchEncoder = comm.BCHEncoder(N,K,gp,S); bchDecoder = comm.BCHDecoder(N,K,gp,S);
Create an error rate counter.
errorRate = comm.ErrorRate('ComputationDelay',3);
Run the main processing loop.
snr = 5; for counter = 1:20 data = randi([0 1],630,1); % Generate binary data encodedData = bchEncoder(data); % BCH encode data modSignal = pskmod(encodedData,2); % BPSK modulate receivedSignal = awgn(modSignal,snr); % Pass through AWGN channel demodSignal = pskdemod(receivedSignal,2); % BSPK demodulate receivedBits = bchDecoder(demodSignal); % BCH decode data errorStats = errorRate(data,receivedBits); % Compute error statistics end
Display the error statistics.
fprintf('Error rate = %f\nNumber of errors = %d\n', ... errorStats(1),errorStats(2))
Error rate = 0.000238 Number of errors = 3
Algorithms
This object implements BoseChaudhuriHocquenghem (BCH) coding as described in [1] and Algorithms for BCH and RS Errorsonly Decoding.
Input and Output Signal Lengths in BCH and RS System Objects
The notation y = c * x
denotes that y
is an integer multiple of x
.
The number of punctures equals the number of zeros in the puncture vector.
M is the degree of the primitive polynomial. Each group of
M bits represents an integer in the range [0,
(2^{M}–1)] that belongs to the finite
Galois field gf
(2^{M}).
ShortMessageLengthSource  "Auto"  "Property" 

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

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

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

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

References
[1] Clark, George C., and J. Bibb Cain. ErrorCorrection Coding for Digital Communications. Applications of Communications Theory. New York: Plenum Press, 1981.
[2] Wicker, Stephen B. Error Control Systems for Digital Communication and Storage. Upper Saddle River, NJ: Prentice Hall, 1995.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
See System Objects in MATLAB Code Generation (MATLAB Coder).
When you use this object to generate code, you must set the generator polynomial to a doubleprecision integer row vector.
Version History
Introduced in R2012a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)