-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArith_Code.m
More file actions
79 lines (71 loc) · 2.29 KB
/
Arith_Code.m
File metadata and controls
79 lines (71 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function [Dataout, code]=Arith_Code(Data)
%Data=[128*ones(1,12) 12*ones(1,12) 6 3 24 4 3 8 8];
%%%%%%%%%%%%%%%%% Arithmetic ecoding %%%%%%%%%%%%%%
Table=0; New_Data=0;
%' Compute Table of Symbols ...'
Table(1)=Data(1);
S_AC=size(Data);
for jAC=1:S_AC(2)
S_2AC=size(Table);Flag=0;
for kAC=1:S_2AC(2)
if (Table(kAC)==Data(jAC))
Flag=1;
end;
end;
if (Flag==0) Table(S_2AC(2)+1)=Data(jAC); end;
end;
% ' Compute the probability of the symbols...'
%%-----------------------------------------
S_2AC=size(Table);
Counts(1:S_2AC(2))=0;
for kAC=1:S_AC(2)
iAC=1;
while (Table(iAC)~=Data(kAC))
iAC=iAC+1;
end;
New_Data(kAC)=iAC;
Counts(iAC)=Counts(iAC)+1;
end;
%%%-------------------------------------
code=0;
% ' Apply Arithmetic coding .... Now'
code = arithenco(New_Data,Counts);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Converting bits to ASCII and save it into a file....
% 'converts bits to bytes... Now'
D_Bits=char(code+48); % convert bits to characters '1' and '0'
N_Bits=8; %%% Choose number of bits for convertion
i_B=1; Size_AC=size(code);
D_Bits(Size_AC(2)+1:Size_AC(2)+N_Bits)='0';
i_B=1;k_Loc=1;
while (i_B<=Size_AC(2))
e8bits(1:N_Bits)=D_Bits(i_B:i_B+N_Bits-1);
Store_Byte(k_Loc)=bin2dec(e8bits);
k_Loc=k_Loc+1;
i_B=i_B+N_Bits;
end;
%%%%%%%%%%%%%%%%% Arithmetic decoding %%%%%%%%%%%%%%
Counts=double(Counts);
Store_Byte=double(Store_Byte);
% ' Compute data size'
length_Data=0;
Size_Counts=size(Counts);
for iAD=1:Size_Counts(2)
length_Data=length_Data+Counts(iAD);
end;
% 'Read Data from the array...'
Size_Data=size(Store_Byte);
Loc=1; code=0;
for iAD=1:Size_Data(2)
e8bits=dec2bin(Store_Byte(iAD),8);
code(Loc:Loc+7)=e8bits(1:8);
Loc=Loc+8;
end;
code=code-48;
% 'Apply Arithmetic Decode..'
New_Data = arithdeco(code,Counts,length_Data);
Data=0;
% 'Return original data'
for iAD=1:length_Data
Dataout(iAD)=Table(New_Data(iAD));
end;