-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic_coding.py
320 lines (223 loc) · 7.88 KB
/
arithmetic_coding.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import sys
import random
import string
import decimal
from decimal import Decimal
class ArithmeticEncoding:
def __init__(self, frequency_table, save_stages=False):
self.save_stages = save_stages
if(save_stages == True):
print("")
self.probability_table = self.get_probability_table(frequency_table)
def get_probability_table(self, frequency_table):
total_frequency = sum(list(frequency_table.values()))
probability_table = {}
for key, value in frequency_table.items():
probability_table[key] = value/total_frequency
return probability_table
def get_encoded_value(self, last_stage_probs):
last_stage_probs = list(last_stage_probs.values())
last_stage_values = []
for sublist in last_stage_probs:
for element in sublist:
last_stage_values.append(element)
last_stage_min = min(last_stage_values)
last_stage_max = max(last_stage_values)
encoded_value = (last_stage_min + last_stage_max)/2
return last_stage_min, last_stage_max, encoded_value
def process_stage(self, probability_table, stage_min, stage_max):
stage_probs = {}
stage_domain = stage_max - stage_min
for term_idx in range(len(probability_table.items())):
term = list(probability_table.keys())[term_idx]
term_prob = Decimal(probability_table[term])
cum_prob = term_prob * stage_domain + stage_min
stage_probs[term] = [stage_min, cum_prob]
stage_min = cum_prob
return stage_probs
def encode(self, msg, probability_table):
msg = list(msg)
encoder = []
stage_min = Decimal(0.0)
stage_max = Decimal(1.0)
for msg_term_idx in range(len(msg)):
stage_probs = self.process_stage(probability_table, stage_min, stage_max)
msg_term = msg[msg_term_idx]
stage_min = stage_probs[msg_term][0]
stage_max = stage_probs[msg_term][1]
if self.save_stages:
encoder.append(stage_probs)
last_stage_probs = self.process_stage(probability_table, stage_min, stage_max)
if self.save_stages:
encoder.append(last_stage_probs)
interval_min_value, interval_max_value, encoded_msg = self.get_encoded_value(last_stage_probs)
return encoded_msg, encoder, interval_min_value, interval_max_value
def process_stage_binary(self, float_interval_min, float_interval_max, stage_min_bin, stage_max_bin):
stage_mid_bin = stage_min_bin + "1"
stage_min_bin = stage_min_bin + "0"
stage_probs = {}
stage_probs[0] = [stage_min_bin, stage_mid_bin]
stage_probs[1] = [stage_mid_bin, stage_max_bin]
return stage_probs
def decode(self, encoded_msg, msg_length, probability_table):
decoder = []
decoded_msg = []
stage_min = Decimal(0.0)
stage_max = Decimal(1.0)
for idx in range(msg_length):
stage_probs = self.process_stage(probability_table, stage_min, stage_max)
for msg_term, value in stage_probs.items():
if encoded_msg >= value[0] and encoded_msg <= value[1]:
break
decoded_msg.append(msg_term)
stage_min = stage_probs[msg_term][0]
stage_max = stage_probs[msg_term][1]
if self.save_stages:
decoder.append(stage_probs)
if self.save_stages:
last_stage_probs = self.process_stage(probability_table, stage_min, stage_max)
decoder.append(last_stage_probs)
return decoded_msg, decoder
# In[2]:
def floattobinary(float_num, num_bits=None):
float_num = str(float_num)
if float_num.find(".") == -1:
# No decimals in the floating-point number.
integers = float_num
decimals = ""
else:
integers, decimals = float_num.split(".")
decimals = "0." + decimals
decimals = Decimal(decimals)
integers = int(integers)
result = ""
num_used_bits = 0
while True:
mul = decimals * 2
int_part = int(mul)
result = result + str(int_part)
num_used_bits = num_used_bits + 1
decimals = mul - int(mul)
if type(num_bits) is type(None):
if decimals == 0:
break
elif num_used_bits >= num_bits:
break
if type(num_bits) is type(None):
pass
elif len(result) < num_bits:
num_remaining_bits = num_bits - len(result)
result = result + "0"*num_remaining_bits
integers_bin = bin(integers)[2:]
result = str(integers_bin) + "." + str(result)
return result
def binarytofloat(bin_num):
if bin_num.find(".") == -1:
# No decimals in the binary number.
integers = bin_num
decimals = ""
else:
integers, decimals = bin_num.split(".")
result = Decimal(0.0)
# Working with integers.
for idx, bit in enumerate(integers):
if bit == "0":
continue
mul = 2**idx
result = result + Decimal(mul)
# Working with decimals.
for idx, bit in enumerate(decimals):
if bit == "0":
continue
mul = Decimal(1.0)/Decimal((2**(idx+1)))
result = result + mul
return result
# In[3]:
def xor(actual, random, j):
result = '0.'
n=len(actual)-2
k=2
for i in range(j,n+j):
if actual[k] == random[i]:
result += "0"
else:
result += "1"
k=k+1
return result
# In[4]:
def random_pattern(k, n):
output = 0
for d in random.sample(range(n), k):
output += (1 << d)
temp = bin(output)[2:]
if len(temp) < n:
temp = temp[::-1]
temp1 = temp + '0' * (n - len(temp))
temp1 = temp1[::-1]
return temp1
else:
return temp
# In[5]:
def binarytoascii(str2):
message = ""
while str2 != "":
i = chr(int(str2[:8], 2))
message = message + i
str2 = str2[8:]
return message
# In[6]:
f = open("text_pattern.txt")
original = f.read()
x = ''.join(format(ord(i), '08b') for i in original)
# In[7]:
test_str = x
chunk_len = 24
res = [test_str[idx : idx + chunk_len] for idx in range(0, len(test_str), chunk_len)]
# In[8]:
# Encode the data
AE_object=[]
binary_codes=[]
M=0
for value in res:
frequency_table = {'0': 1, '1': 1}
AE = ArithmeticEncoding(frequency_table=frequency_table)
original_msg = value
# Encode the message
encoded_msg, encoder , interval_min_value, interval_max_value = AE.encode(msg=original_msg, probability_table=AE.probability_table)
# Get the binary code out of the floating-point value
binary_code = floattobinary(encoded_msg)
binary_codes.append(binary_code)
AE_object.append(AE)
M = M+len(binary_code)-2
# In[9]:
print("Space usage in bits before compression:", len(x))
print("Space usage in bits after compression:", M)
# In[10]:
dist = [0,10,100,200,500,5000]
print("Original text")
print(original)
print()
for d in dist:
print('d = ', d)
# generate random binary string of length M' : CHANNEL ERROR
M_error = random_pattern(d, M)
j=0
d_temp=[]
for i in range(0,len(AE_object)):
# take xor of text file and random string :TRANSMITTED MESSAGE
y = xor(binary_codes[i], M_error, j)
j=j+len(binary_code)-2
encoded_msg = binarytofloat(y)
# Decode the message
decoded_msg, decoder = AE_object[i].decode(encoded_msg=encoded_msg, msg_length=chunk_len,
probability_table=AE_object[i].probability_table)
decoded_msg = "".join(decoded_msg)
d_temp.append(decoded_msg)
decoded="".join(d_temp)
final=binarytoascii(decoded)
print(final)
print()
# In[ ]: