-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelper.py
More file actions
executable file
·53 lines (38 loc) · 1.26 KB
/
helper.py
File metadata and controls
executable file
·53 lines (38 loc) · 1.26 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
44
45
46
47
48
49
50
51
52
53
import numpy as np
__all__ = ["next_p2", "pad"]
def next_p2(n):
"""Returns the smallest power of 2 greater than n"""
if n < 1:
raise ValueError("n must be >= 1")
return 1 << (n-1).bit_length()
def pad(x, method='reflection'):
"""Pad to bring the total length N up to the next-higher
power of two.
Args:
x (1d ndarray): data
method (str): 'reflection', 'periodic' or 'zeros'
Returns:
xp, orig (1d ndarray, 1d ndarray boolean):
padded version of x and a boolean array with
value True where xp contains the original data
"""
x_arr = np.asarray(x)
if not method in ['reflection', 'periodic', 'zeros']:
raise ValueError('Unavailable padding method')
diff = next_p2(x_arr.shape[0]) - x_arr.shape[0]
ldiff = int(diff / 2)
rdiff = diff - ldiff
if method == 'reflection':
left_x = x_arr[:ldiff][::-1]
right_x = x_arr[-rdiff:][::-1]
elif method == 'periodic':
left_x = x_arr[:ldiff]
right_x = x_arr[-rdiff:]
elif method == 'zeros':
left_x = np.zeros(ldiff, dtype=x_arr.dtype)
right_x = np.zeros(rdiff, dtype=x_arr.dtype)
xp = np.concatenate((left_x, x_arr, right_x))
orig = np.ones(x_arr.shape[0] + diff, dtype=np.bool)
orig[:ldiff] = False
orig[-rdiff:] = False
return xp, orig