-
Notifications
You must be signed in to change notification settings - Fork 1
/
segment.py
40 lines (34 loc) · 1.24 KB
/
segment.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
import numpy as np
class Segment1D:
"""
Segment 1D signal
"""
@classmethod
def apply(cls, x, window_size=200, step_size=50):
"""
@param x: 1D signal
@param window_size: window size.Default: 200.
@param step_size: step size. Default: 50
"""
# length = window_size - int(len(x) % window_size)
# pad = np.array([0] * length)
# x_padded = np.concatenate([x, pad])
segments = [x[i:i + window_size] for i in range(0, x.shape[0], step_size) if (i + window_size < x.shape[0])]
return segments
class SegmentND:
"""
Segment multi-channels signal
"""
@classmethod
def apply(cls, x, window_size=200, step_size=50):
"""
@param x: 2D signal
@param window_size: window size. Default: 200.
@param step_size: step size. Default: 50
"""
# length = (x.shape[1] - step_size + window_size) % window_size
# make sure there are an even number of windows before stride tricks
# pad = np.zeros((length, x.shape[1]))
# x_padded = np.vstack([x, pad])
segments = [x[i:i + window_size, :] for i in range(0, x.shape[0], step_size) if (i + window_size < x.shape[0])]
return segments