-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_augmentation.py
More file actions
43 lines (35 loc) · 1.17 KB
/
data_augmentation.py
File metadata and controls
43 lines (35 loc) · 1.17 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
from scipy.signal import resample
import numpy as np
def stretch_hor(vec, intensity):
"""
Applies horizontal stretching to a copy of the given vector.
:returns: Modified copy of given vector.
"""
low = int(len(vec) - len(vec) * intensity)
high = int(len(vec) + len(vec) * intensity)
res_vec = resample(vec, np.random.randint(low, high))
if len(res_vec) < len(vec):
result = np.zeros(len(vec))
result[:len(res_vec)] = res_vec
else:
result = res_vec[:len(vec)]
return result
def stretch_ver(vec, intensity):
"""
Applies vertical stretch to a copy of the given vector.
:returns: Modified copy of given vector.
"""
result = vec + vec * intensity * (.5 - np.random.rand())
return result
def modify_vector(vec, intensity):
"""
Applies amplification and/or stretching to a copy of the given vector.
:returns: Modified copy of given vector.
"""
mod_type = np.random.randint(0, 3)
if mod_type is 0:
return stretch_ver(vec, intensity)
elif mod_type is 1:
return stretch_hor(vec, intensity)
else:
return stretch_ver(stretch_hor(vec, intensity), intensity)