-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinite_Arithmetic_Code.py
More file actions
246 lines (187 loc) · 6.78 KB
/
Copy pathFinite_Arithmetic_Code.py
File metadata and controls
246 lines (187 loc) · 6.78 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
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
import numpy as np
def get_symbol_frequencies(data):
"""
Calculate the frequencies of symbols in the input data.
Args:
data (list or np.ndarray): Input data containing symbols.
Returns:
dict: Dictionary containing symbol frequencies.
"""
if isinstance(data, np.ndarray):
unique_elements, frequencies = np.unique(data, return_counts=True)
symbols = dict(zip(unique_elements, frequencies))
else:
symbols = {}
for char in data:
symbols[char] = symbols.get(char, 0) + 1
return symbols
def cumulative_frequency(symbol, frequency_dict):
"""
Calculate the cumulative frequency of a symbol.
Args:
symbol: Symbol for which cumulative frequency is calculated.
frequency_dict (dict): Dictionary containing symbol frequencies.
Returns:
int: Cumulative frequency of the symbol.
"""
cumulative_freq = 0
for sym, freq in frequency_dict.items():
cumulative_freq += freq
if sym == symbol:
break
return cumulative_freq
def arithmetic_encode(input_stream, precision=32):
"""
Encode the input stream using the arithmetic coding algorithm.
Args:
input_stream (list): Input stream of symbols.
precision (int): Number of precision bits used in the encoder.
Returns:
tuple: Encoded binary stream and symbol frequencies dictionary.
"""
input_stream.append('O')
stream_size = len(input_stream)
symbol_freq_dict = get_symbol_frequencies(input_stream)
full_range = 2**precision
half_range = full_range // 2
quarter_range = full_range // 4
L= 0
H= full_range
steps = 0
encoded_bits = []
for symbol in input_stream:
symbol_freq = symbol_freq_dict[symbol]
S_high= cumulative_frequency(symbol, symbol_freq_dict)
S_low= S_high- symbol_freq
current_range = H- L
H= L+ current_range * S_high// stream_size
L= L+ current_range * S_low// stream_size
while True:
if H< half_range:
encoded_bits.extend([0])
encoded_bits.extend([1] * steps)
steps = 0
L*= 2
H*= 2
elif L>= half_range:
encoded_bits.extend([1])
encoded_bits.extend([0] * steps)
steps = 0
L= 2 * (L- half_range)
H= 2 * (H- half_range)
elif L>= quarter_range and H < 3 * quarter_range:
steps += 1
L= 2 * (L- quarter_range)
H= 2 * (H- quarter_range)
else:
break
steps += 1
if L<= quarter_range:
encoded_bits.extend([0])
encoded_bits.extend([1] * steps)
else:
encoded_bits.extend([1])
encoded_bits.extend([0] * steps)
return encoded_bits, symbol_freq_dict
def arithmetic_decode(encoded_bits, symbol_freq_dict, precision=32):
"""
Decode the encoded bits using the arithmetic coding algorithm.
Args:
encoded_bits (list): Encoded binary stream.
symbol_freq_dict (dict): Symbol frequencies dictionary.
precision (int): Number of precision bits used in the decoder.
Returns:
list: Decoded symbol stream.
"""
bits_count = len(encoded_bits)
stream_size = sum(symbol_freq_dict.values())
full_range = 2**precision
half_range = full_range // 2
quarter_range = half_range // 2
L= 0
H= full_range
value = 0
index = 1
decoded_stream = []
while index <= precision and index <= bits_count:
if encoded_bits[index - 1] == 1:
value = value + 2**(precision - index)
index += 1
end_flag = 1
while end_flag:
for symbol, symbol_freq in symbol_freq_dict.items():
S_high = cumulative_frequency(symbol, symbol_freq_dict)
S_low = S_high- symbol_freq
current_range = H - L
upper_limit_calc = L+ current_range * S_high // stream_size
lower_limit_calc = L+ current_range * S_low // stream_size
if lower_limit_calc <= value < upper_limit_calc:
decoded_stream.extend([symbol])
L= lower_limit_calc
H= upper_limit_calc
if symbol == 'O':
end_flag = 0
break
while True:
if H< half_range:
L*= 2
H*= 2
value *= 2
if index <= bits_count:
value += encoded_bits[index - 1]
index += 1
elif L>= half_range:
L= 2 * (L - half_range)
H= 2 * (H - half_range)
value = 2 * (value - half_range)
if index <= bits_count:
value += encoded_bits[index - 1]
index += 1
elif L>= quarter_range and H < 3 * quarter_range:
L= 2 * (L - quarter_range)
H= 2 * (H - quarter_range)
value = 2 * (value - quarter_range)
if index <= bits_count:
value += encoded_bits[index - 1]
index += 1
else:
break
decoded_stream.pop()
return decoded_stream
def flatten_array(array):
"""
Flatten a 2D array to a 1D list.
Args:
array (np.ndarray): Input 2D array.
Returns:
list: Flattened 1D list.
"""
return array.flatten().tolist()
def reshape_to_array(flat_stream, original_shape):
"""
Reshape a flat stream to the original array shape.
Args:
flat_stream (list): Flattened input stream.
original_shape (tuple): Shape of the original array.
Returns:
np.ndarray: Reshaped array.
"""
return np.array(flat_stream).reshape(original_shape)
original_array = np.random.randint(0, 256, size=(2, 8, 8), dtype=np.uint8)
print("Original Array:")
print(original_array, "\n")
flat_stream = flatten_array(original_array)
print("Flattened Stream:")
print(flat_stream, "\n")
encoded_stream, dictionary = arithmetic_encode(flat_stream)
print("Encoded Stream:")
print(encoded_stream, "\n")
print("Symbol Frequencies:")
print(dictionary, "\n")
decoded_stream = arithmetic_decode(encoded_stream, dictionary)
print("Decoded Stream:")
print(decoded_stream, "\n")
decoded_array = reshape_to_array(decoded_stream, original_array.shape)
print("Decoded Array:")
print(decoded_array, "\n")
print("Original and Decoded Arrays are the same:", np.array_equal(original_array, decoded_array))