changeset 249:a9dc1a5115a7

Add plotting.py
author Amine Sehili <amine.sehili@gmail.com>
date Mon, 26 Aug 2019 20:55:16 +0200
parents c422eeff8013
children a9f4adfae459
files auditok/plotting.py
diffstat 1 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/auditok/plotting.py	Mon Aug 26 20:55:16 2019 +0200
@@ -0,0 +1,29 @@
+import matplotlib.pyplot as plt
+import numpy as np
+
+
+def plot(data, sampling_rate, show=True):
+    y = np.asarray(data)
+    ymax = np.abs(y).max()
+    nb_samples = y.shape[-1]
+    sample_duration = 1 / sampling_rate
+    x = np.linspace(0, sample_duration * (nb_samples - 1), nb_samples)
+    plt.plot(x, y / ymax, c="#024959")
+    plt.ylim(-3, 3)
+    if show:
+        plt.show()
+
+
+def plot_detections(data, sampling_rate, detections, show=True, save_as=None):
+
+    plot(data, sampling_rate, show=False)
+    if detections is not None:
+        for (start, end) in detections:
+            plt.axvspan(start, end, facecolor="g", ec="r", lw=2, alpha=0.4)
+
+    if save_as is not None:
+        plt.savefig(save_as, dpi=120)
+
+    if show:
+        plt.show()
+    return