forked from minjunJchoi/fluctana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filtdata.py
145 lines (115 loc) · 4.1 KB
/
filtdata.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python2.7
# Author : Minjun J. Choi ([email protected])
#
# Description : Filter data
#
# Acknowledgement : TomRoelandts.com
#
# Last updated
# 2018.12.15 : version 0.10;
import numpy as np
import h5py
import matplotlib.pyplot as plt
import stats as st
class FirFilter(object):
def __init__(self, name, fs, fL, fH, b=0.08):
self.name = name
self.fs = fs
self.fL = fL
self.fH = fH
self.b = b
N = int(np.ceil((4 / b)))
if not N % 2: N += 1
self.N = N
self.fir_coef = np.ones(N)
if name == 'FIR_pass' and fL == 0:
self.fir_coef = self.fir_lowpass(float(fH/fs), N)
elif name == 'FIR_pass' and fH == 0:
self.fir_coef = self.fir_lowpass(float(fL/fs), N)
elif name == 'FIR_block':
self.fir_coef = self.fir_bandblock(fL/fs, fH/fs, N)
def apply(self, x):
xlp = np.convolve(x, self.fir_coef)
if self.name == 'FIR_pass' and self.fH == 0: # high pass filter
x = x - xlp[int(self.N/2):int(self.N/2 + len(x))] # delay correction
else:
x = xlp[int(self.N/2):int(self.N/2 + len(x))] # delay correction
return x
def fir_lowpass(self, fc, N):
n = np.arange(N)
# Compute sinc filter.
h = np.sinc(2 * fc * (n - (N - 1) / 2.))
# Compute Blackman window.
w = np.blackman(N)
# Multiply sinc filter with window.
h = h * w
# Normalize to get unity gain.
h = h / np.sum(h)
return h
def fir_bandblock(self, fL, fH, N):
n = np.arange(N)
# Compute a low-pass filter with cutoff frequency fL.
hlpf = np.sinc(2 * fL * (n - (N - 1) / 2.))
hlpf *= np.blackman(N)
hlpf /= np.sum(hlpf)
# Compute a high-pass filter with cutoff frequency fH.
hhpf = np.sinc(2 * fH * (n - (N - 1) / 2.))
hhpf *= np.blackman(N)
hhpf /= np.sum(hhpf)
hhpf = -hhpf
hhpf[int((N - 1) / 2)] += 1
# Add both filters.
h = hlpf + hhpf
return h
class SvdFilter(object):
def __init__(self, cutoff=0.9):
self.cutoff = cutoff
def apply(self, data, verbose=0):
good_channels = self.check_data(data)
cnum, tnum = data.shape
X = np.zeros((tnum, int(np.sum(good_channels))))
xm = np.zeros(int(np.sum(good_channels)))
cnt = 0
for c in range(cnum):
if good_channels[c] == 1:
X[:,cnt] = data[c,:]/np.sqrt(tnum)
xm[cnt] = np.mean(X[:,cnt])
X[:,cnt] = X[:,cnt] - xm[cnt]
cnt += 1
# Do SVD
U, s, Vt = np.linalg.svd(X, full_matrices=False)
# energy of mode and the entropy
sv = s**2
E = np.sum(sv)
pi = sv / E
nsent = st.ns_entropy(pi)
print('The normalized Shannon entropy of sv is {:g}'.format(nsent))
if verbose == 1:
ax1 = plt.subplot(211)
ax1.plot(pi)
ax2 = plt.subplot(212)
ax2.plot(np.cumsum(sv)/np.sum(sv))
ax2.axhline(y=self.cutoff, color='r')
ax1.set_ylabel('SV power')
ax2.set_ylabel('Cumulated sum')
ax2.set_xlabel('Mode number')
plt.show()
# filtering
s[np.cumsum(sv)/np.sum(sv) >= self.cutoff] = 0
# reconstruct
S = np.diag(s)
reX = np.dot(U, np.dot(S, Vt))
# print('reconstructed {:0}'.format(np.allclose(X, reX)))
cnt = 0
for c in range(cnum):
if good_channels[c] == 1:
data[c,:] = (reX[:,cnt] + xm[cnt])*np.sqrt(tnum)
cnt += 1
return data
def check_data(self, data):
cnum, tnum = data.shape
good_channels = np.ones(cnum)
for c in range(cnum):
if np.std(data[c,:]) == 0 or ~np.isfinite(np.sum(data[c,:])): # saturated or bad number
good_channels[c] = 0
return good_channels