Communication Lab Part-B, Experiment-3 and 4.

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Experiment 3.

Program to encode binary data using a ((3,1,2)/suitably designed) Convolution code


and decode it using MATLAB

Aim: To Write a program to encode binary data using a ((3,1,2)/suitably designed)


Convolution code and decode it using MATLAB

Theory:
Convolutional code is a type of error-correcting code that generates parity symbols via
the sliding application of a boolean polynomial function to a data stream.
Convolutional coding is a widely used coding method which is not based on blocks of bits
but rather the output code bits are determined by logic operations on the present bit in a stream
and a small number of previous bits.
Let consider (3,1,2) convolution code

Program:
clc;
message=input('enter the message sequence:');
P=3; %no. of the shift registers input and output connected to
output adders or
%number of bits in generator sequences
g1=6;% first generator sequence [110]
g2=5;% second generator sequence [101]
g3=7;% third generator sequence[111]

trellis=poly2trellis(P,[g1 g2 g3]);
disp(message);
L=length(message);
ms=[message,zeros(1,(P-1))];% pad k-1 zeros to get the
complete encoded sequence.
encoded_data=convenc(ms, trellis); % encoded data will be of
legth n(L+m) in a (n,k,m) Convolution code
disp('Encoded data:');
disp(encoded_data);
decode=vitdec(encoded_data, trellis,L,'trunc','hard');
decoded_data=decode(1:L);
disp('Decoded data:');
disp(decoded_data);
Experiment 4.
Use of CRC-CCITT polynomial to obtain the CRC code. Verify the program for the
cases a) Without error b) With error

Aim: For a given data, use CRC-CCITT polynomial to obtain the CRC code. Verify the
program for the cases a) Without error b) With error

Theory:

Whenever digital data is stored or interfaced, data corruption


might occur. Since the beginning of computer science, developers have been
thinking of ways to deal with this type of problem. For serial data they came
up with the solution to attach a parity bit to each sent byte. This simple
detection mechanism works if an odd number of bits in a byte changes, but
an even number of false bits in one byte will not be detected by the parity
check. To overcome this problem developers have searched for mathematical
sound mechanisms to detect multiple false bits. The CRC calculation or
cyclic redundancy check was the result of this. Nowadays CRC calculations
are used in all types of communications. All packets sent over a network
connection are checked with a CRC. Also each data block on your hard disk
has a CRC value attached to it. Modern computer world cannot do without
these CRC calculations. So let's see why they are so widely used. The answer
is simple; they are powerful, detect many types of errors and are extremely
fast to calculate especially when dedicated hardware chips are used.
The idea behind CRC calculation is to look at the data as one
large binary number. This number is divided by generator and the remainder
of the calculation is called the CRC.
All of the CRC formulas you will encounter are simply
checksum algorithms based on modulo-2 binary division where we ignore
carry bits and in effect the subtraction will be equal to an exclusive or
operation. Though some differences exist in the specifics across different
CRC formulas, the basic mathematical process is always the same:

• The message bits are appended with k-1zero bits; this augmented
message is the dividend
• A pre determined k-bit binarysequence,called the generator polynomial, is
the divisor
• The checksum is the c-bit remainder that results from the division operation

Table 1 lists some of the most commonly used generator polynomials for 16- and 32-
bit CRCs. Remember that the width of the divisor is always one bit wider than the
remainder. So, for example, you’d use a 17-bit generator polynomial whenever a 16-
bit checksum is required.

Table1.InternationalStandardCRCPolynomials
CRC-CCITT CRC-16 CRC-32
Checksum Width 16 bits 16 bits 32 bits
Generator x32+x26+x23+x22+x16+x12+x11+x10
x16+x12+x5+1
Polynomial x16+x15+x2+1 +x8+x7+x5+x4+x2+x+1
Generator 10001000000 11000000000000101 100000100110000010001110110
Polynomialinbits 100001
110111

Error detection with CRC

• Consider a message represented by the polynomial M(x)


• Consider a generating polynomial G(x).This is used to generate a CRC=C(x)to be
appended to M(x).
• Note this G(x)is prime.

Steps:
a. MultiplyM(x)by highest power in G(x).i.e. Add So much zeros to M(x).
b. Divide the result by G(x).The remainder=C(x). Special case:
This won’t work if bit string=al lzeros.We don't allow such
anM(x).But M(x) bitstring = 1 will work, for example. Can
divide 1101 into1000.
c. Transmit: T(x)=M(x) +C(x)
d. Receiver end :Receive T(x).Divide by G(x),shouldhaveremainder0

Note if G(x) has order n-highest power is xn, then G(x) will cover
(n+1)bits and the remainder will cover n bits. i.e. Add n bits (Zeros)
to message.

Fig a: Division at the Sender site:


Fig b. Division at the receiver side

Program:

clc;
clear;

% Polynomial and data input


g = '10011';
N = length(g) - 1;
t = input('Enter data: ', 's');

fprintf('Generating polynomial: %s\n', g);

% Pad the data with zeros


a = length(t);
t(a+1:a+N) = '0';

fprintf('Padded data is: %s\n', t);

% Calculation of CRC
cs = t
for i = 1:a
if cs(i) == '1'
for j = 0:N
cs(i+j) = num2str(xor(str2double(cs(i+j)),
str2double(g(j+1))))
end
end
end

fprintf('Computed CRC is: %s\n', cs(a+1:end));

% Error detection
e = input('Test error detection? 0 (yes) 1 (no): ');
if e == 0
e_pos = input('Enter the position where the error is to be
inserted: ');
while e_pos == 0 || e_pos > a+N
e_pos = input('Please enter a valid position: ');
end
t(e_pos) = char(1 - (t(e_pos) - '0') + '0');
fprintf('Erroneous data: %s\n', t);
end

cs = t;
for i = 1:a
if cs(i) == '1'
for j = 0:N
cs(i+j) = num2str(xor(str2double(cs(i+j)),
str2double(g(j+1))));
end
end
end

error_detected = any(cs(a+1:end) ~= '0');

if error_detected && e == 0
fprintf('Error detected\n\n');
elseif e == 1
fprintf('No error detected\n\n');
end

You might also like