-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathplotting.py
47 lines (34 loc) · 1.33 KB
/
plotting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from config import TrainConfig, ModelConfig
from data import Data
from librosa import amplitude_to_db, stft
from librosa.display import specshow
from preprocess import to_spectrogram, get_magnitude
from pylab import savefig
import matplotlib.pyplot as plt
import numpy as np
data = Data(TrainConfig.DATA_PATH)
mixed_wav, src1_wav, src2_wav, _ = data.next_wavs(TrainConfig.SECONDS, TrainConfig.NUM_WAVFILE)
mixed_spec = to_spectrogram(mixed_wav)
mixed_mag = get_magnitude(mixed_spec)
src1_spec, src2_spec = to_spectrogram(src1_wav), to_spectrogram(src2_wav)
src1_mag, src2_mag = get_magnitude(src1_spec), get_magnitude(src2_spec)
sr = ModelConfig.SR
y = src1_wav[0]
def plot_wav_as_spec(wav, sr=ModelConfig.SR, s=0.5, path='foo.png'):
"""Plots a spectrogram
Will save as foo.png in script directory
Arguments:
wav {array} -- audio data
Keyword Arguments:
sr {int} -- [sample rate] (default: {ModelConfig.SR})
s {number} -- [size of plot] (default: {0.5})
path {string} -- [where to save plot] (default: {foo.png})
"""
plt.figure(figsize=(12*s, 8*s))
D = amplitude_to_db(stft(y), ref=np.max)
# plt.subplot(4, 2, 1)
specshow(D, x_axis = 'time', y_axis='log')
plt.colorbar(format='%+2.0f dB')
plt.title('Log-frequency power spectrogram')
savefig(path)
plot_wav_as_spec(y)