-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_myfilter.py
157 lines (140 loc) · 5.02 KB
/
test_myfilter.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
# test_myfilter.py ---
#
# Filename: test_myfilter.py
# Description:
# Author: Subhasis Ray
# Maintainer:
# Created: Tue Nov 1 10:20:19 2011 (+0530)
# Version:
# Last-Updated: Tue Nov 1 22:44:42 2011 (+0530)
# By: Subhasis Ray
# Update #: 384
# URL:
# Keywords:
# Compatibility:
#
#
# Commentary:
#
#
#
#
# Change log:
#
#
#
# Code:
import numpy
import pylab
import h5py
from datetime import datetime
from scipy import weave
from scipy.weave import inline, converters
from scipy import signal
filename = '/media/sda6/cortical_data/2011_10_21/data_20111021_143831_21310.h5'
fhandle = h5py.File(filename, 'r')
datanode = fhandle['/lfp/electrode_1000um']
data = numpy.zeros(len(datanode))
data[:] = datanode[:]
sampling_interval = fhandle.attrs['plotdt']
def blackmann_windowedsinc_filter(data, sampling_interval, cutoff, rolloff):
print 'Sampling rate:', 1/sampling_interval
print 'Cutoff frequency:', cutoff
print 'Rolloff frequency:', rolloff
start = datetime.now()
m = int(4.0 / (rolloff * sampling_interval) - 0.5)
if m%2 == 1:
m += 1
cutoff = cutoff * sampling_interval
indices = numpy.linspace(0.0, m+1, m+1)
syncwin = 2 * cutoff * numpy.sinc(2*cutoff*(indices-m/2))
# pylab.plot(indices, syncwin, label='sinc window')
blackmann = 0.42 - 0.5 * numpy.cos(2 * numpy.pi * indices / m) + 0.08 * numpy.cos(4 * numpy.pi * indices / m)
lowpass = syncwin * blackmann
lowpass = lowpass/ numpy.sum(lowpass)
filtered_data = numpy.convolve(lowpass, data, mode='same')
end = datetime.now()
delta = end - start
print 'blackmann', '%g s for sample of length %d' % (delta.days * 86400 + delta.seconds + delta.microseconds * 1e-6, len(data))
return filtered_data
# pylab.plot(indices, lowpass, label='kernel')
# pylab.plot(indices, blackmann, label='Blackmann window')
# pylab.legend()
# fig2 = pylab.figure()
# ax2 = fig2.add_subplot(1, 1, 1)
# ts = numpy.linspace(0, sampling_interval * len(data), len(data))
# ax2.plot(ts, data, label='Raw data')
# ax2.plot(ts, filtered_data, label='Filtered with Blackmann window')
# pylab.show()
def c_blackmann_windowedsinc_filter(data, sampling_interval, cutoff, rolloff):
start = datetime.now()
m = int(4.0 / (rolloff * sampling_interval) - 0.5)
if m%2 == 1:
m += 1
cutoff = cutoff * sampling_interval
sample_count = len(data)
result = numpy.zeros(sample_count, dtype='float64')
lowpass = numpy.zeros(m);
code = """
double sum = 0.0, tmp;
double pi = 3.141592;
for ( int ii = 0; ii < m; ++ii){
if ( ii == m / 2 ){
tmp = 2 * pi * cutoff;
} else {
tmp = sin(2 * pi * cutoff * (ii - m/2))/ (ii - m/2);
}
tmp *= (0.42 - 0.5 * cos(2 * pi * ii / m) + 0.08 * cos(4 * pi * ii / m));
sum += tmp;
lowpass[ii] = tmp;
}
for ( int ii = 0; ii < m; ++ii){
lowpass[ii] /= sum;
}
for (int jj = 0; jj < sample_count; ++jj){
result[jj] = 0.0;
for (int ii = 0; ii < m; ++ii){
result[jj] += (data[jj - ii] * lowpass[ii]);
}
}
"""
weave.inline(code, ['data', 'sample_count', 'lowpass', 'cutoff', 'm', 'result'])
end = datetime.now()
delta = end - start
print 'cblackmann', '%g s for sample of length %d' % (delta.days * 86400 + delta.seconds + delta.microseconds * 1e-6, len(data))
# indices = numpy.linspace(0.0, m+1, m+1)
# ts = numpy.linspace(0, sampling_interval * len(data), len(data))
# pylab.plot(ts, data, label='raw data')
# pylab.plot(ts[:sample_count-m/2], result[m/2:], label='filtered data')
# pylab.legend()
# pylab.show()
return result
def scipy_fir(data, sampling_interval, cutoff, rolloff):
"""Use scip.signal for filtering"""
start = datetime.now()
numtaps = int(4/(rolloff*sampling_interval) + 0.5)
nyq = 0.5/sampling_interval
lowpass = signal.firwin(numtaps, cutoff/nyq, width=rolloff/nyq)
result = signal.lfilter(lowpass, 1.0, data)
end = datetime.now()
delta = end - start
print 'scipy', '%g s for sample of length %d' % (delta.days * 86400 + delta.seconds + delta.microseconds * 1e-6, len(data))
return result
if __name__ == '__main__':
cutoff = 450
rolloff = 10
filtered = blackmann_windowedsinc_filter(data, sampling_interval, cutoff, rolloff)
c_filtered = c_blackmann_windowedsinc_filter(data, sampling_interval, cutoff, rolloff)
scipy_fir_filtered = scipy_fir(data, sampling_interval, cutoff, rolloff)
ts = numpy.linspace(0, sampling_interval * len(data), len(data))
m = int(4.0 / (rolloff * sampling_interval) - 0.5)
if m%2 == 1:
m += 1
pylab.plot(ts, data, 'b-', label='raw')
pylab.plot(ts[:len(data) - m/2], c_filtered[m/2:], 'r-.', label='c')
pylab.plot(ts, filtered, 'g-', label='numpy filtered')
pylab.plot(ts, scipy_fir_filtered, 'k-', label='scipy filtered')
pylab.legend()
pylab.show()
#
# test_myfilter.py ends here