diff --git a/fft-graph.py b/fft-graph.py index 7e98ca1..5f08c09 100644 --- a/fft-graph.py +++ b/fft-graph.py @@ -32,34 +32,30 @@ freq = freq[left:right] values = values[left:right] # Génération du graphe -plt.style.use('ggplot') plt.rcParams.update({ 'figure.figsize': (10, 5), - 'font.size': 16, + 'font.size': 18, 'font.family': 'Concourse T4', }) fig, ax = plt.subplots() ax.tick_params(axis='both', which='major', labelsize=12) ax.plot(freq, values) +ax.grid(alpha=.3) # Configuration des axes -def freq_format(value, pos): - return f'{value * freq_scale:.0f} Hz' - - -def ampl_format(value, pos): - return f'{value:.1f}' - - ax.set_xlabel('Fréquence', labelpad=10) ax.set_xscale('log', base=2) -ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(freq_format)) -ax.xaxis.set_major_locator(plt.MultipleLocator(100 / freq_scale)) +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)) ax.set_ylabel('Amplitude relative', labelpad=10) -ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(ampl_format)) -ax.yaxis.set_major_locator(plt.MultipleLocator(.2)) +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)) # Rendu du résultat if output_file == '-': diff --git a/fig/piano-fft.png b/fig/piano-fft.png index 01eb74a..73b7cdd 100644 Binary files a/fig/piano-fft.png and b/fig/piano-fft.png differ diff --git a/fig/piano-stft.png b/fig/piano-stft.png index d96d3d0..2676692 100644 Binary files a/fig/piano-stft.png and b/fig/piano-stft.png differ diff --git a/fig/synth-fft.png b/fig/synth-fft.png index b37c593..9e3857a 100644 Binary files a/fig/synth-fft.png and b/fig/synth-fft.png differ diff --git a/fig/synth-stft.png b/fig/synth-stft.png index 9dab7e6..0292dbe 100644 Binary files a/fig/synth-stft.png and b/fig/synth-stft.png differ diff --git a/stft-graph.py b/stft-graph.py index dc5f47f..ab0ba57 100644 --- a/stft-graph.py +++ b/stft-graph.py @@ -17,45 +17,47 @@ output_file = sys.argv[2] # Calcul du STFT signal = soundbox.load_signal(source_file) + freq, time, vecs = sig.stft(signal, soundbox.samp_rate, nperseg=soundbox.samp_rate * 0.5) values = np.absolute(vecs) +# 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] + # Génération du graphe plt.rcParams.update({ 'figure.figsize': (10, 8), - 'figure.frameon': True, - 'font.size': 20, + 'font.size': 18, 'font.family': 'Concourse T4', }) -fig, ax = plt.subplots(frameon=True) +fig, ax = plt.subplots() ax.tick_params(axis='both', which='major', labelsize=12) - -freq_filter = values.max(axis=1) / np.max(values) >= 0.01 -x = np.arange(len(values)) - -ax.pcolormesh( - time, freq[freq_filter], values[freq_filter], - cmap='Greys', - shading='gouraud') +ax.pcolormesh(time, freq, values, cmap='Greys', shading='gouraud') +ax.grid(alpha=.3) # Configuration des axes -def time_format(value, pos): - return f'{value:.0f} s' - - def freq_format(value, pos): return f'{value:.0f} Hz' ax.set_xlabel('Temps', labelpad=10) -ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(time_format)) +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)) ax.set_ylabel('Fréquence', labelpad=10) ax.set_yscale('log', base=2) ax.yaxis.set_major_locator(plt.MultipleLocator(100)) -ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(freq_format)) -ax.grid(alpha=.3) +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)) # Rendu du résultat if output_file == '-':