Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Asymmetric numeral system for compression
import numpy as np
x_init = 1
x = x_init
q = 0.6
x_history = [x]
data_stream = [0, 1, 1, 1, 0, 0, 0, 1]
for b in data_stream:
if b == 1:
x = np.floor(x / q)
elif b == 0:
x = np.ceil((x + 1) / (1 - q)) - 1
x_history.append(x)
print(data_stream)
print(x_history)
decode_x_history = [x]
decoded_stream = []
for i in range(10):
s = np.ceil((x+1)*q) - np.ceil(x*q)
print(x * q)
if s == 0:
x = x - np.ceil(x * q)
elif s == 1:
x = np.ceil(x * q)
decode_x_history.append(x)
decoded_stream.append(s)
print(decoded_stream)
print(decode_x_history)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment