From 45afaf066f09169bfe4af0ce14ac18dfe4982ce7 Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Tue, 30 Apr 2019 18:28:33 +0200 Subject: [PATCH] mise en forme des algos de devoirs --- 455-Codage_Sources/Cours/chapA.tex | 7 ++-- 455-Codage_Sources/Cours/main.tex | 7 ++-- .../algo_code/code_arithmetique.py | 36 +++---------------- 455-Codage_Sources/algo_code/huffman.py | 4 --- 455-Codage_Sources/algo_code/huffman2.py | 8 +++-- 5 files changed, 18 insertions(+), 44 deletions(-) diff --git a/455-Codage_Sources/Cours/chapA.tex b/455-Codage_Sources/Cours/chapA.tex index fcc8328..7b11b0f 100644 --- a/455-Codage_Sources/Cours/chapA.tex +++ b/455-Codage_Sources/Cours/chapA.tex @@ -2,13 +2,13 @@ \begin{document} \section{Codage d'Huffman} \subsection{version simple} - \inputminted{python}{../algo_code/huffman.py} \subsection{version objet} - \inputminted{python}{../algo_code/huffman2.py} +\newpage \section{Codage arithmétique} \inputminted{python}{../algo_code/code_arithmetique.py} +\newpage \section{Codage LZW} \inputminted{python}{../algo_code/LZW.py} \section{Quantification} @@ -25,7 +25,8 @@ Evaluer le KLT une restriction à $m$ composante pour un signal sonore. Tracer l \end{document} + %%% Local Variables: %%% mode: latex -%%% TeX-master: main.tex +%%% TeX-master: "main" %%% End: diff --git a/455-Codage_Sources/Cours/main.tex b/455-Codage_Sources/Cours/main.tex index 4c258b0..b5f04ef 100644 --- a/455-Codage_Sources/Cours/main.tex +++ b/455-Codage_Sources/Cours/main.tex @@ -5,7 +5,9 @@ \let\shaded\relax \let\endshaded\relax \let\leftbar\relax \let\endleftbar\relax \let\snugshade\relax \let\endsnugshade\relax -\usepackage{minted} + \usepackage{minted} +\setminted{frame=lines, framesep=2mm, baselinestretch=1, fontsize=\footnotesize,linenos} + % Mise en page \renewcommand{\vec}{\mathbf} @@ -16,7 +18,8 @@ \begin{document} \maketitle \tableofcontents -\chapter*{Rappel de probabilité} +\setcounter{chapter}{-1} +\chapter{Rappel de probabilité} \subfile{chap0_MAN.tex} \chapter{Introduction - Motivation} \subfile{chap0.tex} diff --git a/455-Codage_Sources/algo_code/code_arithmetique.py b/455-Codage_Sources/algo_code/code_arithmetique.py index 90f325c..fcff079 100755 --- a/455-Codage_Sources/algo_code/code_arithmetique.py +++ b/455-Codage_Sources/algo_code/code_arithmetique.py @@ -2,28 +2,15 @@ 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] -print(X) 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 - return binaire,np.binary_repr(int(binaire)) def arithm(X,p): - l=[0] - h= [1] + l=[0]; h= [1] for x in X: if x == 0: h.append(l[-1]+p*(h[-1]-l[-1])) @@ -31,18 +18,13 @@ def arithm(X,p): else: l.append(l[-1]+p*(h[-1]-l[-1])) h.append(h[-1]) - lmb = (l[-1]+h[-1])/2 - mu = int(-np.log2(h[-1]-l[-1]))+1 code = binary(lmb,mu) return code,lmb,mu def arithm_pratique(X,p): - l = [0] # borne inférieur - h =[1] # borne supérieur - f = 0 # follow - c =[] # code + l = [0]; h =[1] ;f = 0;c =[] #inf, sup,follow, code for k in range(len(X)): print("for loop") if X[k] == 0: @@ -51,28 +33,18 @@ def arithm_pratique(X,p): else: l.append(l[-1]+p*(h[-1]-l[-1])) h.append(h[-1]) - print(X[k]) - 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") + 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)): if (l[-1]>=0 and h[-1]<0.5): - print(" case 1") c += [0]+[1]*f l[-1] *=2 h[-1] *=2 elif (l[-1]>=0.5 and h[-1]<1): - print(" case 2") c += [1]+[0]*f l[-1] = 2*l[-1]-1 h[-1] = 2*h[-1]-1 elif (l[-1]>= 0.25 and h[-1]<0.75): - print(" case 3") f +=1 l[-1] = 2*l[-1]-0.5 h[-1] = 2*h[-1]-0.5 return c -#print(arithm(X,p)) print(arithm_pratique(X,p)) diff --git a/455-Codage_Sources/algo_code/huffman.py b/455-Codage_Sources/algo_code/huffman.py index f9e4713..6ae1585 100755 --- a/455-Codage_Sources/algo_code/huffman.py +++ b/455-Codage_Sources/algo_code/huffman.py @@ -1,7 +1,5 @@ #!/usr/bin/python3 -import numpy as np - def get_2min(l): min1 = 0 min2 = 1 @@ -29,9 +27,7 @@ def huffman_rec(p): C[min1] +='0' p.insert(min2,p_save) p[min1] -= p_save - print(p,C) return C p = [25,20,15,12,10,8,5,5] - print(huffman_rec(p)) diff --git a/455-Codage_Sources/algo_code/huffman2.py b/455-Codage_Sources/algo_code/huffman2.py index 7c66476..2c2f439 100755 --- a/455-Codage_Sources/algo_code/huffman2.py +++ b/455-Codage_Sources/algo_code/huffman2.py @@ -17,11 +17,11 @@ class Noeud(object): return self.p 2: queue.sort() - print(queue) l=queue.pop(0) r=queue.pop(0) queue.append(Noeud(left=l,right=r)) @@ -41,10 +41,12 @@ def gen_code(node,prefix=''): x = gen_code_rec(node,prefix) return x - +#affichage à l'aide de graphviz def draw_tree(node): 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: desc = 'N{} [label="{}"];\n'.format(node.code,node.code) desc += draw_tree(node.left)