-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.py
More file actions
68 lines (58 loc) · 2.07 KB
/
util.py
File metadata and controls
68 lines (58 loc) · 2.07 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#encoding: utf-8
import glob
import os
import librosa
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import wave
from matplotlib.pyplot import specgram
from task import DEBUG_DIR
def print_wave_info(file):
wf = wave.open(file, "r")
print "チャンネル数:", wf.getnchannels()
print "サンプル幅:", wf.getsampwidth()
print "サンプリング周波数:", wf.getframerate()
print "フレーム数:", wf.getnframes()
print "パラメータ:", wf.getparams()
print "長さ(秒):", float(wf.getnframes()) / wf.getframerate()
def load_sound_files(file_paths):
raw_sounds = []
for fp in file_paths:
X, sr = librosa.load(fp)
raw_sounds.append(X)
return raw_sounds
def plot_waves(sound_names, raw_sounds):
i = 1
fig = plt.figure(figsize=(25, 60), dpi=100)
for n, f in zip(sound_names, raw_sounds):
plt.subplot(3, 1, i)
librosa.display.waveplot(np.array(f), sr=22050)
plt.title(n.title())
i += 1
plt.suptitle('Figure 1: Waveplot', x=0.5, y=0.985, fontsize=18)
#plt.show()
plt.savefig(os.path.join(DEBUG_DIR, 'f1_waveplot.jpg'))
def plot_specgram(sound_names, raw_sounds):
i = 1
fig = plt.figure(figsize=(25, 60), dpi=100)
for n, f in zip(sound_names, raw_sounds):
plt.subplot(3, 1, i)
specgram(np.array(f), Fs=22050)
plt.title(n.title())
i += 1
plt.suptitle('Figure 2: Spectrogram', x=0.5, y=0.985, fontsize=18)
#plt.show()
plt.savefig(os.path.join(DEBUG_DIR, 'f2_spec.jpg'))
def plot_log_power_specgram(sound_names, raw_sounds):
i = 1
fig = plt.figure(figsize=(25, 60), dpi=100)
for n, f in zip(sound_names, raw_sounds):
plt.subplot(3, 1, i)
D = librosa.logamplitude(np.abs(librosa.stft(f)) ** 2, ref_power=np.max)
librosa.display.specshow(D, x_axis='time', y_axis='log')
plt.title(n.title())
i += 1
plt.suptitle('Figure 3: Log power spectrogram', x=0.5, y=0.985, fontsize=18)
#plt.show()
plt.savefig(os.path.join(DEBUG_DIR, 'f3_logpowerspec.jpg'))