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

K. J.

Somaiya College of Engineering, Mumbai-77


(An Autonomous College Affiliated to University of Mumbai)

Batch: A4 Roll No.: 1923008


Experiment / assignment / tutorial No. 01
Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

TITLE: Source Coding


AIM: To develop a program for implementation of source coding algorithm.

OUTCOME: Students will be able to generate codebook for a given phrase compactly.

Problem: To encode a tongue- twister phrase compactly.

Some of the tongue- twister phrases are given below:

1. Peter piper picked a peck of pickled peppers.


2. Better Botter bought some butter.
3. She sells seashells by the sea shore.
4. I scream , you scream, we all scream for ice cream.

Selected tongue twister – “ peter piper picked a peck of pickled peppers “

Department of Electronics and Telecommunication Engineering

Page No DCOMM/Sem V/July-Nov 2020


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

Write answers to the following Pre-Laboratory questions:

Q1. How many different number of symbols are there in the phrase?
Ans: 14 symbols
[' ', 'a', 'c', 'd', 'e', 'f', 'i', 'k', 'l', 'o', 'p', 'r', 's', 't']

Q2. Calculate the no. of code words representing different symbols.

Symbol Frequency Code Word


space 7 0000
a 1 0001
c 3 0010
d 2 0011
e 8 0100
f 1 0101
i 3 0110
k 3 0111
l 1 1000
o 1 1001
p 9 1010
r 3 1011
s 1 1100
t 1 1101

Q3. How many bits are needed for coding characters in this phrase using fixed length code?

Ans: To encode fourteen unique symbols we would need four bits, for 24 = 16 different codewords.
∴ This gives 4×44 = 176 bits

Q4 Determine theoretical minimum no. of bits required to encode the entire phrase assuming that each character is
independent of other surrounding character.

p e t e r p i p e r
1010 0100 1101 0100 1011 0000 1010 0110 1010 0100 1011 0000

Department of Electronics and Telecommunication Engineering

Page No DCOMM/Sem V/July-Nov 2020


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

p i c k e d a p e c
1010 0110 0010 0111 0100 0011 0000 0001 0000 1010 0100 0010

k o f p i c k l e d
0111 0000 1011 0101 0000 1010 0110 0010 0111 1000 0100 0011

p e p p e r s
0000 1010 0100 1010 1010 0100 1011 1100

Procedure:
1. Select a tongue- twister phrase yourself or from the list given.
2. Develop a program of Huffman coding or LZ algorithm to generate a codebook for the symbols in the phrase.
(Use MATLAB or any other available software)

MATLAB CODE for Huffman Algorithm :

%calculation of frequency/probability of each character


clc;
clear all;
text=char("peter piper picked a peck of pickled peppers"); % taking the text data

fprintf('1. Original string = ')


fprintf(text)
fprintf('\n');

%converting text data into ascii valuet=double(text);


t=double(text);
%getting the length of the string passed
%for finding probability
N=length(t);

%creating an array to store no. of occurrence of each character


frequency=zeros(1,128);

for k=0:127
count=0;
for i=1:N
if(t(i)==k)
count=count+1;
end
end
frequency(k+1)=count; %counting frequency of each character
end

sym=find(frequency)-1; % getting information about used character in paragraph


sym_count=frequency(sym+1); % frequency of used symbols
Department of Electronics and Telecommunication Engineering

Page No DCOMM/Sem V/July-Nov 2020


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

sym_prob=sym_count/N; % probability of symbol

% creating huffman code for symbol


% creating the dictionary corresponding to each used symbol
[dict,avglen]=huffmandict(sym,sym_prob);

%encoding the data


encode_data = huffmanenco(t,dict);

fprintf('\n');
fprintf('2. Encoded data for given string = ')
for i=1:length(encode_data)
fprintf('%d', encode_data(i))
end
fprintf('\n');

fprintf('\n');
fprintf('2. Encoded data for given string = ')
for i=1:length(encode_data)
fprintf('%d', encode_data(i))
end
fprintf('\n');

fprintf('\n');
fprintf('3. Fixed length code = %d bits', length(text)*4)
fprintf('\n');

fprintf('\n');
fprintf("4. Huffman's length code = %d bits", length(encode_data))
fprintf('\n');

%Find entropy of probabilites


entropy=0;
for i=1:length(sym_prob)
entropy=entropy-sym_prob(i)*log2(sym_prob(i));
end

efficiency = (entropy/avglen)*100;

fprintf('\n');
fprintf("5. Efficiency = %d ", round(efficiency))
fprintf('\n');

Department of Electronics and Telecommunication Engineering

Page No DCOMM/Sem V/July-Nov 2020


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

Results:

Program output – Codebook of Huffman code

Write answers to the following Post- Laboratory questions:


Q1. How many bits are needed for encoding the sequence using codebook derived by Huffman coding?

Character No. of Characters Code Bits per character Bits Needed


p 9 00 2 18
e 8 01 2 16
space 7 110 3 21
c 3 1000 4 12
d 2 11100 5 10
i 3 1001 4 12
k 3 1010 4 12
r 3 1011 4 12
a 1 111010 6 6
f 1 111011 6 6
l 1 111100 6 6
o 1 111101 6 6
s 1 111110 6 6
t 1 111111 6 6
Total 44 characters 149

∴ 149 bits are needed for encoding the sequence using codebook derived by Huffman coding

Department of Electronics and Telecommunication Engineering

Page No DCOMM/Sem V/July-Nov 2020


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

Q2. Compare the no. of bits needed using fixed length coding, Huffman coding and LZ algorithm.
Source coding method No. of Bits
Fixed Length Code 176
Huffman Algorithm 149

Q3. Calculate efficiency of each codebook.

Department of Electronics and Telecommunication Engineering

Page No DCOMM/Sem V/July-Nov 2020


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

Signature of faculty in-charge with date

Department of Electronics and Telecommunication Engineering

Page No DCOMM/Sem V/July-Nov 2020

You might also like