From 8acd77664bc5930f16ea03a016c08c7adbbe3eed Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Mon, 10 Feb 2025 21:40:48 +0100
Subject: [PATCH 1/3] Update figure labels in 01-capteurs.md and 02-signaux.md
 for consistency

---
 cours/SIN/01-capteurs.md |  6 +++---
 cours/SIN/02-signaux.md  | 12 ++++++------
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/cours/SIN/01-capteurs.md b/cours/SIN/01-capteurs.md
index 0c3deb9..fbffa9b 100644
--- a/cours/SIN/01-capteurs.md
+++ b/cours/SIN/01-capteurs.md
@@ -29,7 +29,7 @@ Un interrupteur (@inter) ou un détecteur de mouvement PIR (@pir) sont des capte
 tout ou rien.
 
 ::::{figure}
-:label: tor
+:label: fig:capteur-tor
 :::{figure} https://upload.wikimedia.org/wikipedia/commons/3/3a/Switches-electrical.agr.jpg 
 :label: inter
 
@@ -60,7 +60,7 @@ Une thermistance (@thermistance) ou une jauge de déformation (@jauge) sont des
 capteurs analogiques.
 
 ::::{figure}
-:label: analogique
+:label: fig:capteur-analogique
 :::{figure} https://upload.wikimedia.org/wikipedia/commons/3/3b/NTC_bead.jpg
 :label: thermistance
 
@@ -88,7 +88,7 @@ Un capteur numérique génère un signal de sortie ne pouvant prendre qu'un cert
 Une caméra (@camera) ou un codeur absolu (@codeur) sont des capteurs numériques.
 
 ::::{figure}
-:label: numeriques
+:label: fig:capteur-numerique
 :::{figure} https://upload.wikimedia.org/wikipedia/commons/0/02/S4000_Image_Sensor_%28Colorful%29.jpg
 :label: camera
 
diff --git a/cours/SIN/02-signaux.md b/cours/SIN/02-signaux.md
index 919c48e..651ccee 100644
--- a/cours/SIN/02-signaux.md
+++ b/cours/SIN/02-signaux.md
@@ -29,7 +29,7 @@ Un signal logique ne peut prendre que deux valeurs : un niveau **haut** ("High")
 et un niveau **bas** ("Low").
 
 ````{figure}
-:label: logique
+:label: fig:sig-logique
 ```{code-cell} python
 :tags: [remove-input]
 import altair as alt
@@ -62,7 +62,7 @@ alt.Chart(
 Exemple de signal logique
 ````
 
-Le signal logique en @logique est par exemple à l'état haut entre 2 s et 3 s,
+Le signal logique en @fig:sig-logique est par exemple à l'état haut entre 2 s et 3 s,
 et à l'état bas entre 3 s et 6 s.
 Lorsque le signal passe de l'état bas à l'état haut (comme à 2 s),
 on parle de **front montant**.
@@ -71,10 +71,10 @@ Dans le cas contraire (comme à 3 s), on parle de **front descendant**.
 ## Les signaux analogiques
 
 Un signal analogique est un signal qui peut prendre un ensemble continu de valeurs.
-Un exemple de signal analogique est donné en @analogique.
+Un exemple de signal analogique est donné en @fig:sig-analogique.
 
 ````{figure}
-:label: analogique
+:label: fig:sig-analogique
 ```{code-cell} python
 :tags: [remove-input]
 import altair as alt
@@ -113,10 +113,10 @@ Exemple de signal analogique
 ## Les signaux numériques
 Un signal numérique est un signal qui peut prendre un ensemble discret de valeur,
 c'est-à-dire un ensemble précis de valeurs distinctes (généralement des nombres entiers).
-Un exemple de signal analogique est donné en @numerique.
+Un exemple de signal analogique est donné en @fig:sig-numerique.
 
 ````{figure}
-:label: numerique
+:label: fig:sig-numerique
 ```{code-cell} python
 :tags: [remove-input]
 import altair as alt

From 5d9d2e0ea0d7f6186528316b0d279c812045785e Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Mon, 10 Feb 2025 21:41:06 +0100
Subject: [PATCH 2/3] =?UTF-8?q?WIP:=20Add=20new=20course=20material=20on?=
 =?UTF-8?q?=20CAN=20(Convertisseur=20Analogique=20Num=C3=A9rique)=20with?=
 =?UTF-8?q?=20definitions,=20sampling,=20and=20conversion=20examples?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 cours/SIN/03-can.md | 141 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 141 insertions(+)
 create mode 100644 cours/SIN/03-can.md

diff --git a/cours/SIN/03-can.md b/cours/SIN/03-can.md
new file mode 100644
index 0000000..9b40196
--- /dev/null
+++ b/cours/SIN/03-can.md
@@ -0,0 +1,141 @@
+---
+title: Le CAN
+subject: Cours
+kernelspec:
+  name: python3
+  display_name: Python 3
+abbreviations:
+  CAN: Convertisseur Analogique Numérique
+---
+
+# Définition
+
+:::{prf:definition} CAN
+:nonumber: true
+Un convertisseur analogique-numérique est un dispositif électronique dont la fonction est de traduire une grandeur analogique en une valeur numérique codée sur plusieurs bits. Le signal converti est généralement une tension électrique.
+
+Source : Article _[Convertisseur analogique-numérique](http://fr.wikipedia.org/wiki/Convertisseur_analogique-num%C3%A9rique)_ de [Wikipédia en français](https://fr.wikipedia.org/) ([auteurs](http://fr.wikipedia.org/w/index.php?title=Convertisseur_analogique-num%C3%A9rique&action=history))
+:::
+
+# L'échantillonage du signal
+L'échantillonage du signal est la prise d'une valeur à un intervalle régulier de temps. L'intervalle entre deux valeurs s'appelle **période d'échantillonage**. On la note $T_e$ (en secondes). On parle aussi de **fréquence d'échantillonage** $f_e=\frac{1}{T_e}$ (en Hertz), qui correspond au nombre de valeurs prises chaque seconde.
+
+Le **quantum** correspond au plus petit écart quantifiable (la "hauteur d'une marche"). On le note $q$ et son unité est celle du signal d'entrée (généralement le Volt).
+
+La **tension de pleine échelle** ou **tension de référence** est la tension maximale quantifiable par le CAN. On la note $V_\text{pe}$ ou $V_\text{ref}$.
+
+Le nombre de valeurs que peut générer le convertisseur se note $N$ et dépend du nombre de bits $n$ du convertisseur. Ainsi : $N=2^n$.
+
+On obtient la relation suivante : $q=\frac{V_\text{pe}}{N}=\frac{V_\text{pe}}{2^n}$.
+
+# Exemple de conversion
+On donne en @fig:exemple-can l'exemple d'un CAN de tension de référence 5 V fonctionnant sur 3 bits avec une fréquence d'échantillonage de 2 Hz.
+
+La **caractéristique** du CAN est la courbe représentant la valeur numérique en sortie en fonction de la valeur analogique en entrée (@fig:carac-can).
+
+````{figure}
+:label: fig:exemple-can
+```{code-cell} python
+:tags: [remove-input]
+import altair as alt
+import numpy as np
+import pandas as pd
+from scipy import interpolate
+
+rng = np.random.default_rng(25)
+
+n = 20
+t_max = 16
+
+T = np.linspace(0, t_max, 1601)
+y = np.clip(
+    interpolate.BSpline(np.linspace(0, t_max, n), 5 * rng.random(n), 2)(T),
+    0,
+    5,
+)
+y_n = np.full([1601], np.nan)
+y_n[::50] = np.floor(y[::50] * 8 / 5)
+y_n[y_n == 8] = 7
+
+
+data = pd.DataFrame({
+  "t": T,
+  "s": y,
+  "s_n": y_n,
+})
+
+base = alt.Chart(
+  data
+).encode(
+  alt.X("t:Q").axis(title="Temps (s)").scale(domain=(0,t_max)),
+)
+
+ch = base.mark_line(
+  interpolate="basis",
+  strokeWidth=3,
+  color="#6666cc",
+).encode(
+  alt.Y("s:Q", axis=alt.Axis(title="Signal analogique", titleColor="#6666cc")).scale(domain=(0,5)),
+)
+
+ch_n = base.mark_point(
+  filled=True,
+  color="#ff6600",
+).encode(
+  alt.Y(
+    "s_n:Q",
+    axis=alt.Axis(
+      title="Signal numérisé",
+      titleColor="#ff6600",
+      values=np.arange(8),
+    )
+  ).scale(domain=(0,8)),
+)
+
+alt.layer(ch_n, ch).resolve_scale(
+  y="independent",
+).properties(
+  width="container",
+  height=200,
+)
+
+
+```
+Signal analogique et signal numérisé.
+````
+
+````{figure}
+:label: fig:carac-can
+```{code-cell} python
+:tags: [remove-input]
+import altair as alt
+import numpy as np
+import pandas as pd
+from scipy import interpolate
+
+N = 8
+s_n = np.arange(N+1)
+s_n[-1] = s_n[-2]
+data = pd.DataFrame({
+  "s_n": s_n,
+  "s_a": np.linspace(0, 5, N+1),
+})
+
+alt.Chart(
+  data
+).mark_line(
+  interpolate="step-after",
+  strokeWidth=3,
+  color="#ff6600",
+).encode(
+  alt.X("s_a:Q").axis(title="Signal Analogique").scale(domain=(0,5)),
+  alt.Y("s_n:Q", axis=alt.Axis(title="Signal numérique", values=np.arange(N))).scale(domain=(0,N)),
+).properties(
+  width=200,
+  height=200,
+)
+
+
+```
+Caractéristique du CAN.
+````
\ No newline at end of file

From bc9d8904e2e505c4e63be5da5d78f0cbb01d2a26 Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Thu, 27 Feb 2025 18:33:19 +0100
Subject: [PATCH 3/3] Refactor signal visualizations to use Matplotlib,
 removing Altair dependencies and updating figure configurations for clarity

---
 cours/SIN/02-signaux.md |  17 +----
 cours/SIN/03-can.md     | 144 ++++++++++++++++++++--------------------
 2 files changed, 73 insertions(+), 88 deletions(-)

diff --git a/cours/SIN/02-signaux.md b/cours/SIN/02-signaux.md
index b9cbeb5..49288b0 100644
--- a/cours/SIN/02-signaux.md
+++ b/cours/SIN/02-signaux.md
@@ -43,7 +43,7 @@ t = np.arange(n+1)
 s = rng.choice([0, 1], n)
 
 fig, ax = plt.subplots()
-ax.stairs(s, t, lw=3)
+ax.stairs(s, t, lw=3, baseline=None)
 ax.set(
   xlim=(0, n),
   ylim=(-.5, 1.5),
@@ -88,7 +88,6 @@ lhs = (qmc.LatinHypercube(d=n-2, rng=rng).random(1)[0] - .5) * t_max/n
 t = t_base + np.concatenate(([0], lhs, [0]))
 t = t_base
 s = 5 * rng.random(n)
-s[-1] = s[-2]
 
 t_interp = np.linspace(0, t_max, 1024)
 s_interp = np.clip(CubicSpline(t, s)(t_interp), 0, 5)
@@ -127,7 +126,7 @@ t = np.arange(n+1)
 s = rng.integers(0, 16, n)
 
 fig, ax = plt.subplots()
-ax.stairs(s, t, lw=3)
+ax.stairs(s, t, lw=3, baseline=None)
 ax.set(
   xlim=(0, n),
   ylim=(-.5, 16.5),
@@ -136,18 +135,6 @@ ax.set(
 )
 ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
 ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
-# alt.Chart(
-#   data
-# ).mark_line(
-#   interpolate="step-after",
-#   strokeWidth=3,
-# ).encode(
-#   alt.X("t:Q").axis(title="Temps (s)").scale(domain=(0,n)),
-#   alt.Y("s:Q", axis=alt.Axis(title="Signal numérique", values=np.arange(0, 16))).# scale(domain=(0,15)),
-# ).properties(
-#   width="container",
-#   height=200,
-# )
 ```
 Exemple de signal numérique
 ````
\ No newline at end of file
diff --git a/cours/SIN/03-can.md b/cours/SIN/03-can.md
index 9b40196..ff412e3 100644
--- a/cours/SIN/03-can.md
+++ b/cours/SIN/03-can.md
@@ -37,69 +37,64 @@ La **caractéristique** du CAN est la courbe représentant la valeur numérique
 :label: fig:exemple-can
 ```{code-cell} python
 :tags: [remove-input]
-import altair as alt
+import matplotlib.pyplot as plt
+from matplotlib import ticker
 import numpy as np
-import pandas as pd
-from scipy import interpolate
+from scipy.interpolate import CubicSpline
+from scipy.stats import qmc
 
-rng = np.random.default_rng(25)
+rng = np.random.default_rng(50)
 
 n = 20
-t_max = 16
+t_max = 8
+n_interp = t_max * 100 + 1
 
-T = np.linspace(0, t_max, 1601)
-y = np.clip(
-    interpolate.BSpline(np.linspace(0, t_max, n), 5 * rng.random(n), 2)(T),
-    0,
-    5,
+t_base = np.linspace(0, t_max, n)
+lhs = (qmc.LatinHypercube(d=n-2, rng=rng).random(1)[0] - .5) * t_max/n
+t = t_base + np.concatenate(([0], lhs, [0]))
+t = t_base
+s = 5 * rng.random(n)
+
+t_interp = np.linspace(0, t_max, n_interp)
+s_interp = np.clip(CubicSpline(t, s)(t_interp), 0, 5)
+s_n = np.full_like(t_interp[::50], np.nan)
+s_n = np.floor(s_interp[::50] * 8 / 5)
+s_n[s_n == 8] = 7
+
+fig, ax = plt.subplots()
+ax2 = ax.twinx()
+
+ax.plot(t_interp, s_interp, lw=3)
+ax2.scatter(t_interp[::50], s_n, color="C1")
+
+ax.grid(False, axis="y")
+ax.grid(True, axis="x", which="both")
+ax2.grid(True)
+
+ax.set(
+  xlim=(0, t_max),
+  ylim=(-.5, 5.5),
+  xlabel="Temps (s)",
 )
-y_n = np.full([1601], np.nan)
-y_n[::50] = np.floor(y[::50] * 8 / 5)
-y_n[y_n == 8] = 7
-
-
-data = pd.DataFrame({
-  "t": T,
-  "s": y,
-  "s_n": y_n,
-})
-
-base = alt.Chart(
-  data
-).encode(
-  alt.X("t:Q").axis(title="Temps (s)").scale(domain=(0,t_max)),
+ax.set_ylabel("Signal analogique (V)", color="C0")
+ax2.set(
+  ylim=(-8/5*.5, 8/5*5.5),
 )
+ax2.set_ylabel("Signal numérique", color="C1")
 
-ch = base.mark_line(
-  interpolate="basis",
-  strokeWidth=3,
-  color="#6666cc",
-).encode(
-  alt.Y("s:Q", axis=alt.Axis(title="Signal analogique", titleColor="#6666cc")).scale(domain=(0,5)),
-)
+ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
+ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
+ax.xaxis.set_minor_locator(ticker.MultipleLocator(.5))
+ax2.set_yticks(np.arange(9), np.concatenate((np.arange(8), [""])))
 
-ch_n = base.mark_point(
-  filled=True,
-  color="#ff6600",
-).encode(
-  alt.Y(
-    "s_n:Q",
-    axis=alt.Axis(
-      title="Signal numérisé",
-      titleColor="#ff6600",
-      values=np.arange(8),
-    )
-  ).scale(domain=(0,8)),
-)
-
-alt.layer(ch_n, ch).resolve_scale(
-  y="independent",
-).properties(
-  width="container",
-  height=200,
-)
+arr = ax2.annotate("", xy=(0, 0), xytext=(0.5, 0), arrowprops=dict(arrowstyle="<->"))
+ax2.annotate("$T_e$", (0.5, 1), xycoords=arr, ha="center", va="bottom")
 
+arr2 = ax2.annotate("", xy=(0.5, 0), xytext=(0.5, 1), arrowprops=dict(arrowstyle="<->"))
+ax2.annotate("$q$", (1, 0.5), xycoords=arr2, ha="left", va="center")
 
+arr3 = ax2.annotate("", xy=(1, 0), xytext=(1, 8), arrowprops=dict(arrowstyle="<->"))
+ax2.annotate("$V_{pe}$", (1, 0.5), xycoords=arr3, ha="left", va="center")
 ```
 Signal analogique et signal numérisé.
 ````
@@ -108,34 +103,37 @@ Signal analogique et signal numérisé.
 :label: fig:carac-can
 ```{code-cell} python
 :tags: [remove-input]
-import altair as alt
+import matplotlib.pyplot as plt
+from matplotlib import ticker
 import numpy as np
-import pandas as pd
-from scipy import interpolate
 
 N = 8
-s_n = np.arange(N+1)
-s_n[-1] = s_n[-2]
-data = pd.DataFrame({
-  "s_n": s_n,
-  "s_a": np.linspace(0, 5, N+1),
-})
+s_n = np.arange(N)
+s_a = np.linspace(0, 5, N+1)
 
-alt.Chart(
-  data
-).mark_line(
-  interpolate="step-after",
-  strokeWidth=3,
-  color="#ff6600",
-).encode(
-  alt.X("s_a:Q").axis(title="Signal Analogique").scale(domain=(0,5)),
-  alt.Y("s_n:Q", axis=alt.Axis(title="Signal numérique", values=np.arange(N))).scale(domain=(0,N)),
-).properties(
-  width=200,
-  height=200,
+fig, ax = plt.subplots()
+
+ax.stairs(s_n, s_a, color="C1", lw=3, baseline=None)
+
+ax.set(
+  xlim=(0, 5),
+  ylim=(-1, N),
+  yticks=s_n,
+  xlabel="Signal analogique (V)",
+  ylabel="Signal numérique",
 )
+ax.set_xticks(s_a, [f"{v:.3f}" for v in s_a], rotation=45, ha="right", rotation_mode="anchor")
+ax.set_aspect(5/8, 'box')
 
+arr4 = ax.annotate(
+    "", xy=(s_a[0], 0), xytext=(s_a[1], 0), arrowprops=dict(arrowstyle="<->")
+)
+ax.annotate("$q$", (0.5, 1), xycoords=arr4, ha="center", va="bottom")
 
+arr5 = ax.annotate(
+    "", xy=(s_a[0], 1.5), xytext=(s_a[-1], 1.5), arrowprops=dict(arrowstyle="<->")
+)
+ax.annotate("$V_{pe}$", (0.5, 1), xycoords=arr5, ha="center", va="bottom")
 ```
 Caractéristique du CAN.
 ````
\ No newline at end of file