-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithcode.m
73 lines (62 loc) · 1.36 KB
/
arithcode.m
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
function enc = arithcode(inx, a)
N = 22;
P = 8;
C = 0;
A = 2^N;
r = -1;
b = 0;
enc = [];
pc= floor(a * 2^P);
ps = floor((1-a) * 2^P);
pre = inx(1);
for xn = inx
if xn ~= pre
T = A * pc;
C = C + 0;
else
T = A * ps;
C = C + T;
end
if C >= 2^(N+P)
C = mod(C, 2^(N+P));
enc = [enc 1];
if r > 0
enc = [enc zeros(1,r-1)];
r = 0;
else
r = -1;
end
end
while T < 2^(N+P-1)
b = b + 1;
T = 2 * T;
C = 2 * C;
if C >= 2^(N+P)
C = mod(C, 2^(N+P));
if r < 0
enc = [enc 1];
else
r = r + 1;
end
else
if r >= 0
enc = [enc 0 ones(1,r)];
end
r = 0;
end
end
A = floor(T / 2^P);
pre = xn;
end
if r >= 0
enc = [enc 0 ones(1,r)];
end
tc = dec2binvec(C);
if length(tc) > (N + P)
tc = tc(end-(N+P)+1:end);
else
hl=N+P-length(tc);
tc=[tc zeros(1,hl)];
end
enc = [enc tc];
end