-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyze_spectogram.py
47 lines (37 loc) · 1.87 KB
/
analyze_spectogram.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 dataset.spectogram.preprocess import multichannel_stft, multichannel_complex_to_log_mel
from dataset.dataset_utils import read_multichannel_audio
from dataset.spectogram import spectogram_configs as cfg
import matplotlib.pyplot as plt
import numpy as np
import soundfile
import matplotlib
matplotlib.use('TkAgg')
if __name__ == '__main__':
# audio_path = '/home/ariel/projects/sound/data/FilmClap/original/Meron/S005-S004T1.WAV'
# audio_path = '/home/ariel/projects/sound/data/FilmClap/original/StillJames/2C-T001.WAV'
# audio_path = '/home/ariel/projects/sound/data/FilmClap/original/JackRinger-05/161019_1233.wav'
audio_path = '/home/ariel/projects/sound/data/FilmClap/original/StillJames/8D-T001.WAV'
sec_start = 35.45
sec_end = 35.65
multichannel_waveform = read_multichannel_audio(audio_path=audio_path, target_fs=cfg.working_sample_rate)
multichannel_waveform = multichannel_waveform[int(cfg.working_sample_rate * sec_start): int(cfg.working_sample_rate * sec_end)]
soundfile.write("tmp_file.WAV", multichannel_waveform, cfg.working_sample_rate)
feature = multichannel_stft(multichannel_waveform)
feature = multichannel_complex_to_log_mel(feature)
frames_num = feature.shape[1]
tick_hop = max(1, frames_num // 20)
xticks = np.concatenate((np.arange(0, frames_num - tick_hop, tick_hop), [frames_num]))
xlabels = [f"{x / cfg.frames_per_second:.3f}s" for x in xticks]
fig = plt.figure()
ax = fig.add_subplot(211)
ax.matshow(feature[0].T, origin='lower', cmap='jet')
ax.set_xticks(xticks)
ax.set_xticklabels(xlabels, rotation='vertical')
ax.xaxis.set_ticks_position('bottom')
ax = fig.add_subplot(212)
signal = multichannel_waveform.mean(1)
ax.plot(range(len(signal)), signal)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.autoscale(tight=True)
plt.show()