ITCCourse Project 2GI17EC018
ITCCourse Project 2GI17EC018
ETY’
S
GOGTEI
NSTI
TUTEOFTECHNOLOGY
UDYAMBAG,
BELAGAVI -
590008
(
AnAut
onomousI
nst
it
uti
ou
nnderVi
svesv
aray
aTechnol
ogi
cal
Uni
ver
sit
y,Bel
agav
i)
(
APPROVEDBYAI
CTE,
NEW DELHI )
Depar
tmentofElect
roni
csand
Communicat
ion
2019
-2020
Cour
seAct
ivi
ty
Repor
t
I
MPLEMENTATI
ON OFSHANNON BI
NARY ENCODI
NG AND
DECODI
NG USI
NG MATLAB
I
NFORMATI NG-
ONTHEORYANDCODI 16EC63
Submi
tt
edintheparti
alful
fi
ll
mentf ort
heawardofthedegr
eeof
BachelorofEngineeri
ng
I
n
El
ect
roni
csand Communi cati
onEngi
neeri
ng
Submi
tt
edby
NAMEOFCANDI DATES USN
ABHAYKAGWAD 2GI
17EC002
ANUP ANGADI 2GI
17EC018
GOGTEI
NSTI
TUTEOFTECHNOLOGY
UDYAMBAG,BELAGAVI-
590008
(
AnAut
onomousI
nst
it
uti
onunderVi
svesvar
ayaTechnol
ogi
cal
Uni
ver
sit
y,Bel
agav
i)
(
APPROVEDBYAI CTE,NEW DELHI)
Depar
tmentofEl
ect
roni
csandCommuni
cat
ion
CERTI
FICATE
Cert
if
iedthatthecourseacti
vi
tyent
it
ledI
MPLEMENTATION OFSHANNON BINARY
ENCODING ANDDECODI NG USING MATLABdoneatKLSGogt
eInst
it
uteof
Technol
ogy,Belagav
iisabonafi
deworkcar
ri
edoutby
Mr
.AbhayKagwad USN 2GI
17EC002,
Mr
.AnupAngadi USN 2GI
17EC018,
i
n par t
ial f ulfi
l
ment f or t he awar d of Bachel orof Engi neering i n
Electr
onicsandCommuni cationoft heVi
sv esv
ar ayaTechnologicalUni
versity
,Belagavi
duringthey ear2019-2020
I
ti scerti
fiedthatallcorrecti
ons/suggesti
onsi ndicat
edhav ebeeni ncorporatedi nthe
report.Theact i
vi
tyreporthasbeenappr ovedasi tsat
isfi
estheacademi cr equirements
i
nr espectofcour seacti
vityprescr
ibedforthesaidDegr ee.
Nameoft
hest
udent USN Mar
ks Si
gnat
ure
1. AbhayKagwad 2GI
17EC002
2. AnupAngadi 2GI
17EC018
Signat
ureofthest
affi
n
chargeDate:
25-5-
2020
Obj
ect
ive:
Towr
it
eaMATLABcodet
oimpl
ementShannonBi
nar
yEncodi
ngand
Decodi
ngAl
gor
ihm u
t si
ngst
ri
ngasinput.
I
ntr
oduct
ion:
Intoday’
swor ldofdi git
ali
zati
on,allt
hedatawhi chistobepr ocessed
ortransmi t
ted orr eceived should contai
n memor yorbi tsasl essas
possibl
e.Thus,dat acompr essi
onisthepr ocessofmodi f
ying,encodingor
converti
ngt hebitsst r
uctureofdat ainsuchawayt hati
tconsumesl ess
tr
ansmi ssi
onbandwi dth.Itenablesr educingthest or
agesi zeofoneor
moredat ainstances orel ements.Data compr essi
onis also known as
sourcecodingorbi t
-ratereducti
on.
I
nt hef i
eldofdatacompr essi
on,Shannoncoding,namedaf t
erits
creator,Claude Shannon, i s an ent ropy encodedlossless dat a
compr essiontechni
queforconst r
ucti
ngapr efi
xcodebasedonasetof
symbol sandt hei
rprobabi
li
ties(est
imatedormeasur ed).I t
,t her
efor
e,
assigns a unique code to each symbolbased on thei
rpr obabil
it
yof
occurrence.
Thi
scodi ngmet hodgaveri
setot hefi
eldofinf
ormati
ontheoryand
wit
houti ts contr
ibut
ion,t
he world would nothave any ofthe many
successors;for example Shannon-
Fano coding,Huffman coding,or
ari
thmeticcoding.
Theor
y:
ShannonBinaryEncodingAl gor
it
hm takessymbolsasi nputalong
wit
ht hei
rprobabil
i
tyofoccur rence.Heresymbolsrepr
esentanygener al
charact
erornumer i
cal.Inthi
scasewehav echosensomecommonpai rs
oflett
ersofEngli
shal phabetassy mbol
swhichcomewi t
hpre-est
imated
fr
equencyofoccurrence.
Let
terf
requency
:Iti
ssi
mpl
ytheamountoft
imesl
ett
ersoft
he
alphabetorpair
sofalphabet
sappearonaverageinwrit
tenlanguage.
Frequencyanalysi
s(alsoknownascount i
nglett
ers)i
st hestudyof
thef requencyoflet
tersorgroupsofl et
tersinaci phert
ext.The
met hodisusedasanai dt
obreaki
ngclassi
calci
phers.
Fr
equencyanal y
sisisbasedont hef actthat,inanygi venstretchof
wri
ttenlanguage,certai
nl et
tersandcombi nat
ionsofl ett
ersoccurwi th
var
ying fr
equencies.Mor eover,thereis a char acteri
stic di
str
ibuti
on of
l
ett
er st
hatisroughlythesamef oral mostallsamplesoft hatlanguage.For
i
nstance,givenasectionofEngl i
shlanguage,E,T,AandOar ethemost
common,whi l
eZ,QandXar er are.Li
kewise,TH,ER,ON,andANar ethe
mostcommonpai rsoflet
ters(termedbigramsordi graphs),andSS,EE,TT,
andFFar ethemostcommonr epeats.
The let
terfrequencyis being used t
o predictthe occurrence of
character
sorpai rsofcharacter
s(al
phabet)oneaftertheother.Theref
ore,
usi
ngt hi
spr obabi
li
tyofoccur r
ence,Shannonbinaryencodingal gori
thm
can beappl ied toreducet hechunksi zeofeach sy mbolsand make
eff
ect i
veuseofbandwi dthofchannel
.
Below tabl
edepictsthefrequencyofoccur renceofsomecommon
pai
rs ofEngl i
shalphabets.From thet abl
ei tiscleart
hatpair‘t
h’has
hi
ghestprobabil
i
tyandthatof‘i
s’hasleastprobabil
it
yofoccur
rence.
Al
gor
it
hm:
St
ep1:Li
stthesour
cesy
mbol
sint
heor
deroft
hei
rdecr
easi
ng
pr
obabi
li
ti
es.
Step2:Det
ermi
nesetofi
nteger
s‘l
i
’whi
char
ethesmal
l
esti
ntegersol
uti
on
ofthe
i
nequal
i
ties.
Forbi
nar
yr=2,
Log2(
1/Pi)≤l
i≤Log2(
1/Pi)+1
wher
e,i
=1,
2,
……,
m
St
ep3:
Comput
esequenceFisucht
hat
,
F1=0,
F2=P1,
F3=P1+P2
Fm=P1+P2+…….
+Pm-1
Fm+1=1=Fm+Pm
St
ep4:
Expanddeci
mal
number
sFii
nbi
nar
yfor
m upt
olpl
i aces
Step5:Remov
alofbi
nar
ycoder
esul
tsi
ntodesi
redcode(
whi
chi
sencoded
partof
i
nputst
ri
ngofsy
mbol
s)
St
ep6:Att
her
ecei
verendcodesar
edecodedi
ndi
vi
dual
l
ytogetbacki
nput
st
ri
ng.
MATLAB Code:
cl
c;
cl
earal
l
;
cl
oseal
l
;
al
phabet
={'
th'
,
'he'
,
'an'
,
'i
n'
,'
er'
,
'nd'
,
'r
e',
'
ed'
,
'es'
,
'ou'
,
't
o',
'
ha'
,
'en'
,
'ea'
,
'st
',
'
nt'
,
'on'
,
'at
',
'
hi'
,
'
as'
,
'i
t'
,
'ng'
,
'i
s'
}; %pai
rofal
phabett
hatcanbecodedanddecoded
P1=[
.0730.
0702.
0581.
0579.
0569.
0546.
0533.
0526.
0515.
0515.
0515
.
0514.
0511.
0510.
0309.
0306.
0306.
0304.
0197.
0195.
0193.
0192.
0152]
%pr
obabi
l
ityofoccur
enceofeachpai
rofal
phabetr
espect
ivel
y
N=l
engt
h(P1)
P=sor
t(P1,
'
descend'
);
su1=sum(
P);
aq=num2st
r(su1)
; %t
oconv
ertt
ost
ri
ngsot
hatwhi
l
econv
ert
ingbackt
o
number
su2=st
r2doubl
e(aq) %wewi
l
lgeti
nteger
.
i
f(
su2~=1) %t
ocheckwhet
hersum ofal
lthepr
obabi
l
ityi
sone
h=msgbox(
'sum ofpr
obabi
l
ityshoul
dbeone'
,
'Er
ror
',
'
err
or'
);
r
etur
n;
el
se
di
sp(
't
otal
probabi
l
itysum i
sone'
);
end
[
r1,
c1]
=si
ze(
P); %t
ofi
ndt
otal
noofpr
obabi
l
iti
esgi
ven
%pause(
5);
i
f(
N~=c1)
h=msgbox(
'pr
obabi
l
itydi
mensi
oni
snotmat
chi
ngwi
thgi
vennoof
sy
mbol
s'
,'
Err
or'
,
'er
ror
')
;
r
etur
n;
end
%f
oll
owi
ngdecl
arat
ioni
nor
dert
oal
l
ocat
ememor
yandi
ncr
easespeedof
%cal
cul
ati
on
m=zer
os(
1,N)
;%t
ocal
cul
ate1+l
og2(
1/P(
i)
m1=zer
os(
1,N)
;%t
ocal
cul
atel
og2(
1/P(
i)
H=0;
r
esul
t=st
ri
ngs(
1,N)
;
r
esul
t1=zer
os(
1,N)
; %t
ogetcodewor
d
f
=zer
os(
1,N)
; %t
ofi
ndcumul
ati
vepr
obabi
l
ity
g=zer
os(
1,N)
; %v
ari
abl
etost
orebi
nar
yconv
ersi
onof
i
nteger
k=zer
os(
1,N)
; %t
oret
ainonl
y'0'
or'
1'
N_
bar
=0; %t
ocal
cul
ateav
eragecodel
engt
h
f
ori
=1:
N
H=H+P(
i)
*log2(
1/(
P(i
))
); %ent
ropyofsour
ce
m(
i)
=1+l
og2(
1/P(
i)
); %f
indcodel
engt
h
m1(
i)
=log2(
1/P(
i)
); %codel
engt
h
t
emp=m1(
i)
-f
ix(
m1(
i)
); %casewher
eint
egerappeari
nm
i
f(
temp==0)
m(
i)
=m1(
i)
;
end
r
=fi
x(m(
i)
); %t
ogeti
ntegerv
alue
N_
bar
=N_
bar
+r*
P(i
); %t
ofi
ndav
eragecodel
engt
h
i
f(
i==1)
f
(i
)=0; %cumul
ati
vef
orf
ir
st
el
se
f
(i
)=P(
i-
1)+f
(i
-1)
; %cumul
ati
vepr
obabi
l
ity
end
f
orj
=1:
r %l
oopt
ogetcodewor
dsofsi
zer
i
f(
j==1)
g(
j)
=f(
i)
*2; %f
indi
ngt
hebi
nar
yval
ue
el
se
g(
j)
=g(
j-
1)*
2; %f
indbi
nar
yval
ue
end
k(
j)
=fi
x(g(
j)
); %t
ogetonl
y0and1
i
f(
g(j
)>=1)
g(
j)
=g(
j)
-1;
end
i
f(
j==1) %f
ir
stsy
mbol
hasf
=0t
her
efor
ethecodewor
dcont
ains0
atf
ir
st
r
esul
t1=spr
int
f('
%0*
d',
1,
(k(
j)
));
%get
ti
ngst
ri
ngv
aluesot
hateasi
l
yconv
ert
edt
obi
nar
y
el
se
i
f(
k(j
)==0)
r
esul
t2=spr
int
f('
%0*
d',
1,
(k(
j)
));
%inot
hercodewor
dsi
fel
ement
goti
szer
otoconcat
enat
econv
ert
ingt
ost
ri
ng
r
esul
t1=st
rcat
(resul
t1,
resul
t2)
;%mer
gingofst
ri
ngs
el
se
r
esul
t1=st
rcat
(resul
t1,
num2st
r(k(
j)
)); %concat
enat
ion
end
end
end
r
esul
t(
i)
=resul
t1; %mat
ri
xel
ement
sassi
gnedwi
thst
ri
ngs
end
ef
f=H/
N_bar
;
di
sp(
'ent
ropy
')
;H
di
sp(
'av
ergenoofbi
tspersy
mbol
'
);
N_bar
di
sp(
'ef
fi
ciency
')
;ef
f
di
sp(
'bi
nar
yofsy
mol
s'
);
resul
t %di
spl
ayt
hecodewor
ds
di
sp(
'codef
orsy
mbol
wit
hpr
obabi
l
iti
es'
);
P
pause(
.5)
di
sp(
'ar
e')
;r
esul
t
di
sp(
'r
espect
ivel
y'
)
i
np=i
nput
("
Ent
eri
nput
::
",
"s"
) %i
nputast
ri
ngt
obeencoded
%encodi
ngt
akesher
e
j
=1;
f
ori
=1:
2:l
engt
h(i
np)
-1
st
ri
ng1=st
rcat
(i
np(
i)
,i
np(
i+1)
); %gr
oupi
ng2char
act
ersofi
nputst
ri
ng
successi
vel
y
i
ndex=st
rcmp(
alphabet
,st
ri
ng1)
; %f
indt
hei
ndexofgr
oupchar
act
er
t
hatmat
cheswi
thgr
oupedal
phabet
encode(
j)
=resul
t(
index)
; %r
etr
ivet
hecodewor
dfr
om t
hei
ndex
f
ound
j
=j+1;
end
di
spl
ay(
encode) %di
spl
aycodewor
dar
ray
%decodi
ngt
akesher
e
f
ori
=1:
l
engt
h(encode)
i
ndex2=f
ind(
ismember
(resul
t,
encode(
i)
)); %f
indt
hei
ndexofcodewor
d
f
rom encodemat
ri
x
decode(
i)
=al
phabet
(i
ndex2)
; %r
etr
ivet
hegr
oupedal
phabet
f
rom t
hei
ndexf
ound
end
di
spl
ay(
decode) %di
spl
aydecodedst
ri
ng
Out
put
:
Appl
icat
ions:
1)Usedint hefi
eldofdatacompr on.
essi Si
nceShannonbi
naryencodi
ng
techni
quei susedtoreducet
hechunksizeofeachsymbol
sandmake
effect
iveuseofbandwidthofchannel
.
2)Iti
salsousedinani
mpl
odecompr
essi
onmet
hodwhi
char
eusedi
n
zipfi
l
eor.rarf
ormat
.
3)Wi t
hlit
tl
emodi fi
cati
ons,t
histechni
quecanbeappl
iedinthef
iel
dof
Cryptography(
fordevel
opingasecuredatat
ransmi
ssionand
recepti
onov eralossl
esschannel).
Obser
vat
ions:
I
tis obser
ved t
hatt
he al
phabetpai
rwi
th hi
ghestpr
obabi
l
ity of
occur
rencewi
l
lhav
eleastcodewor
dlengt
handv
ice-
ver
sa.
Al
so,t
hesy
mbolpai
rwi
thhi
ghestoccur
rencepr
obabi
l
itywi
l
lconv
ey
l
easti
nfor
mat
ionascompar
edt
oot
heral
phabet
s(sy
mbol
s)wi
thl
ess
occur
rencepr
obabi
l
ity
.
Concl
usi
on:
Successf
ull
yimplement
edShannonBi nar
yEncodingandDecoding
Algor
it
hm usingMATLAB.Thistechni
quecanfurt
herbeextendedto
numeric,
special
symbolpai
rs(
foreg:@,#,%,
$…)andev enwhi t
espace.