Main Content




code = bchenc(msg,N,K) encodes the input message using an (N,K) BCH encoder that uses a narrow-sense generator polynomial. For a description of Bose–Chaudhuri–Hocquenghem (BCH) coding, see [1].

code = bchenc(msg,N,K,paritypos) appends or prepends the parity symbols to the encoded input message to form the output.


collapse all

Set the BCH parameters for a Galois array of GF(2).

M = 4;
n = 2^M-1;   % Codeword length
k = 5;       % Message length
nwords = 10; % Number of words to encode

Create a message.

msgTx = gf(randi([0 1],nwords,k));

Find the error-correction capability.

t = bchnumerr(n,k)
t = 3

Encode the message.

enc = bchenc(msgTx,n,k);

Corrupt up to t bits in each codeword.

noisycode = enc + randerr(nwords,n,1:t);

Decode the noisy code.

msgRx = bchdec(noisycode,n,k);

Validate that the message was properly decoded.

ans = logical

Increase the number of possible errors, and generate another noisy codeword.

t2 = t + 1;
noisycode2 = enc + randerr(nwords,n,1:t2);

Decode the new received codeword.

[msgRx2,numerr] = bchdec(noisycode2,n,k);

Determine if the message was properly decoded by examining the number of corrected errors, numerr. Entries of -1 correspond to decoding failures, which occur when the codeword has more errors than can be corrected for the specified [n,k] pair.

numerr = 10×1


Two of the ten transmitted codewords were not correctly received.

Input Arguments

collapse all

Message to encode, specified as a Galois field array of symbols over GF(2). Each K-element row of msg represents a message word, where the leftmost symbol is the most significant symbol.

For more information, see Creating a Galois field array.

Example: msgTx = gf(randi([0 1],10,5)), where msgTx is a 10-by-5 gf array.

Codeword length, specified as an integer of the form N = 2M–1, where M is an integer from 3 through 16. For more information, see Tips.

Example: 15 for M=4

Message length, specified as an integer. N and K must produce a narrow-sense BCH code.

Example: 5 specifies a Galois array with five elements

Parity position, specified as 'end' or 'beginning'. Parity symbols are at the end or beginning of each word in the output Galois array.

Output Arguments

collapse all

Encoded message, returned as a Galois field array. Parity symbols are at the end or beginning of each word in the output Galois array. To specify the position of the parity symbols, use the paritypos argument.


  • To generate the list of valid (N,K) pairs along with the corresponding values of the error-correction capability, run bchnumerr(N).

  • Valid values for N = 2M–1, where M is an integer from 3 through 16. The maximum allowable value of N is 65,535.


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

Version History

Introduced before R2006a