2020-12-17 22:08:07 +00:00
|
|
|
|
import soundbox
|
|
|
|
|
import numpy as np
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
|
|
Affiche la transformée de Fourier du fichier [source] sur un graphe
|
2020-12-18 12:24:42 +00:00
|
|
|
|
dans le fichier [output]. Passer - comme [output] fait s’afficher le
|
|
|
|
|
graphe 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 FFT
|
|
|
|
|
signal = soundbox.load_signal(source_file)
|
2020-12-19 20:39:41 +00:00
|
|
|
|
freq_scale = soundbox.samp_rate / len(signal)
|
|
|
|
|
|
2020-12-18 19:37:44 +00:00
|
|
|
|
vecs = np.fft.fft(signal)[:soundbox.samp_rate // 2]
|
|
|
|
|
values = np.absolute(vecs) / np.max(np.absolute(vecs))
|
2020-12-19 20:39:41 +00:00
|
|
|
|
freq = np.arange(len(values))
|
|
|
|
|
|
|
|
|
|
# Calcul de la fenêtre des fréquences intéressantes
|
2020-12-19 20:42:56 +00:00
|
|
|
|
high_enough = np.where(values >= 0.01)
|
2020-12-19 20:39:41 +00:00
|
|
|
|
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 18:59:55 +00:00
|
|
|
|
'figure.figsize': (10, 5),
|
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-17 22:08:07 +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 20:39:41 +00:00
|
|
|
|
ax.plot(freq, values)
|
2020-12-19 22:50:49 +00:00
|
|
|
|
ax.grid(alpha=.3)
|
2020-12-18 18:59:55 +00:00
|
|
|
|
|
|
|
|
|
# Configuration des axes
|
|
|
|
|
ax.set_xlabel('Fréquence', labelpad=10)
|
|
|
|
|
ax.set_xscale('log', base=2)
|
2020-12-19 22:50:49 +00:00
|
|
|
|
ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, pos: f'{x * freq_scale:.0f} Hz'))
|
|
|
|
|
ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(100 / freq_scale))
|
|
|
|
|
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
|
|
|
|
|
ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(10 / freq_scale))
|
2020-12-17 23:36:36 +00:00
|
|
|
|
|
2020-12-18 18:59:55 +00:00
|
|
|
|
ax.set_ylabel('Amplitude relative', labelpad=10)
|
2020-12-19 22:50:49 +00:00
|
|
|
|
ax.yaxis.set_major_formatter(matplotlib.ticker.StrMethodFormatter('{x:.1f}'))
|
|
|
|
|
ax.yaxis.set_major_locator(matplotlib.ticker.MultipleLocator(.2))
|
|
|
|
|
ax.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
|
|
|
|
|
ax.yaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(.05))
|
2020-12-17 22:08:07 +00:00
|
|
|
|
|
2020-12-18 18:59:55 +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)
|