Paramétrisation des scripts et ajout README
This commit is contained in:
parent
e8bfb981cc
commit
de9d35b08b
|
@ -0,0 +1,7 @@
|
|||
<!-- vim: set spelllang=fr: -->
|
||||
|
||||
# Utilisation d’un sonagramme pour retrouver les notes d’un enregistrement
|
||||
|
||||
![](fig/synth-single.png)
|
||||
|
||||
![](fig/synth-shorttime.png)
|
|
@ -3,12 +3,30 @@ import numpy as np
|
|||
import scipy.signal as sig
|
||||
import matplotlib
|
||||
import matplotlib.pyplot as plt
|
||||
import sys
|
||||
|
||||
signal = soundbox.load_signal('out.wav')
|
||||
if len(sys.argv) != 3:
|
||||
print(f"""Utilisation: {sys.argv[0]} [source] [output]
|
||||
|
||||
Génère le sonagramme du fichier [source] dans le fichier [output].""")
|
||||
sys.exit(1)
|
||||
|
||||
source_file = sys.argv[1]
|
||||
output_file = sys.argv[2]
|
||||
|
||||
# Calcul du STFT
|
||||
signal = soundbox.load_signal(source_file)
|
||||
freq, time, fts = sig.stft(signal, soundbox.samp_rate, nperseg=soundbox.samp_rate * 0.5)
|
||||
|
||||
# Génération du graphe
|
||||
plt.rcParams.update({
|
||||
'figure.figsize': (8, 8),
|
||||
'font.size': 16,
|
||||
'font.family': 'Concourse T4',
|
||||
})
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
ax.tick_params(axis='both', which='major', labelsize=12)
|
||||
ax.pcolormesh(
|
||||
time, freq,
|
||||
np.abs(fts),
|
||||
|
@ -16,14 +34,14 @@ ax.pcolormesh(
|
|||
shading='gouraud')
|
||||
|
||||
|
||||
def freq_format(value, pos):
|
||||
return f'{value:.0f} Hz'
|
||||
|
||||
|
||||
def time_format(value, pos):
|
||||
return f'{value:.0f} s'
|
||||
|
||||
|
||||
def freq_format(value, pos):
|
||||
return f'{value:.0f} Hz'
|
||||
|
||||
|
||||
ax.set_xlabel('Temps')
|
||||
ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(time_format))
|
||||
|
||||
|
@ -31,4 +49,6 @@ ax.set_ylabel('Fréquence')
|
|||
ax.set_ylim(0, 800)
|
||||
ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(freq_format))
|
||||
|
||||
plt.show()
|
||||
# Sauvegarde comme image
|
||||
plt.tight_layout()
|
||||
plt.savefig(output_file)
|
||||
|
|
|
@ -2,22 +2,54 @@ import soundbox
|
|||
import numpy as np
|
||||
import matplotlib
|
||||
import matplotlib.pyplot as plt
|
||||
import sys
|
||||
|
||||
signal = soundbox.load_signal('out.wav')
|
||||
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
|
||||
dans le fichier [output].""")
|
||||
sys.exit(1)
|
||||
|
||||
source_file = sys.argv[1]
|
||||
output_file = sys.argv[2]
|
||||
|
||||
# Calcul du FFT
|
||||
signal = soundbox.load_signal(source_file)
|
||||
freqs = np.fft.fft(signal)
|
||||
scale = soundbox.samp_rate / len(signal)
|
||||
|
||||
# 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',
|
||||
})
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
ax.tick_params(axis='both', which='major', labelsize=12)
|
||||
ax.plot(np.absolute(freqs))
|
||||
|
||||
|
||||
def freq_format(value, pos):
|
||||
return f'{value * scale:.0f} Hz'
|
||||
return f'{value * freq_scale:.0f} Hz'
|
||||
|
||||
|
||||
def ampl_format(value, pos):
|
||||
return f'{value * ampl_scale:.1f}'
|
||||
|
||||
|
||||
ax.set_xlabel('Fréquence')
|
||||
ax.set_xlim(0 / scale, 800 / scale)
|
||||
ax.set_xlim(0 / freq_scale, 800 / freq_scale)
|
||||
ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(freq_format))
|
||||
ax.xaxis.set_major_locator(plt.MultipleLocator(100 / scale))
|
||||
ax.xaxis.set_major_locator(plt.MultipleLocator(100 / freq_scale))
|
||||
|
||||
plt.show()
|
||||
ax.set_ylabel('Amplitude')
|
||||
ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(ampl_format))
|
||||
ax.yaxis.set_major_locator(plt.MultipleLocator(.2 / ampl_scale))
|
||||
|
||||
# Sauvegarde comme image
|
||||
plt.tight_layout()
|
||||
plt.savefig(output_file)
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 162 KiB |
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
Binary file not shown.
Loading…
Reference in New Issue