| Communications Toolbox | ![]() |
The bchenc and bchdec functions create and decode BCH codes, using the data described in Representing Words for BCH Codes and Parameters for BCH Codes. This section illustrates how to use bchenc and bchdec.
The topics are
The example below illustrates how to encode and decode data using a [15, 5] Reed-Solomon code. The example shows that
You can vary the position of the parity symbols within the codewords, choosing either the end (default) or beginning.
Corresponding syntaxes of bchenc and bchdec use the same input arguments, except for the first input argument.
n = 15; k = 5; % Codeword length and message length dat = randint(4,k); % Four random binary messages msg = gf(dat); % Represent data using a Galois array. % Simplest syntax for encoding c1 = bchenc(msg,n,k); d1 = bchdec(c1,n,k); % Prepend the parity symbols instead of appending them. c2 = bchenc(msg,n,k,'beginning'); d2 = bchdec(c2,n,k,'beginning'); % Check that the decoding worked correctly. chk = isequal(d1,msg) & isequal(d2,msg)
The output is below.
chk =
1
The example below illustrates the decoding results for a corrupted code. The example encodes some data, introduces errors in each codeword, and invokes bchdec to attempt to decode the noisy code. It uses additional output arguments in bchdec to gain information about the success of the decoding process.
n = 15; k = 5; % Codeword length and message length [gp,t] = bchgenpoly(n,k); % t is error-correction capability. nw = 4; % Number of words to process msgw = gf(randint(nw,k)); % Random k-symbol messages c = bchenc(msgw,n,k); % Encode the data. noise = randerr(nw,n,t); % t errors/row cnoisy = c + noise; % Add noise to the code. [dc,nerrs,corrcode] = bchdec(cnoisy,n,k); % Decode cnoisy. % Check that the decoding worked correctly. chk2 = isequal(dc,msgw) & isequal(corrcode,c) nerrs % Find out how many errors bchdec corrected.
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 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, bchdec corrected all the errors. However, each BCH code has a finite error-correction capability. To learn more about how bchdec behaves when the noise is excessive, see the analogous discussion for Reed-Solomon codes in Excessive Noise in Reed-Solomon Codewords.
| Parameters for BCH Codes | Representing Words for Linear Block Codes | ![]() |
© 1994-2005 The MathWorks, Inc.