This small piece of code achieves arithmetic coding. Ketul Shah from Nirma University is acknowledged as the copyright holder in the license file.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copyright (c) 2014, Ketul Shah
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution
* Neither the name of the nor the names
of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
clc;clear all;
%Arithmatic Coding
%by Ketul Shah
%Nirma University
prompt=' Enter the word ';
str=input(prompt,'s');
arith=str;
len=size(str);
le=len(2);
count=[];
disp('Arithmatic Encoding Started');
for i=1:le-1
count(i)=1;
for j=i+1:le
if str(i)==str(j)
str(j)=0;
count(i)=count(i)+1;
end
end
end
if(str(le)~=0)
count(le)=1;
end
j=1;
%------------Transmitter part--------------------
for i=1:le
if(str(i)~=0)
new(j)=str(i);
p(j)=count(i)/le;
if(j>1)
ar(j)=ar(j-1)+p(j);
else
ar(j)=p(j);
end
disp(['Probability for ',str(i),' is ',num2str(p(j))]);
j=j+1;
end
end
larith=size(new);
l=[];u=[];
l(1)=0;
u(1)=ar(1);
for i=2:le
for j=1:larith(2)
if(arith(i)==new(j))
l(i)=l(i-1)+(u(i-1)-l(i-1))*(ar(j)-p(j));
u(i)=l(i-1)+(u(i-1)-l(i-1))*ar(j);
end
end
end
tag=(l(i)+u(i))/2;
disp(['The tag is ',num2str(tag)]);
%----------------Reciever part--------------
disp('Arithmetic Decoding Started');
rec='a';
tagr=tag;
for i=1:le
for j=1:larith(2)
if(tagr<ar(j) && tagr>(ar(j)-p(j)))
rec(i)=new(j);
nm=j;
end
end
if(nm>1)
tagr=(tagr-ar(nm-1))/p(nm);
else
tagr=tagr/p(nm);
end
end
disp(['Recieved word is ',rec]);
if(rec==arith)
disp('Succesfully Recieved');
else
disp('Sorry not recieved successfully');
end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sample input and output:
Enter the word hello there
Arithmatic Encoding Started
Probability for h is 0.18182
Probability for e is 0.27273
Probability for l is 0.18182
Probability for o is 0.090909
Probability for is 0.090909
Probability for t is 0.090909
Probability for r is 0.090909
The tag is 0.060858
Arithmetic Decoding Started
Recieved word is hello there
Succesfully Recieved
Awesome piece of code.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copyright (c) 2014, Ketul Shah
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution
* Neither the name of the nor the names
of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
clc;clear all;
%Arithmatic Coding
%by Ketul Shah
%Nirma University
prompt=' Enter the word ';
str=input(prompt,'s');
arith=str;
len=size(str);
le=len(2);
count=[];
disp('Arithmatic Encoding Started');
for i=1:le-1
count(i)=1;
for j=i+1:le
if str(i)==str(j)
str(j)=0;
count(i)=count(i)+1;
end
end
end
if(str(le)~=0)
count(le)=1;
end
j=1;
%------------Transmitter part--------------------
for i=1:le
if(str(i)~=0)
new(j)=str(i);
p(j)=count(i)/le;
if(j>1)
ar(j)=ar(j-1)+p(j);
else
ar(j)=p(j);
end
disp(['Probability for ',str(i),' is ',num2str(p(j))]);
j=j+1;
end
end
larith=size(new);
l=[];u=[];
l(1)=0;
u(1)=ar(1);
for i=2:le
for j=1:larith(2)
if(arith(i)==new(j))
l(i)=l(i-1)+(u(i-1)-l(i-1))*(ar(j)-p(j));
u(i)=l(i-1)+(u(i-1)-l(i-1))*ar(j);
end
end
end
tag=(l(i)+u(i))/2;
disp(['The tag is ',num2str(tag)]);
%----------------Reciever part--------------
disp('Arithmetic Decoding Started');
rec='a';
tagr=tag;
for i=1:le
for j=1:larith(2)
if(tagr<ar(j) && tagr>(ar(j)-p(j)))
rec(i)=new(j);
nm=j;
end
end
if(nm>1)
tagr=(tagr-ar(nm-1))/p(nm);
else
tagr=tagr/p(nm);
end
end
disp(['Recieved word is ',rec]);
if(rec==arith)
disp('Succesfully Recieved');
else
disp('Sorry not recieved successfully');
end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sample input and output:
Enter the word hello there
Arithmatic Encoding Started
Probability for h is 0.18182
Probability for e is 0.27273
Probability for l is 0.18182
Probability for o is 0.090909
Probability for is 0.090909
Probability for t is 0.090909
Probability for r is 0.090909
The tag is 0.060858
Arithmetic Decoding Started
Recieved word is hello there
Succesfully Recieved
Awesome piece of code.
No comments:
Post a Comment