-
Notifications
You must be signed in to change notification settings - Fork 1
/
matlab.py
70 lines (59 loc) · 1.29 KB
/
matlab.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
70
import numpy as np
def movmean(A, k, discard_endpoints=True, std=False):
"""
M = movmean(A, k) returns an array of local k-point mean values,
where each mean is calculated over a sliding window of length k
across neighboring elements of A.
"""
k1 = k[0]
k2 = k[1]
new_array = []
for i in range(len(A)):
low = i-k1
high = i+k2+1
if low < 0:
if discard_endpoints:
continue
else:
low = 0
if high > len(A):
if discard_endpoints:
continue
else:
high = len(A)
this = A[low:high]
if std:
to_append = np.std(this, ddof=1)
else:
to_append = np.mean(this)
new_array.append(to_append)
return np.array(new_array)
def movmax(A, k, discard_endpoints=False, return_min=False):
"""
M = movmax(A,k) returns an array of local k-point maximum values,
where each maximum is calculated over a sliding window of length
k across neighboring elements of A
"""
k1 = k[0]
k2 = k[1]
new_array = []
for i in range(len(A)):
low = i-k1
high = i+k2+1
if low < 0:
if discard_endpoints:
continue
else:
low = 0
if high > len(A):
if discard_endpoints:
continue
else:
high = len(A)
this = A[low:high]
if return_min:
to_append = min(this)
else:
to_append = max(this)
new_array.append(to_append)
return np.array(new_array)