Paramétrisation des scripts et ajout README

This commit is contained in:
Mattéo Delabre 2020-12-18 00:36:36 +01:00
parent e8bfb981cc
commit de9d35b08b
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
6 changed files with 71 additions and 12 deletions

7
README.md Normal file
View File

@ -0,0 +1,7 @@
<!-- vim: set spelllang=fr: -->
# Utilisation dun sonagramme pour retrouver les notes dun enregistrement
![](fig/synth-single.png)
![](fig/synth-shorttime.png)

View File

@ -3,12 +3,30 @@ import numpy as np
import scipy.signal as sig import scipy.signal as sig
import matplotlib import matplotlib
import matplotlib.pyplot as plt 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) 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() fig, ax = plt.subplots()
ax.tick_params(axis='both', which='major', labelsize=12)
ax.pcolormesh( ax.pcolormesh(
time, freq, time, freq,
np.abs(fts), np.abs(fts),
@ -16,14 +34,14 @@ ax.pcolormesh(
shading='gouraud') shading='gouraud')
def freq_format(value, pos):
return f'{value:.0f} Hz'
def time_format(value, pos): def time_format(value, pos):
return f'{value:.0f} s' return f'{value:.0f} s'
def freq_format(value, pos):
return f'{value:.0f} Hz'
ax.set_xlabel('Temps') ax.set_xlabel('Temps')
ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(time_format)) 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.set_ylim(0, 800)
ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(freq_format)) ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(freq_format))
plt.show() # Sauvegarde comme image
plt.tight_layout()
plt.savefig(output_file)

View File

@ -2,22 +2,54 @@ import soundbox
import numpy as np import numpy as np
import matplotlib import matplotlib
import matplotlib.pyplot as plt 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) 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() fig, ax = plt.subplots()
ax.tick_params(axis='both', which='major', labelsize=12)
ax.plot(np.absolute(freqs)) ax.plot(np.absolute(freqs))
def freq_format(value, pos): 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_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_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)

BIN
fig/synth-shorttime.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

BIN
fig/synth-single.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
sounds/synth.wav Normal file

Binary file not shown.