-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_spectrograms_16k.py
157 lines (140 loc) · 6.64 KB
/
create_spectrograms_16k.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
import os
import numpy as np
import math
import fnmatch
from my_spectrogram import my_specgram
from collections import OrderedDict
from scipy.io import wavfile
import matplotlib.pylab as plt
from pylab import rcParams
from sklearn.model_selection import train_test_split
rcParams['figure.figsize'] = 6, 3
SCRIPT_DIR = os.getcwd()
INPUT_FOLDER = 'Input_audio_wav_16k/'
OUTPUT_FOLDER = 'Input_spectrogram_16k/'
languages = os.listdir(INPUT_FOLDER)
languages.sort()
audio_dict = OrderedDict()
for l in languages:
audio_dict[l] = sorted(os.listdir(INPUT_FOLDER + l))
def plot_spectrogram(audiopath, plotpath=None, NFFT_window=0.025,
noverlap_window=0.023, freq_min=None, freq_max=None,
axis='off'):
fs, data = wavfile.read(audiopath)
data = data / data.max()
center = data.mean() * 0.2
data = data + np.random.normal(center, abs(center * 0.5), len(data))
NFFT = pow(2, int(math.log(int(fs*NFFT_window), 2) + 0.5)) # 25ms window, nearest power of 2
noverlap = int(fs*noverlap_window)
fc = int(np.sqrt(freq_min*freq_max))
# Pxx is the segments x freqs array of instantaneous power, freqs is
# the frequency vector, bins are the centers of the time bins in which
# the power is computed, and im is the matplotlib.image.AxesImage
# instance
Pxx, freqs, bins, im = my_specgram(data, NFFT=NFFT, Fs=fs,
Fc=fc, detrend=None,
window=np.hanning(NFFT),
noverlap=noverlap, cmap='Greys',
xextent=None,
pad_to=None, sides='default',
scale_by_freq=None,
minfreq=freq_min, maxfreq=freq_max)
plt.axis(axis)
im.axes.axis('tight')
im.axes.get_xaxis().set_visible(False)
im.axes.get_yaxis().set_visible(False)
if plotpath:
plt.savefig(plotpath, bbox_inches='tight',
transparent=False, pad_inches=0, dpi=96)
else:
plt.show()
plt.clf()
# same as training but no added noise
def plot_spectrogram_val(audiopath, plotpath=None, NFFT_window=0.025,
noverlap_window=0.023, freq_min=None, freq_max=None,
axis='off'):
fs, data = wavfile.read(audiopath)
data = data / data.max()
NFFT = pow(2, int(math.log(int(fs*NFFT_window), 2) + 0.5)) # 25ms window, nearest power of 2
noverlap = int(fs*noverlap_window)
fc = int(np.sqrt(freq_min*freq_max))
# Pxx is the segments x freqs array of instantaneous power, freqs is
# the frequency vector, bins are the centers of the time bins in which
# the power is computed, and im is the matplotlib.image.AxesImage
# instance
Pxx, freqs, bins, im = my_specgram(data, NFFT=NFFT, Fs=fs,
Fc=fc, detrend=None,
window=np.hanning(NFFT),
noverlap=noverlap, cmap='Greys',
xextent=None,
pad_to=None, sides='default',
scale_by_freq=None,
minfreq=freq_min, maxfreq=freq_max)
plt.axis(axis)
im.axes.axis('tight')
im.axes.get_xaxis().set_visible(False)
im.axes.get_yaxis().set_visible(False)
if plotpath:
plt.savefig(plotpath, bbox_inches='tight',
transparent=False, pad_inches=0, dpi=96)
else:
plt.show()
plt.clf()
# create spectrograms of randomly drawn samples from each language
def find(pattern, path):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result[0]
random_wav = []
for key in audio_dict:
random_wav.append(sorted(np.random.choice(audio_dict[key], 500, replace=False)))
training_list = []
validation_list = []
for i in range(0, len(random_wav)):
x_train, x_val = train_test_split(random_wav[i],
test_size=0.4,
random_state=42)
training_list.append(x_train)
validation_list.append(x_val)
if not os.path.exists(OUTPUT_FOLDER + 'Training'):
os.makedirs(OUTPUT_FOLDER + 'Training')
print('Successfully created a training folder!')
print('Populating training folder with spectrograms...')
for i in range(0, len(training_list)):
if not os.path.exists(OUTPUT_FOLDER + 'Training/' + str(languages[i])):
os.makedirs(OUTPUT_FOLDER + 'Training/' + str(languages[i]))
print('Successfully created a {} training folder!'.format(languages[i]))
print('Populating {} training folder with spectrograms...'.format(languages[i]))
for j in range(0, len(training_list[i])):
for k in range(0, 3):
plot_spectrogram(find(training_list[i][j], INPUT_FOLDER),
plotpath=OUTPUT_FOLDER + 'Training/' +
str(languages[i]) + '/' +
str(training_list[i][j][:-4]) + '_' +
str(k) + '.jpeg',
NFFT_window=0.025, noverlap_window=0.023,
freq_min=0, freq_max=5500)
print('Done with {}.'.format(training_list[i][j][:-4]))
if not os.path.exists(OUTPUT_FOLDER + 'Validation'):
os.makedirs(OUTPUT_FOLDER + 'Validation')
print('Successfully created a validation folder!')
print('Populating validation folder with spectrograms...')
for i in range(0, len(validation_list)):
if not os.path.exists(OUTPUT_FOLDER + 'Validation/' + str(languages[i])):
os.makedirs(OUTPUT_FOLDER + 'Validation/' + str(languages[i]))
print('Successfully created a {} validation folder!'.format(languages[i]))
print('Populating {} validation folder with spectrograms...'.format(languages[i]))
for j in range(0, len(validation_list[i])):
for k in range(0, 1):
plot_spectrogram_val(find(validation_list[i][j], INPUT_FOLDER),
plotpath=OUTPUT_FOLDER + 'Validation/' +
str(languages[i]) + '/' +
str(validation_list[i][j][:-4]) + '_' +
str(k) + '.jpeg',
NFFT_window=0.025, noverlap_window=0.023,
freq_min=0, freq_max=5500)
print('Done with {}.'.format(validation_list[i][j][:-4]))