-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcum2x.py
More file actions
42 lines (38 loc) · 1.66 KB
/
Copy pathcum2x.py
File metadata and controls
42 lines (38 loc) · 1.66 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
import numpy as np
def cum2x (x,y, maxlag, nsamp, overlap):
assert len(x) == len(y), "The two signal should be same length!"
assert maxlag >= 0, " 'maxlag' must be non-negative!"
if nsamp > len(x) or nsamp <= 0:
nsamp = len(x)
overlap = overlap/100*nsamp
nadvance = nsamp - overlap
nrecs = (len(x)-overlap)/nadvance
nlags = 2*maxlag+1
y_cum = np.zeros(nlags, dtype=float)
count = np.zeros(nlags, dtype=float)
ind = 0
for k in range(nrecs):
xs = x[ind:(ind+nsamp)]
xs = np.array([j-float(sum(xs))/sum(1 for i in xs if i!=0) if j!=0 else 0 for j in xs])
ys = y[ind:(ind+nsamp)]
ys = np.array([j-float(sum(ys))/sum(1 for i in ys if i!=0) if j!=0 else 0 for j in ys])
temp = xs*ys
y_cum[maxlag] += reduce(lambda m,n:m+n,temp, 0)
count[maxlag] += sum(1 for i in temp if i!=0)
for m in range(1,maxlag+1):
temp = xs[m:nsamp]*ys[:nsamp-m]
y_cum[maxlag-m] = y_cum[maxlag-m]+reduce(lambda i,j:i+j,temp, 0)
count[maxlag-m] += sum(1 for i in temp if i!=0)
temp = xs[:nsamp-m]*ys[m:nsamp]
y_cum[maxlag+m] = y_cum[maxlag+m]+reduce(lambda i,j:i+j,temp, 0)
count[maxlag+m] += sum(1 for i in temp if i!=0)
ind += nadvance
# if flag == "biased":
# scale = np.ones(nlags, dtype=float)/nsamp/nrecs
# elif flag == "unbiased":
# scale = np.array(range(nsamp-maxlag,nsamp+1)+range(nsamp-1,nsamp-maxlag-1,-1))
# scale = np.ones(2*maxlag+1, dtype=float)/scale
# else:
# raise Exception("The flag should be either 'biased' or 'unbiased'!!")
scale = 1./count
return y_cum*scale