-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_sin_wave_data.py
47 lines (35 loc) · 1.42 KB
/
create_sin_wave_data.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
import os
import numpy as np
from librosa.output import write_wav
import time
def get_batch(batch_size, timesteps):
repeat_factor = 128.
low_freq_factor = np.random.uniform(size=(batch_size,))
high_freq_factor = np.random.uniform(size=(batch_size,))
x = np.arange(0, np.pi, np.pi/timesteps)
low_y = repeat_factor*(low_freq_factor + 1)[:, None]*x[None, :]
high_y = repeat_factor*20.*(high_freq_factor + 1)[:, None]*x[None, :]
noise = np.random.uniform(low=-0.001, high=0.001, size=high_y.shape)
batch = np.sin(high_y)*np.sin(low_y) + noise
return batch.astype('float32')
def create_sin_wave_data(data_dir, num_files, seq_len, sample_rate=16000, save_raw=False):
os.makedirs(data_dir, exist_ok=True)
dataset = get_batch(num_files, seq_len)
for i in range(num_files):
if save_raw:
dataset[i].tofile('sin_{}.csv'.format(i),
sep=',', format='%7.5f')
write_wav(
os.path.join(
data_dir, 'sin_{}.wav'.format(i)
),
dataset[i, :], sr=sample_rate, norm=False
)
if __name__ == "__main__":
data_dir = "datasets/sin_wave/"
file_len = 16000 * 8
num_files = 512
begin = time.time()
create_sin_wave_data(data_dir, num_files, file_len)
print("Total time taken for generating {} sequences of {} length = {}"\
.format(num_files, file_len, time.time() - begin))