cours-m1-eea/455-Codage_Sources/algo_code/code_arithmetique.py

30 lines
586 B
Python
Raw Normal View History

2019-02-04 14:43:31 +01:00
#!/usr/bin/env python3
import numpy as np
p_s = [0.1,0.9]
P = np.random.rand(N)
X = [0 for p in P if p > p_s[0] else 1]
def binary(n,b=2,m):
"""
Convertie un nombre décimal en sa version binaire tronqué à m bits.
"""
return np.ceil(n*b**m)
def arithm(X):
l=[0]
h= [1]
for x in X:
if x == 0:
h.append(l[-1]+p_s[0]*(h[-1]-l[-1]))
l.append(l[-1])
else:
l.append(l[-1]+p_s[0]*(h[-1]-l[-1]))
h.append(h[-1])
lmb = (l[-1]+h[-1])/2
mu = int(-np.log2(h[-1]-l[-1]))+1