-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild_dataset.py
70 lines (53 loc) · 1.64 KB
/
build_dataset.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
#!/usr/bin/python
import cPickle
from os import listdir, path
import numpy as np
from scikits.audiolab import Sndfile, Format
import sys
import gzip
import random
def build_set(files):
size = len(files)
inputs = np.zeros((size, 200))
targets = np.zeros(size, dtype=np.int)
for i in range(size):
fn = files[i]
f = Sndfile('data/audio/' + fn, 'r')
sig = f.read_frames(f.nframes)
f.close()
sig = sig * np.hamming(200)
spec = np.abs(np.fft.fft(sig))
inputs[i] = spec
if fn[0] == 's':
targets[i] = 1
else:
targets[i] = 0
return (inputs, targets)
def main():
files = listdir('data/audio/')
mtimes = []
for f in files:
mtime = path.getmtime('data/audio/' + f)
mtimes.append([f, mtime])
files = sorted(mtimes, key=lambda m: m[1], reverse=False)
files = [f[0] for f in files]
n_files = [f for f in files if f[0] == 'n']
s_files = [f for f in files if f[0] == 's']
audio_files = []
n_index = 0
s_index = 0
for i in range(int(sys.argv[1])):
if i%2 == 0:
audio_files.append(n_files[n_index])
n_index = n_index + 1
else:
audio_files.append(s_files[s_index])
s_index = s_index + 1
test_set = build_set(audio_files[0:2000])
valid_set = build_set(audio_files[2000:4000])
train_set = build_set(audio_files[4000:len(audio_files)])
dataset = (train_set, valid_set, test_set)
with gzip.open('data/audio.pkl.gz', 'wb') as f:
cPickle.dump(dataset, f, cPickle.HIGHEST_PROTOCOL)
if __name__ == '__main__':
main()