forked from sagarjauhari/BCIpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.py
76 lines (67 loc) · 2.67 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
# /usr/bin/env python
# Copyright 2013, 2014 Justis Grant Peters and Sagar Jauhari
# This file is part of BCIpy.
#
# BCIpy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BCIpy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BCIpy. If not, see <http://www.gnu.org/licenses/>.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import butter, lfilter, freqz
import csv
def butter_bandpass(lowcut, highcut, fs, order=5):
"""
http://wiki.scipy.org/Cookbook/ButterworthBandpass
"""
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
# Default lowcut and highcut are from Bao Hong Tan's Thesis - p16
def butter_bandpass_filter(data, lowcut=0.1, highcut=20.0, fs=512.0, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
return pd.TimeSeries(lfilter(b, a, data), index=data.index.copy())
def plot_butter(fs, lowcut, highcut, orders, pdfpages):
"""Plot the frequency response for a few different orders."""
plt.figure()
plt.clf()
for order in orders:
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
w, h = freqz(b, a, worN=2000)
plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="order = %d" % order)
plt.title("Sample frequency responses of the band filter")
plt.xlabel('Frequency (Hz)')
plt.ylabel('Gain')
plt.grid(True)
plt.legend(loc='best')
pdfpages.savefig()
def do_filter_signal(data, low_cut, high_cut, fs, order, out_file):
data_np = data['Value']
data_filtered = butter_bandpass_filter(data_np,
low_cut,
high_cut,
fs,
order)
limit=2000
fig, ax = plt.subplots()
ax.plot(data[0:limit], label="Original Signal")
ax.plot(data_filtered[0:limit], label="Filtered Signal")
plt.grid(True)
plt.legend(loc='best')
plt.title("data[0:"+str(limit)+"]")
if out_file is not None:
with open(out_file,'w') as fo:
fw = csv.writer(fo)
fw.writerow(list(data_filtered))
return data_filtered