0% found this document useful (0 votes)
5 views

DC Lab ITC Experiments

The document outlines various coding techniques including Huffman coding, Hamming coding, convolution coding, and CRC coding. It provides MATLAB code for encoding and decoding binary data using these methods, along with example outputs demonstrating the processes. Additionally, it includes a C program for generating CRC codes and checking for errors in transmitted data.

Uploaded by

vishalinivi07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DC Lab ITC Experiments

The document outlines various coding techniques including Huffman coding, Hamming coding, convolution coding, and CRC coding. It provides MATLAB code for encoding and decoding binary data using these methods, along with example outputs demonstrating the processes. Additionally, it includes a C program for generating CRC codes and checking for errors in transmitted data.

Uploaded by

vishalinivi07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

HUFFMAN CODING AND DECODING

AIM:

To write a program to encode binary data using Huffman


coding and decoding
MATLAB CODE:
x=input('enter the number of symbols:');
N=1:x;
disp('The number of symbols are N:');
disp(N);
P=input('Enter the probabilities=');
disp(P);
S=sort(P,'descend');
disp('The sorted probabilities are :');
disp(S);
[dict,avglen]=huffmandict(N,S) %returns the average codeword length of the
Huffman code.
disp('The average length of the code is:');
disp(avglen);
H=0;
for i=1:x
H=H+(P(i)*log2(1/P(i)));
end
disp('Entropy is:');
disp(H);
disp('bits/msg');
E=(H/avglen)*100;
disp('Efficiency is:')
disp(E);
codeword=huffmanenco(N,dict);
disp('The codewords are:');
disp(codeword);
decode=huffmandeco(codeword, dict);
disp('Decoded output is:');
disp(decode);

OUTPUT:

enter the number of symbols:4

The number of symbols are N:

1 2 3 4
Enter the probabilities=[0.4 0.3 0.2 0.1]

0.4000 0.3000 0.2000 0.1000

The sorted probabilities are :

0.4000 0.3000 0.2000 0.1000

dict =

4×2 cell array

[1] [ 1]

[2] [1×2 double]

[3] [1×3 double]

[4] [1×3 double]

avglen =

1.9000

The average length of the code is:

1.9000

binits/msg-sym

Entropy is:

1.8464

bits/msg-sym

Efficiency is:

97.1810

The codewords are:

1 0 1 0 0 0 0 0 1

Decoded output is:

1 2 3 4
Hamming coding and decoding
Aim:
To write a program to encode binary data using a (7, 4)
Hamming code and decode it.

MATLAB CODE:

%Hamming code simulation-JC-4/15/06


%To run hit F5 and observe command window
%Simulation for encoding and decoding of a [7,4] Hamming
code. The decoder
%can correct one error as shown and as theory states. The
table at the end
%of the file shows the various outputs with different error
positions and
%message bits. One error can be placed at any of the 7 bit
locations and
%corrections made.
clear
n = 7 %# of codeword bits per block
k = 4 %# of message bits per block
A = [ 1 1 1;1 1 0;1 0 1;0 1 1 ]; %Parity submatrix-Need
binary(decimal combination of 7,6,5,3)
G = [ eye(k) A ] %Generator matrix
H = [ A' eye(n-k) ] %Parity-check matrix
% ENCODER%
msg = [ 1 1 1 1 ] %Message block vector-change to any 4 bit
sequence
code = mod(msg*G,2)%Encode message
% CHANNEL ERROR(add one error to code)%
%code(1)= ~code(1);
%code(2)= ~code(2);
code(3)= ~code(3);
%code(4)= ~code(4);%Pick one,comment out others
%code(5)= ~code(5);
%code(6)= ~code(6);
%code(7)= ~code(7);
recd = code %Received codeword with error
% DECODER%
syndrome = mod(recd * H',2)
%Find position of the error in codeword (index)
find = 0;
for ii = 1:n
if ~find
errvect = zeros(1,n)
errvect(ii) = 1
search = mod(errvect * H',2)
if search == syndrome
find = 1
index = ii
end
end
end
disp(['Position of error in codeword=',num2str(index)]);
correctedcode = recd;
correctedcode(index) = mod(recd(index)+1,2)%Corrected
codeword
%Strip off parity bits
msg_decoded=correctedcode;
msg_decoded=msg_decoded(1:4)

OUTPUT:

n =

k =

G =

1 0 0 0 1 1 1
0 1 0 0 1 1 0
0 0 1 0 1 0 1
0 0 0 1 0 1 1

H =

1 1 1 0 1 0 0
1 1 0 1 0 1 0
1 0 1 1 0 0 1

msg =

1 1 1 1
code =

1 1 1 1 1 1 1

recd =

1 1 0 1 1 1 1

syndrome =

1 0 1

errvect =

0 0 0 0 0 0 0

errvect =

1 0 0 0 0 0 0

search =

1 1 1

errvect =

0 0 0 0 0 0 0

errvect =

0 1 0 0 0 0 0

search =

1 1 0

errvect =

0 0 0 0 0 0 0

errvect =

0 0 1 0 0 0 0

search =

1 0 1
find =

index =

3
Position of error in codeword=3

correctedcode =

1 1 1 1 1 1 1

msg_decoded =

1 1 1 1

%Error position Syndrome Decimal 4 bit word


codeword dmin
% 1 111 7 0000
0000000
% 2 110 6 0001
0001011 3
% 3 101 5 0010
0010101 4
% 4 011 3 0011
0011110 3
% 5 100 4 0100
0100110 3
% 6 010 2 0101
0101101 3
% 7 001 1 0110
0110011 4
%No error will give syndrome of 000 0111
0111000 3
% 1000
1000111 4
% 1001
1001100 3
% 1010
1010010 4
% 1011
1011001 3
% 1100
1100001 3
% 1101
1101010 3
% 1110
1110100 4
% 1111
1111111 3
%Any exclusive or additions of any two codewords should give
another
%codeword.

Convolution Encoder and Decoder


AIM:
To write a program to encode binary data using a (2, 1, 2)
convolution code and decode it.

MATLAB CODE:

clc
K=3;
G1=7;
G2=5;
msg=[1 0 1 1 1];
trel=poly2trellis(K, [G1 G2])
disp('Message sequence:');
disp(msg);
coded=convenc(msg,trel);
disp('Encoder output:');
disp(coded);
tblen = length(msg);
decoded=vitdec(coded,trel, tblen,'trunc','hard');
disp('Decoder output:');
disp(decoded);

OUTPUT:

trel =

struct with fields:

numInputSymbols: 2
numOutputSymbols: 4
numStates: 4
nextStates: [4×2 double]
outputs: [4×2 double]

Message sequence:
1 0 1 1 1

Encoder output:
1 1 1 0 0 0 0 1 1 0

Decoder output:
1 0 1 1 1

NOTE:
nextStates: [4×2 double] %[0,2;0,2;1,3;1,3]
outputs: [4×2 double] %[0,3;3,0;2,1;1,2]

Solution:

Encoder Diagram:

State Table:

State Transition Table:


Codeword for the given message [1 0 1 1 1]:
11, 10, 00, 01, 10, (01, 11)

2. Write a Program to obtain CRC code for the given data, using
CRC-CCITT polynomial

Program
#include <stdio.h>
#include <string.h>
void main()
{
int i,j,k,keylen,msglen,value;
char
data[100],ddata[100],out[100],recv[100],key[30],temp[30],quot[100
],rem[30], key1[30];
printf("Enter Data: ");
scanf("%s", &data);
printf("Enter Key: ");
scanf("%s", &key);
keylen=strlen(key);
msglen=strlen(data);
strcpy(ddata,data);
strcpy(key1,key);
for (i=0;i<keylen-1;i++)
{
data[msglen+i]='0';
}
for (i=0;i<keylen;i++)
temp[i]=data[i];
for (i=0;i<msglen;i++)
{
quot[i]=temp[0];
if(quot[i]=='0')
for (j=0;j<keylen;j++)
key[j]='0';
else
for (j=0;j<keylen;j++)
key[j]=key1[j];
for (j=keylen-1;j>0;j--)
{
if(temp[j]==key[j])
rem[j-1]='0';
else
rem[j-1]='1';
}
rem[keylen-1]=data[i+keylen];
strcpy(temp,rem);
}
strcpy(rem,temp);
printf("\nQuotient is ");
for (i=0;i<msglen;i++)
printf("%c",quot[i]);
printf("\nRemainder is ");
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
strcat(ddata,rem);
printf("\nData to be transmitted is:%s\n", ddata);
printf("\n Enter the Received data:");
scanf("%s", &recv);
value=strcmp(ddata,recv);
if(value==0)
printf("Data is ERROR FREE\n");
else
printf("Error in received data\n");
return 0;
}

Output:
1. Enter Data: 110111011100
Enter Key: 1101

Quotient is 100010001001
Remainder is 101
Data to be transmitted is:110111011100101

Enter the Received data:111111011100101


Error in received data

2. Enter Data: 110110101


Enter Key: 1101

Quotient is 100011011
Remainder is 111
Data to be transmitted is:110110101111

Enter the Received data:110110101111


Data is ERROR FREE

You might also like