mise en forme des algos de devoirs

This commit is contained in:
Pierre-antoine Comby 2019-04-30 18:28:33 +02:00
parent bb08d92f23
commit 45afaf066f
5 changed files with 18 additions and 44 deletions

View file

@ -2,13 +2,13 @@
\begin{document} \begin{document}
\section{Codage d'Huffman} \section{Codage d'Huffman}
\subsection{version simple} \subsection{version simple}
\inputminted{python}{../algo_code/huffman.py} \inputminted{python}{../algo_code/huffman.py}
\subsection{version objet} \subsection{version objet}
\inputminted{python}{../algo_code/huffman2.py} \inputminted{python}{../algo_code/huffman2.py}
\newpage
\section{Codage arithmétique} \section{Codage arithmétique}
\inputminted{python}{../algo_code/code_arithmetique.py} \inputminted{python}{../algo_code/code_arithmetique.py}
\newpage
\section{Codage LZW} \section{Codage LZW}
\inputminted{python}{../algo_code/LZW.py} \inputminted{python}{../algo_code/LZW.py}
\section{Quantification} \section{Quantification}
@ -25,7 +25,8 @@ Evaluer le KLT une restriction à $m$ composante pour un signal sonore. Tracer l
\end{document} \end{document}
%%% Local Variables: %%% Local Variables:
%%% mode: latex %%% mode: latex
%%% TeX-master: main.tex %%% TeX-master: "main"
%%% End: %%% End:

View file

@ -6,6 +6,8 @@
\let\leftbar\relax \let\endleftbar\relax \let\leftbar\relax \let\endleftbar\relax
\let\snugshade\relax \let\endsnugshade\relax \let\snugshade\relax \let\endsnugshade\relax
\usepackage{minted} \usepackage{minted}
\setminted{frame=lines, framesep=2mm, baselinestretch=1, fontsize=\footnotesize,linenos}
% Mise en page % Mise en page
\renewcommand{\vec}{\mathbf} \renewcommand{\vec}{\mathbf}
@ -16,7 +18,8 @@
\begin{document} \begin{document}
\maketitle \maketitle
\tableofcontents \tableofcontents
\chapter*{Rappel de probabilité} \setcounter{chapter}{-1}
\chapter{Rappel de probabilité}
\subfile{chap0_MAN.tex} \subfile{chap0_MAN.tex}
\chapter{Introduction - Motivation} \chapter{Introduction - Motivation}
\subfile{chap0.tex} \subfile{chap0.tex}

View file

@ -2,28 +2,15 @@
import numpy as np import numpy as np
N=10
p = 0.2
P = np.random.rand(N)
X = np.zeros(N,dtype='int')
for i in range(N):
if P[i] > p:
X[i] = 1
X=[0,1,0,0,1,0,0,0,0,0] X=[0,1,0,0,1,0,0,0,0,0]
print(X)
def binary(n,m,b=2): def binary(n,m,b=2):
""" # Convertie un nombre décimal en sa version binaire tronqué à m bits.
Convertie un nombre décimal en sa version binaire tronqué à m bits.
"""
binaire= np.floor(n*b**m) # on se décale dans les entiers et on floor binaire= np.floor(n*b**m) # on se décale dans les entiers et on floor
return binaire,np.binary_repr(int(binaire)) return binaire,np.binary_repr(int(binaire))
def arithm(X,p): def arithm(X,p):
l=[0] l=[0]; h= [1]
h= [1]
for x in X: for x in X:
if x == 0: if x == 0:
h.append(l[-1]+p*(h[-1]-l[-1])) h.append(l[-1]+p*(h[-1]-l[-1]))
@ -31,18 +18,13 @@ def arithm(X,p):
else: else:
l.append(l[-1]+p*(h[-1]-l[-1])) l.append(l[-1]+p*(h[-1]-l[-1]))
h.append(h[-1]) h.append(h[-1])
lmb = (l[-1]+h[-1])/2 lmb = (l[-1]+h[-1])/2
mu = int(-np.log2(h[-1]-l[-1]))+1 mu = int(-np.log2(h[-1]-l[-1]))+1
code = binary(lmb,mu) code = binary(lmb,mu)
return code,lmb,mu return code,lmb,mu
def arithm_pratique(X,p): def arithm_pratique(X,p):
l = [0] # borne inférieur l = [0]; h =[1] ;f = 0;c =[] #inf, sup,follow, code
h =[1] # borne supérieur
f = 0 # follow
c =[] # code
for k in range(len(X)): for k in range(len(X)):
print("for loop") print("for loop")
if X[k] == 0: if X[k] == 0:
@ -51,28 +33,18 @@ def arithm_pratique(X,p):
else: else:
l.append(l[-1]+p*(h[-1]-l[-1])) l.append(l[-1]+p*(h[-1]-l[-1]))
h.append(h[-1]) h.append(h[-1])
print(X[k]) while ((l[-1]>=0 and h[-1]<0.5) or (l[-1]>=0.5 and h[-1]<1) or (l[-1]>= 0.25 and h[-1]<0.75)):
print(l[-3:])
print(h[-3:])
while ((l[-1]>=0 and h[-1]<0.5)
or (l[-1]>=0.5 and h[-1]<1)
or (l[-1]>= 0.25 and h[-1]<0.75)):
print(" loop")
if (l[-1]>=0 and h[-1]<0.5): if (l[-1]>=0 and h[-1]<0.5):
print(" case 1")
c += [0]+[1]*f c += [0]+[1]*f
l[-1] *=2 l[-1] *=2
h[-1] *=2 h[-1] *=2
elif (l[-1]>=0.5 and h[-1]<1): elif (l[-1]>=0.5 and h[-1]<1):
print(" case 2")
c += [1]+[0]*f c += [1]+[0]*f
l[-1] = 2*l[-1]-1 l[-1] = 2*l[-1]-1
h[-1] = 2*h[-1]-1 h[-1] = 2*h[-1]-1
elif (l[-1]>= 0.25 and h[-1]<0.75): elif (l[-1]>= 0.25 and h[-1]<0.75):
print(" case 3")
f +=1 f +=1
l[-1] = 2*l[-1]-0.5 l[-1] = 2*l[-1]-0.5
h[-1] = 2*h[-1]-0.5 h[-1] = 2*h[-1]-0.5
return c return c
#print(arithm(X,p))
print(arithm_pratique(X,p)) print(arithm_pratique(X,p))

View file

@ -1,7 +1,5 @@
#!/usr/bin/python3 #!/usr/bin/python3
import numpy as np
def get_2min(l): def get_2min(l):
min1 = 0 min1 = 0
min2 = 1 min2 = 1
@ -29,9 +27,7 @@ def huffman_rec(p):
C[min1] +='0' C[min1] +='0'
p.insert(min2,p_save) p.insert(min2,p_save)
p[min1] -= p_save p[min1] -= p_save
print(p,C)
return C return C
p = [25,20,15,12,10,8,5,5] p = [25,20,15,12,10,8,5,5]
print(huffman_rec(p)) print(huffman_rec(p))

View file

@ -17,11 +17,11 @@ class Noeud(object):
return self.p<other.p return self.p<other.p
def __repr__(self): def __repr__(self):
return self.name return self.name
def create_tree(table_noeud): def create_tree(table_noeud):
queue = table_noeud.copy() queue = table_noeud.copy()
while len(queue) > 2: while len(queue) > 2:
queue.sort() queue.sort()
print(queue)
l=queue.pop(0) l=queue.pop(0)
r=queue.pop(0) r=queue.pop(0)
queue.append(Noeud(left=l,right=r)) queue.append(Noeud(left=l,right=r))
@ -41,10 +41,12 @@ def gen_code(node,prefix=''):
x = gen_code_rec(node,prefix) x = gen_code_rec(node,prefix)
return x return x
#affichage à l'aide de graphviz
def draw_tree(node): def draw_tree(node):
if len(node.name) == 1: # feuille if len(node.name) == 1: # feuille
desc ='N{} [label="{}:{}", fontcolor=blue, fontsize=16, width=2, shape=box];\n'.format(node.code, node.name, node.code) desc ='N{} [label="{}:{}",\
fontcolor=blue, fontsize=16,\
width=2, shape=box];\n'.format(node.code, node.name, node.code)
else: else:
desc = 'N{} [label="{}"];\n'.format(node.code,node.code) desc = 'N{} [label="{}"];\n'.format(node.code,node.code)
desc += draw_tree(node.left) desc += draw_tree(node.left)