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-17 22:08:07 +00:00
|
|
|
|
freqs = np.fft.fft(signal)
|
2020-12-17 23:36:36 +00:00
|
|
|
|
|
|
|
|
|
# Génération du graphe
|
|
|
|
|
ampl_scale = 1 / np.max(np.absolute(freqs))
|
|
|
|
|
freq_scale = soundbox.samp_rate / len(signal)
|
|
|
|
|
|
|
|
|
|
plt.rcParams.update({
|
|
|
|
|
'figure.figsize': (8, 4),
|
|
|
|
|
'font.size': 16,
|
|
|
|
|
'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-17 22:08:07 +00:00
|
|
|
|
ax.plot(np.absolute(freqs))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def freq_format(value, pos):
|
2020-12-17 23:36:36 +00:00
|
|
|
|
return f'{value * freq_scale:.0f} Hz'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ampl_format(value, pos):
|
|
|
|
|
return f'{value * ampl_scale:.1f}'
|
2020-12-17 22:08:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ax.set_xlabel('Fréquence')
|
2020-12-17 23:36:36 +00:00
|
|
|
|
ax.set_xlim(0 / freq_scale, 800 / freq_scale)
|
2020-12-17 22:08:07 +00:00
|
|
|
|
ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(freq_format))
|
2020-12-17 23:36:36 +00:00
|
|
|
|
ax.xaxis.set_major_locator(plt.MultipleLocator(100 / freq_scale))
|
|
|
|
|
|
|
|
|
|
ax.set_ylabel('Amplitude')
|
|
|
|
|
ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(ampl_format))
|
|
|
|
|
ax.yaxis.set_major_locator(plt.MultipleLocator(.2 / ampl_scale))
|
2020-12-17 22:08:07 +00:00
|
|
|
|
|
2020-12-18 12:24:42 +00:00
|
|
|
|
# Rend le résultat
|
|
|
|
|
if output_file == '-':
|
|
|
|
|
plt.show()
|
|
|
|
|
else:
|
|
|
|
|
plt.tight_layout()
|
|
|
|
|
plt.savefig(output_file)
|