Graphe du FFT en échelle log et style ggplot

This commit is contained in:
Mattéo Delabre 2020-12-18 19:59:55 +01:00
parent 7c3679ccaf
commit 575d9b5992
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
2 changed files with 17 additions and 12 deletions

View File

@ -17,21 +17,26 @@ output_file = sys.argv[2]
# Calcul du FFT # Calcul du FFT
signal = soundbox.load_signal(source_file) signal = soundbox.load_signal(source_file)
freqs = np.fft.fft(signal) freqs = np.fft.fft(signal)[:soundbox.samp_rate // 2]
values = np.absolute(freqs) / np.max(np.absolute(freqs))
# Génération du graphe # Génération du graphe
ampl_scale = 1 / np.max(np.absolute(freqs)) plt.style.use('ggplot')
freq_scale = soundbox.samp_rate / len(signal)
plt.rcParams.update({ plt.rcParams.update({
'figure.figsize': (8, 4), 'figure.figsize': (10, 5),
'font.size': 16, 'font.size': 16,
'font.family': 'Concourse T4', 'font.family': 'Concourse T4',
}) })
fig, ax = plt.subplots() fig, ax = plt.subplots()
ax.tick_params(axis='both', which='major', labelsize=12) ax.tick_params(axis='both', which='major', labelsize=12)
ax.plot(np.absolute(freqs))
remove = values < 0.01
x = np.arange(len(values))
ax.plot(x[remove == False], values[remove == False])
# Configuration des axes
freq_scale = soundbox.samp_rate / len(signal)
def freq_format(value, pos): def freq_format(value, pos):
@ -39,19 +44,19 @@ def freq_format(value, pos):
def ampl_format(value, pos): def ampl_format(value, pos):
return f'{value * ampl_scale:.1f}' return f'{value:.1f}'
ax.set_xlabel('Fréquence') ax.set_xlabel('Fréquence', labelpad=10)
ax.set_xlim(0 / freq_scale, 800 / freq_scale) ax.set_xscale('log', base=2)
ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(freq_format)) ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(freq_format))
ax.xaxis.set_major_locator(plt.MultipleLocator(100 / freq_scale)) ax.xaxis.set_major_locator(plt.MultipleLocator(100 / freq_scale))
ax.set_ylabel('Amplitude') ax.set_ylabel('Amplitude relative', labelpad=10)
ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(ampl_format)) ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(ampl_format))
ax.yaxis.set_major_locator(plt.MultipleLocator(.2 / ampl_scale)) ax.yaxis.set_major_locator(plt.MultipleLocator(.2))
# Rend le résultat # Rendu du résultat
if output_file == '-': if output_file == '-':
plt.show() plt.show()
else: else:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 32 KiB