-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.py
158 lines (135 loc) · 5.85 KB
/
filters.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
146
147
148
149
150
151
152
153
154
155
156
157
158
from __future__ import division
from scipy.signal import cheby1,filtfilt,butter,ellip
import numpy as np
"""
Contains
->Chebyshev Filter
->Butterworth Filter
->Elliptic Filter
Add
-2d data handling
"""
class BaseFilter:
def fit(self,X,y=None):
return self
def transform(self,X):
return X
class ChebshevFilter(BaseFilter):
def __init__(self,sampling_rate,fc1 = 0.1,fc2 = 15,order = 8,ripple = 0.5,input_type="continuous"):
self.fc1 = fc1
self.fc2 = fc2
self.order = order
self.ripple = ripple
self.fe = sampling_rate
self.input_type = input_type
def make_filter(self):
b,a = cheby1(self.order,self.ripple,[float(2*self.fc1)/self.fe,float(2*self.fc2)/self.fe],btype="bandpass",output="ba")
return b,a
def transform(self,X):
b,a = self.make_filter()
if self.input_type == "epoched":
n_epochs,n_samples,n_channels = X.shape
Xfilt = np.zeros((n_epochs,n_samples,n_channels))
for epoch in xrange(n_epochs):
for channel in xrange(n_channels):
Xfilt[epoch,:,channel] = filtfilt(b,a,X[epoch,:,channel])
elif self.input_type == "continuous":
n_samples,n_channels = X.shape
Xfilt = np.zeros((n_samples,n_channels))
for channel in xrange(n_channels):
Xfilt[:,channel] = filtfilt(b,a,X[:,channel].T)
else:
raise Exception("Incorrect input-type : choose 'epoched'/'continuous'")
return Xfilt
def __repr__(self):
return '_'.join(["ChebyshevFilter",str(self.fc1),str(self.fc2),str(self.order),str(self.fe),str(self.input_type),str(self.ripple)])
class ButterworthFilter(BaseFilter):
def __init__(self,fc1 = 0.1,fc2 = 15,order = 8,sampling_rate = 128,input_type="continuous"):
self.fc1 = fc1
self.fc2 = fc2
self.order = order
self.fe = sampling_rate
self.input_type = input_type
def make_filter(self):
b,a = butter(self.order,[2*self.fc1/self.fe,2*self.fc2/self.fe],output="ba",btype="bandpass")
return b,a
def transform(self,X):
b,a = self.make_filter()
if self.input_type == "epoched":
n_epochs,n_samples,n_channels = X.shape
Xfilt = np.zeros((n_epochs,n_samples,n_channels))
for epoch in xrange(n_epochs):
for channel in xrange(n_channels):
Xfilt[epoch,:,channel] = filtfilt(b,a,X[epoch,:,channel])
elif self.input_type == "continuous":
n_samples,n_channels = X.shape
Xfilt = np.zeros((n_samples,n_channels))
for channel in xrange(n_channels):
Xfilt[:,channel] = filtfilt(b,a,X[:,channel].T)
else:
raise Exception("Incorrect input-type : choose 'epoched'/'continuous'")
return Xfilt
def __repr__(self):
return '_'.join(["ButterworthFilter",str(self.fc1),str(self.fc2),str(self.order),str(self.fe),str(self.input_type)])
class EllipticFilter(BaseFilter):
def __init__(self,fc1 = 0.1,fc2 = 15,order = 8,sampling_rate = 128,attentuation = 60,input_type="continuous"):
self.fc1 = fc1
self.fc2 = fc2
self.order = order
self.fe = sampling_rate
self.input_type = input_type
self.attentuation = attentuation
def make_filter(self):
b,a = ellip(self.order,self.attentuation,[2*self.fc1/self.fe,2*self.fc2/self.fe],output="ba",btype="bandpass")
return b,a
def transform(self,X):
b,a = self.make_filter()
if self.input_type == "epoched":
n_epochs,n_samples,n_channels = X.shape
Xfilt = np.zeros((n_epochs,n_channels,n_samples))
for epoch in xrange(n_epochs):
for channel in xrange(n_channels):
Xfilt[epoch,:,channel] = filtfilt(b,a,X[epoch,:,channel])
elif self.input_type == "continuous":
n_samples,n_channels = X.shape
Xfilt = np.zeros((n_samples,n_channels))
for channel in xrange(n_channels):
Xfilt[:,channel] = filtfilt(b,a,X[:,channel])
else:
raise Exception("Incorrect input-type : choose 'epoched'/'continuous'")
return Xfilt
def __repr__(self):
return '_'.join(["EllipticFilter",str(self.fc1),str(self.fc2),str(self.order),str(self.fe),str(self.input_type)])
class FilterBank(BaseFilter):
def __init__(self,fc1 = 0.1,fc2 = 15,order = 8,ripple = 0.5,sampling_rate = 128,M = 8,input_type="continuous"):
self.fc1 = fc1
self.fc2 = fc2
self.order = order
self.fs = sampling_rate
self.input_type = input_type
self.ripple = ripple
self.M = M
self.d = (fc2 - fc1)/M
def filter_params(self,m):
Wn = [2*(self.fc1 + self.d*(m))/self.fs,2*(self.fc1 + self.d*(m+1))/self.fs]
b,a = cheby1(self.order,self.ripple,Wn,btype="bandpass")
return b,a
def transform(self,X):
if self.input_type == "epoched":
n_epochs,n_samples,n_channels = X.shape
Xfilt = np.zeros((n_epochs,n_samples,n_channels*self.M))
for epoch in xrange(n_epochs):
for channel in xrange(n_channels):
for m in xrange(self.M):
b,a = self.filter_params(m)
Xfilt[epoch,:,self.M*channel + m] = filtfilt(b,a,X[epoch,:,channel])
elif self.input_type == "continuous":
n_samples,n_channels = X.shape
Xfilt = np.zeros((n_samples,n_channels*self.M))
for channel in xrange(n_channels):
for m in xrange(self.M):
b,a = self.filter_params(m)
Xfilt[:,self.M*channel + m] = filtfilt(b,a,X[:,channel])
else:
raise Exception("Incorrect input-type : choose 'epoched'/'continuous'")
return Xfilt