2020-12-17 22:08:07 +00:00
|
|
|
|
import soundbox
|
|
|
|
|
import numpy as np
|
|
|
|
|
import scipy.signal as sig
|
|
|
|
|
import matplotlib
|
|
|
|
|
import matplotlib.pyplot as plt
|
2020-12-17 23:36:36 +00:00
|
|
|
|
import sys
|
2020-12-17 22:08:07 +00:00
|
|
|
|
|
2020-12-17 23:36:36 +00:00
|
|
|
|
if len(sys.argv) != 3:
|
|
|
|
|
print(f"""Utilisation: {sys.argv[0]} [source] [output]
|
2020-12-17 22:08:07 +00:00
|
|
|
|
|
2020-12-18 12:24:42 +00:00
|
|
|
|
Génère le sonagramme du fichier [source] dans le fichier [output].
|
|
|
|
|
Passer - comme [output] fait s’afficher le sonagramme dans une fenêtre.""")
|
2020-12-17 23:36:36 +00:00
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
source_file = sys.argv[1]
|
|
|
|
|
output_file = sys.argv[2]
|
|
|
|
|
|
|
|
|
|
# Calcul du STFT
|
|
|
|
|
signal = soundbox.load_signal(source_file)
|
2020-12-19 22:50:49 +00:00
|
|
|
|
|
2020-12-18 19:37:44 +00:00
|
|
|
|
freq, time, vecs = sig.stft(signal, soundbox.samp_rate, nperseg=soundbox.samp_rate * 0.5)
|
|
|
|
|
values = np.absolute(vecs)
|
2020-12-17 22:08:07 +00:00
|
|
|
|
|
2020-12-19 22:50:49 +00:00
|
|
|
|
# Calcul de la fenêtre des fréquences intéressantes
|
|
|
|
|
high_enough = np.where(values.max(axis=1) / np.max(values) >= 0.01)
|
|
|
|
|
left = high_enough[0][0]
|
|
|
|
|
right = high_enough[0][-1]
|
|
|
|
|
|
|
|
|
|
freq = freq[left:right]
|
|
|
|
|
values = values[left:right]
|
|
|
|
|
|
2020-12-17 23:36:36 +00:00
|
|
|
|
# Génération du graphe
|
|
|
|
|
plt.rcParams.update({
|
2020-12-18 19:37:44 +00:00
|
|
|
|
'figure.figsize': (10, 8),
|
2020-12-19 22:50:49 +00:00
|
|
|
|
'font.size': 18,
|
2020-12-17 23:36:36 +00:00
|
|
|
|
'font.family': 'Concourse T4',
|
|
|
|
|
})
|
|
|
|
|
|
2020-12-19 22:50:49 +00:00
|
|
|
|
fig, ax = plt.subplots()
|
2020-12-17 23:36:36 +00:00
|
|
|
|
ax.tick_params(axis='both', which='major', labelsize=12)
|
2020-12-19 22:50:49 +00:00
|
|
|
|
ax.pcolormesh(time, freq, values, cmap='Greys', shading='gouraud')
|
|
|
|
|
ax.grid(alpha=.3)
|
2020-12-17 22:08:07 +00:00
|
|
|
|
|
2020-12-18 19:37:44 +00:00
|
|
|
|
# Configuration des axes
|
2020-12-17 23:36:36 +00:00
|
|
|
|
def freq_format(value, pos):
|
|
|
|
|
return f'{value:.0f} Hz'
|
|
|
|
|
|
|
|
|
|
|
2020-12-18 19:37:44 +00:00
|
|
|
|
ax.set_xlabel('Temps', labelpad=10)
|
2020-12-19 22:50:49 +00:00
|
|
|
|
ax.xaxis.set_major_formatter(matplotlib.ticker.StrMethodFormatter('{x:.0f} s'))
|
|
|
|
|
ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(1))
|
|
|
|
|
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
|
|
|
|
|
ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(.2))
|
2020-12-17 22:08:07 +00:00
|
|
|
|
|
2020-12-18 19:37:44 +00:00
|
|
|
|
ax.set_ylabel('Fréquence', labelpad=10)
|
|
|
|
|
ax.set_yscale('log', base=2)
|
|
|
|
|
ax.yaxis.set_major_locator(plt.MultipleLocator(100))
|
2020-12-19 22:50:49 +00:00
|
|
|
|
ax.yaxis.set_major_formatter(matplotlib.ticker.StrMethodFormatter('{x:.0f} Hz'))
|
|
|
|
|
ax.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
|
|
|
|
|
ax.yaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(10))
|
2020-12-17 22:08:07 +00:00
|
|
|
|
|
2020-12-18 19:37:44 +00:00
|
|
|
|
# Rendu du résultat
|
2020-12-18 12:24:42 +00:00
|
|
|
|
if output_file == '-':
|
|
|
|
|
plt.show()
|
|
|
|
|
else:
|
|
|
|
|
plt.tight_layout()
|
|
|
|
|
plt.savefig(output_file)
|