Faculty of Engineering - Download as DOC by J63o2xk8



                                 Multimedia University
                 Experiment 2: Linear Block Coding
1.       OBJECTIVE
        To apply error-control coding techniques using Linear Block Codes.
        To design and generate Linear Block Codes with MATLAB software.

        MATLAB


3.1     MATLAB
MATLAB is a powerful collection of tools for algorithm development, computation and
visualization. It provides more control and flexibility compared to a traditional high-level
programming language. Unlike such languages, MATLAB is compact and easy to learn,
letting you express algorithms in concise, readable code. In addition, MATLAB provides
an extensive set of ready-to-use functions including mathematical and matrix operations,
graphics, color and sound control, and low-level file I/O. MATLAB is readily extendable
- you can use the MATLAB language to easily create functions that operate as part of the
MATLAB environment.

The MATLAB User's Guide describes how to work with the MATLAB language. It
discusses how to enter and manipulate data and use MATLAB's extensive collection of
functions. It explains how to perform command-line computations and how to create
your own functions and scripts. The MATLAB Reference Guide provides reference
descriptions of supplied MATLAB functions and commands.

SIMULINK is a dynamic system simulation environment. It allows you to represent
systems as block diagrams, which you build using your mouse to connect blocks and your
keyboard to edit block parameters.

The SIMULINK User's Guide describes how to work with SIMULINK. It also provides
reference descriptions of each block in the standard SIMULINK libraries.

3.3    Communications Toolbox
The Communications Toolbox is a collection of computation functions and simulation
blocks for research, development, system design, analysis, and simulation in the
communications area. The toolbox is designed to be suitable for both experts and
beginners in the communications field. The toolbox contains ready-to-use functions and
blocks, which you can easily modify to implement your own schemes, methods, and

IT2: LINEAR BLOCK CODING                                                                  1

algorithms. The Communications Toolbox is designed for use with MATLAB and
SIMULINK. The Communications Toolbox includes:
     SIMULINK blocks
     MATLAB functions

You can use the toolbox directly from the MATLAB workspace. You can use the
SIMULINK environment to construct a simulation block diagram for your
communication system. For convenience, the Communications Toolbox provides online
interactive examples that use any of the function found in this toolbox.

3.4    Available Functions
This toolbox supports a variety of functions in the communications area. These functions
     Data source
     Source coding/ decoding
     Error-control coding
     Modulation/ demodulation
     Transmission/ reception filters
     Transmitting channel
     Multiple access
     Synchronization
     Utilities

In this section, we will illustrate some commands used for this experiment. (For additional
information on the individual MATLAB commands, we refer you to the MATLAB User's Guide or
the online help facility.)

4.1    Fundamental Expressions
Working in the MATLAB environment is straightforward because most commands are
entered as you would write them mathematically. For example, entering the following
simple expression

       >>a = 4/3

yields the MATLAB response


In general, it is better to use appropriate and memorable names for variables. MATLAB
recognizes the first 19 characters of a variable name but the first character in a variable
name be a letter. MATLAB is case sensitive and all MATLAB commands are written in
lowercase letters.

IT2: LINEAR BLOCK CODING                                                                 2

If you prefer to create a new variable but do not wish to see the MATLAB response, type
a semicolon at the end of the expression. For example,

       >>b = 4+7;

will create a new variable b whose value is 11, but MATLAB will not display the value
of b.

4.2     Creating Script Files
It is much more convenient to use script files than to enter commands line by line at the
MATLAB prompt. A script file is an ASCII file (regular text file) that contains a series
of MATLAB commands written just as you would enter them in the MATLAB
environment. Statements that begin with a '%' are considered to be comments and are
ignored by MATLAB. The script file is created outside of MATLAB with any available
text editor or word processor. Each script file should have a name that ends in ".m". The
commands in the script file are executed in the MATLAB environment simply by
entering the name of the script file without the ".m". For example, suppose the text file
magphase.m contains the following statements:

       % magphase.m: example m-file to
       % compute the magnitude and phase of G at  = 1.
        =1;
       G =l/(j* + 2);
       mag =abs(G)
       phase =atan(imag(G)/real(G))

Then type magphase at the MATLAB prompt will yield the following MATLAB



which are the magnitude and phase of the transfer function G(j) = l/(j + 2) evaluated at
 = 1. By entering help magphase will give you back the text in the comment lines at the
beginning of the file:

       magphase.m: example m-file to
       compute the magnitude and phase of G at =1.

It should be obvious that it is very desirable to include at least some brief comments as a
header to each m-file.

IT2: LINEAR BLOCK CODING                                                                 3

4.3    Matrices
Matrices are entered into MATLAB by listing the elements of the matrix and enclosing
them within a pair of brackets. Commas or blanks separate elements of a single row, and
semicolons or carriage returns separate rows. For example,

       >>A = [1 2; 3 4]

yields the MATLAB response

            1      2
            3      4

To get the dimensions of a matrix, we can use the size command, e.g.,

       >>[K,N] = size(A)

Special vectors can be created using the ':' operator. For example, the command

       >>k= 1:10

will generate a row vector with elements from 1 to 10 with increment 1.

Using the simple commands described thus far, you can easily manipulate both matrices
and vectors. For example, to add a row onto matrix A we type,

       >>A = [A; [7 8]]
           1     2
           3     4
           7     8

To extract the submatrix of A that consists of the first through second columns of the
second through third rows, use vectors as indices as follows:

       >>B = A(2:3,1:2)
           3    4
           7    8

IT2: LINEAR BLOCK CODING                                                             4

MATLAB has commands for generating special matrices. For example, you can create a
n x n identity matrix with the eye(n). The zeros, ones and rand commands work
similarly to eye and make matrices with elements equal to zero, elements equal to one,
and random elements respectively. These commands can also be used to create non-
square matrices. For example, zeros(2,4) generates a 2 x 4 matrix of zeros.

Finally, A = A' command will give the tranpose of the matrix A.

4.4    Additional Commands

ENCODE (Error-control code encoding computation)
    CODE = ENCODE(MSG, N, K, METHOD, OPT) encodes the message signal
    MSG using an error-control coding method such that the message length is K and
    code word length is N with the coding method as specified in variable METHOD.
    OPT is an optional parameter used in some of the methods.

       'hamming'      Hamming code
       'linear'       Linear block code
       'cyclic'       Cyclic code
       'bch'          Binary BCH code
       'rs'           Reed-Solomon code
       'convol'       Convolution code

       The CODE outputs the encoded code word. The format of CODE matched the
       format of MSG. When MSG is a K-column matrix, the output CODE is an N-
       column matrix.

DECODE (Error-control code decoding computation)
    MSG = DECODE(CODE, N, K, METHOD, OPT1, OPT2, OPT3, OPT4) decodes
    the codeword signal CODE using an error-control coding method such that the
    message length is K and code word length is N with the coding method specified
    in variable METHOD. OPT1 to OPT4 are extra optional parameters used in some
    of the methods.

       'hamming'      Hamming code
       'linear'       Linear block code
       'cyclic'       Cyclic code
       'bch'          BCH code
       'rs'           Reed-Solomon code
       'convol'       Convolution code

IT2: LINEAR BLOCK CODING                                                            5

       To use all of the methods, the message signal in CODE could be a binary vector
       or an L-column matrix. The output MSG contains the decoded message. The
       format of MSG matches the format of CODE. When CODE is an N-column
       matrix, the output MSG is a K-column matrix. When the format of the input
       CODE is not the same as the output of function ENCODE, this function stops

DE2BI (Converts a positive decimal numbers to binary numbers)
     B = DE2BI(D) converts a positive integer decimal vector D to a binary matrix B.
     Each row of the binary matrix B represents the corresponding number in D.

FLIPLR (Flip matrix in left/right direction)
     FLIPLR(X) returns X with row preserved and columns flipped in the left/right

       X=     123     becomes        321
              456                    654

RANDINT (Random integer matrix generator)
     OUT = RANDINT(N) generates an N-by-N matrix of random binary numbers.
     The appearance of "0" and "1" has even probability.
     OUT = RANDINT(N, M) generates an N-by-M matrix of random binary numbers.
     The appearance of "0" and "1" has even probability.

BITERR (Computes bit error)
     [NUMBER, RATIO] = BITERR(X, Y) compares the elements in the input matrices
     X and Y. The number of the bit differences is outputted in NUMBER. The ratio of
     NUMBER to the total bit number contained in Y is output in RATIO. All elements
     in X and Y should be non-negative integers.

      XOR(S, T) is the logical symmetric difference of elements S and T. The result is
      one where either S or T, but not both, is nonzero. The result is zero where S and T
      are both zero or nonzero. S and T must have the same dimensions (or one can be a

IT2: LINEAR BLOCK CODING                                                               6

5.     THEORY

5.1     Error-Control Coding
Error-control coding techniques are used to detect and/or correct errors that occur in the
message transmission in a digital communication system. The transmitting side of the
error-control coding adds redundant bits or symbols to the original information signal
sequence. The receiving side of the error-control coding uses these redundant bits or
symbols to detect and/or correct the errors that occurred during transmission. The
transmission coding process is known as encoding, and the receiving coding process is
known as decoding.

There are two major classes in error-control code: block and convolutional. In block
coding, successive blocks of K information (message) symbols are formed. The coding
algorithm then transforms each block into a codeword consisting of n symbols where
n>k. This structure is called an (n,k) code. The ratio k/n is called the code rate. A key
point is that each codeword is formed independently from other codewords.

An error-control code is a linear code if the transmitted signals are a linear function of the
information symbols. The code is called a systematic code if the information symbols are
transmitted without being altered. Most block codes are systematic, whereas most
convolutional codes are nonsystematic. Almost all codes used for error control are linear.
The symbols in a code can be either binary or non-binary. Binary symbols are the
familiar '0' and '1'.

5.2     Linear Block Codes
Linear block coding is a generic coding method. Other coding methods, such as
Hamming and BCH codes, are special cases of linear block coding. The codeword vector
of a linear block code is a linear mapping of the message vector. The codeword x and the
message m have the relationship

        x  mG

where G is a K-by-N matrix and is known as the generator matrix.

Linear block code is called a systematic linear code if the generator matrix has the form

        G  P I k 

where P is an (n-k)-by-k matrix and I k is a k-by-k identity matrix. A systematic linear
code renders a length k message into a length n codeword where the last k bits are exactly
the original message and the first (n-k) bits are redundant. These redundant bits serve as
parity-check digits.

IT2: LINEAR BLOCK CODING                                                                    7

Step 1: Start the MATLAB program.
Step 2: In the Command Window, type the MATLAB code line by line, as given in
        Example 1 (see Section 7.1). Observe the result after typing each line.
Step 3: Repeat the procedure for Example 2.

Alternatively, you can type the entire MATLAB code in an m-file (this is actually the
preferred method). By saving the m-file, you can run the code at a later date, or you can
make minor modifications without retyping the entire code, or you can copy the code to
another computer to be executed there.

Step 1: Start the MATLAB program.
Step 2: To view the present working directory, type:
         This is where all your m-files will be stored. To change the present working
         directory, use the "cd" command. For example:
                                     cd d:\infot\exp2
Step 3: Go to File -> New -> M-file. The MATLAB Editor window will open.
Step 4: Type the entire code as given in Example 1 or Example 2 (see Section 7.1 or
Step 5: Save the m-file by going to File -> Save As. Choose a short name for your m-
        file. Make sure it contains no spaces.
Step 6: To run the m-file from the MATLAB Editor window, go to Tools -> Run. You
        can also execute the m-file from the Command Window by typing the m-file
        name (without the “.m”) on the command line.


7.1    Example 1
In this example, assume that the generator matrix for a (6,3) linear block code is:

                                 0 1 1 1 0 0 
                             G  1 0 1 0 1 0
                                             
                                 1 1 0 0 0 1
                                             

IT2: LINEAR BLOCK CODING                                                               8

Specifying the Generator matrix
The generator matrix is specified with the following MATLAB code:

        G = [[0 1 1;1 0 1 ;1 1 0], eye(3)]
        [K,N] = size(G)

(Use the "help" command to discover the action and syntax of each function. e.g. help

Generating message blocks
Let us construct all the possible message blocks, each of length k = 3. There are 2k
possible message blocks.

        msg = [0: 7];
        msg = msg';
        msg = de2bi(msg);
        msg = fliplr(msg)
        msg0 = msg(1, :)

This will produce one of the eight 1 x 3 matirx of binary numbers ranging from 0 to 7,
which is all the possible message vectors. You can list the rest by typing the following

        msgn = msg(n+1, :)

where n = 0, 1, 2, ..., 7.

Coding the message blocks
The next step is to use linear block code to produce the codewords pertaining to each

        code0 = encode(msg0,N,K,'linear',G);
        code0 = code0'

You can repeat the above line to obtain the codewords for the rest of the message blocks
by replacing msg0 with msg1, msg2 and so on and change code0 to code1, code2 and so

Decoding the codeword vectors
We now assume that the transmitted codeword vector is equal to the received codeword
vector. To recover the message vector, the codeword vector needs to be decoded.

IT2: LINEAR BLOCK CODING                                                              9

dmsg0 = decode(code0,N,K,'linear',G);
      dmsg0 = dmsg0'

dmsg0 is the recovered message from the codeword vector msg0. The vector dmsg0 is
then compared using function biterr with the original message vector msg0 to compute
the number of errors and its error ratio.

[number, ratio] = biterr(msg0,dmsg0)

You can repeat the above lines for the rest of the message vectors.

7.2     Example 2
Earlier in Example 1, we had assumed that the transmitted codeword vector = received
codeword vector, which means that there was no noise. Now let us introduce some noise
in the channel.

Generating noise in the channel

noise = [zeros(1,5), randint(1)]

noise is a 1 x 6 matrix where all the elements are zeros except for the last element which
is a random 0 of 1. zeros(1,5) generates 1 x 5 zero element matrix and randint(1)
generates random 0's and 1's in the sixth element.

Combining the noise with transmitted codeword vector
Using the logical operator xor on code0 and noise will give the received codeword vector
over a noisy channel. This in turn results the received codeword vector.

rcv_code0 = xor(code0,noise)

It can be seen that if the last element in the noise vector is a '1', the last bit of the received
codeword vector will be different from the transmitted codeword vector. In other words,
the last bit received is an error.

Decoding the received codeword vector
dmsg0 = decode(rcv_code0,N,K,'linear',G);
      dmsg0 = dmsg0'
To recover the message vector, rcv_code0 is decoded.

Comparing the transmitted and decoded message vector

IT2: LINEAR BLOCK CODING                                                                       10

       [number, ratio] = biterr(msg0,dmsg0)
The transmitted and received message vector is compared using function biterr to
evaluate the number of errors and error ratio.

You can repeat all these procedures for other codeword vectors.

i.     Can you analyze the vector noise and conclude what does it represent for?
ii.   Can you explain why the number of error and error ratio are both zero?

Doing everything in one go
MATLAB can also encode all the possible message blocks in one matrix by using the
following lines

code = encode(msg,N,K,'linear',G)
You can also decode the 8 x 6 codeword matrix by the following lines

       msg = decode(code,N,K,'linear',G)


Problem 1:

Given k = 4; n = 7 and

                               1     1 0 1 0 0 0
                               0     1 1 0 1 0 0
                             G                 
                               1     1 1 0 0 1 0
                                                
                               1     0 1 0 0 0 1

i. generate 16 possible message blocks
ii. generate all the pertaining codeword vectors

Problem        2:

IT2: LINEAR BLOCK CODING                                                           11

i.   Define a new P matrix (see Section 5.2). Use your P matrix to specify another
     generator matrix G for a (7,4) code:
                                       G  P  I k 
     If you have forgotten, refer again to Section 7.1 for an example on how to encode a
     generator matrix.

Use the new G in the noisy channel and then confirm that it is a generator matrix for a
single error correction code. (Hint: Follow the same procedure in Example 2, but this
time replace the old generator matrix with your new G.)

9.       REPORT

Print out and submit your report 2 weeks after the laboratory session. The report should
contain the following:

        Introduction
        Procedures
        Results and Discussion
        Conclusions
        Appendices (MATLAB code)

Lab report writing is an individual work. Fabricating results and copying the report of
others are strictly prohibited. Severe penalties will be imposed for any such offences.

IT2: LINEAR BLOCK CODING                                                                  12

                  FACULTY OF ENGINEERING

                     LAB REPORT SUBMISSION
                 AND ERROR CODING
                   TRIMESTER 2 SESSION 2010/2011

 Student Name:         ………..………………………………………………………………
 Student ID:           ………………………………
 Lab Group No.:        ………………………………
 Degree Major:         TE / MCE
 Declaration of originality:
 I declare that all sentences, results and data mentioned in this report are from my own work.
 All work derived from other authors have been listed in the references. I understand that
 failure to do this is considered plagiarism and will be penalized.
 Note that collaboration and discussions in conducting the experiments are allowed but
 copying and any act of cheating in the report, results and data are strictly prohibited.

                                  Student signature: …………………………………………

 Experiment title:     IT2 – Linear Block Coding
 Experiment Date:      ………………………………
 Table/PC No.:         ………………………………
 Date Submitted:       ……………………................
 Lab Instructor Name: ……………………………………… Verified:………………….
       (Please get your lab instructor signature after they have verified your result)

IT2: LINEAR BLOCK CODING                                                               13

To top