# comm.BCHEncoder

Encode data using BCH encoder

## Description

The `BCHEncoder` object creates a BCH code with specified message and codeword lengths.

To encode data using a BCH coding scheme:

1. Define and set up your BCH encoder object. See Construction.

2. Call `step` to create a BCH code with message and codeword lengths specified according to the properties of `comm.BCHEncoder`. The behavior of `step` is specific to each object in the toolbox.

### Note

Starting in R2016b, instead of using the `step` method to perform the operation defined by the System object™, you can call the object with arguments, as if it were a function. For example, ```y = step(obj,x)``` and `y = obj(x)` perform equivalent operations.

## Construction

`enc = comm.BCHEncoder` creates a BCH encoder System object, `enc`, that performs BCH encoding.

`enc = comm.BCHEncoder(N,K)` creates a BCH encoder object, `enc`, with the `CodewordLength` property set to `N` and the `MessageLength` property set to `K`.

`enc = comm.BCHEncoder(N,K,GP)` creates a BCH encoder object, `enc`, with the `CodewordLength` property set to `N`, the `MessageLength` property set to `K` and the `GeneratorPolynomial` property set to `GP`.

`enc = comm.BCHEncoder(N,K,GP,S)` creates a BCH encoder object, `enc`, with the `CodewordLength` property set to `N`, the `MessageLength` property set to `K`, the `GeneratorPolynomial` property set to `GP` and the `ShortMessageLength` property set to `S`.

`enc = comm.BCHEncoder(N,K,GP,S,Name,Value)` creates a BCH encoder object, `enc`, with the `CodewordLength` property set to `N`, the `MessageLength` property set to `K`, the `GeneratorPolynomial` property set to `GP`, the `ShortMessageLength` property set to `S`, and each specified property `Name` set to the specified `Value`.

`enc = comm.BCHEncoder(Name,Value)` creates a BCH encoder object, `enc`, with each specified property set to the specified value. You can specify additional name-value pair arguments in any order as (`Name1`,`Value1`,...,`NameN`,`ValueN`).

## Properties

### Note

The input and output signal lengths are listed in Input and Output Signal Lengths in BCH and RS System Objects on the `comm.BCHDecoder` reference page.

 `CodewordLength` Codeword length Specify the codeword length of the BCH code as a double-precision positive integer scalar. The default is `15`. The values of the `CodewordLength` and `MessageLength` properties must produce a valid narrow-sense BCH code. For a full-length BCH code, the value of the property must use the form ${2}^{M}-1$, where M is an integer such that $3\le M\le 16$. The default is `15`. `MessageLength` Message length Specify the message length as a double-precision positive integer scalar. The values of the `CodewordLength` and `MessageLength` properties must produce a valid narrow-sense BCH code. The default is `5`. `ShortMessageLengthSource` Short message length source Specify the source of the shortened message as either `Auto` or `Property`. When this property is set to `Auto`, the BCH code is defined by the `CodewordLength`, `MessageLength`, `GeneratorPolynomial`, and `PrimitivePolynomial` properties. When `ShortMessageLengthSource` is set to `Property`, you must specify the `ShortMessageLength` property that is used with the other properties to define the RS code. The default is `Auto`. `ShortMessageLength` Shortened message length Specify the length of the shortened message as a double-precision positive integer scalar whose value must be less than or equal to `MessageLength`. When `ShortMessageLength` < `MessageLength`, the BCH code is shortened. The default is `5`. `GeneratorPolynomialSource` Source of generator polynomial Specify the source of the generator polynomial as either `Auto` or `Property`. Set this property to `Auto` to create the generator polynomial automatically. Set it to `Property` to specify a generator polynomial using the `GeneratorPolynomial`  property. The default is `Auto`. `GeneratorPolynomial` Generator polynomial Specify the generator polynomial as a binary double-precision row vector, a binary Galois row vector that represents the coefficients of the generator polynomial in order of descending powers, or as a polynomial character vector. The length of the generator polynomial requires a value of `CodewordLength`-`MessageLength`+`1`. This property applies when you set `GeneratorPolynomialSource` to `Property`. The default is `'X^10 + X^8 + X^5 + X^4 + X^2 + X + 1'`, which is the result of `bchgenpoly`(`15,5,[],'double'`) and corresponds to a (15,5) code. `CheckGeneratorPolynomial` Enable generator polynomial checking Set this property to `true` to perform a generator polynomial check the first time you call the `step` method. This check verifies that the specified generator polynomial is valid. For larger codes, disabling the check reduces processing time. As a best practice, perform the check at least once before setting this property to false. This property applies when you set  `GeneratorPolynomialSource`   to `Property`. The default is `true`. `PrimitivePolynomialSource` Source of primitive polynomial Specify the source of the primitive polynomial as one of `Auto` or `Property`. Set this property to `Auto` to create a primitive polynomial of degree M=`ceil(log2(CodewordLength+1))`. Set it to `Property` to specify a polynomial using the `PrimitivePolynomial` property. The default is `Auto` `PrimitivePolynomial` Primitive polynomial Specify the primitive polynomial of order M, that defines the finite Galois field GF(2). Use a double-precision, binary row vector with the coefficients of the polynomial in order of descending powers or as a polynomial character vector. This property applies when you set the `PrimitivePolynomialSource` property to `Property`. The default is `'X^4 + X + 1'`, which is the result of `fliplr(de2bi(primpoly(4)))`. `PuncturePatternSource` Source of puncture pattern Specify the source of the puncture pattern as one of `None` or `Property`. Set this property to `None` to disable puncturing. Set it to `Property` to decode punctured codewords. This decoding is based on a puncture pattern vector you specify in the `PuncturePattern` property. The default is `None`. `PuncturePattern` Puncture pattern vector Specify the pattern that the object uses to puncture the encoded data. Use a double-precision binary column vector of length `CodewordLength`-`MessageLength`. Zeros in the puncture pattern vector indicate the position of the parity bits that the object punctures or excludes from each codeword. This property applies when you set `PuncturePatternSource` to `Property`. The default is `[ones(8,1); zeros(2,1)]`.

## Methods

 step Encode data using a BCH encoder
Common to All System Objects
`release`

Allow System object property value changes

## Examples

collapse all

```% The following code transmits a BCH-encoded, 8-DPSK-modulated bit stream % through an AWGN channel. Then, the example demodulates, decodes, and counts errors. enc = comm.BCHEncoder; mod = comm.DPSKModulator('BitInput',true); chan = comm.AWGNChannel(... 'NoiseMethod','Signal to noise ratio (SNR)','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 = step(enc, data); modSignal = step(mod, encodedData); receivedSignal = step(chan, modSignal); demodSignal = step(demod, receivedSignal); receivedBits = step(dec, demodSignal); errorStats = step(errorRate, data, receivedBits); end fprintf('Error rate = %f\nNumber of errors = %d\n', ... errorStats(1), errorStats(2))```
```Error rate = 0.015075 Number of errors = 9 ```

Transmit and receive a BPSK-modulated signal encoded with a shortened BCH code, then count errors.

Specify the codeword, message, and shortened message lengths.

```N = 255; K = 239; S = 63;```

Create a BCH (255,239) generator polynomial. Use the generator polynomial to create a BCH encoder and decoder pair. The BCH code is based on the AMR standard.

```gp = bchgenpoly(255,239); bchEncoder = comm.BCHEncoder(N,K,gp,S); bchDecoder = comm.BCHDecoder(N,K,gp,S);```

Create an error rate counter.

`errorRate = comm.ErrorRate('ComputationDelay',3);`

Main processing loop.

```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,5); % 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.000318 Number of errors = 4 ```

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`.

```enc = comm.BCHEncoder(31,26,'x5+x2+1',6); dec = comm.BCHDecoder(31,26,'x5+x2+1',6);```

Encode and decode random binary data and verify that the decoded bit stream matches the original data.

```x = randi([0 1],60,1); y = step(enc,x); z = step(dec,y); isequal(x,z)```
```ans = logical 1 ```

## Selected Bibliography

[1] Clark, George C. Jr., and J. Bibb Cain, Error-Correction Coding for Digital Communications. New York, Plenum Press, 1981.

[2] Wicker, Stephen B., Error Control Systems for Digital Communication and Storage Upper Saddle River, NJ, Prentice Hall, 1995.

## Algorithms

This object implements the algorithm, inputs, and outputs described in Algorithms for BCH and RS Errors-only Decoding.