/
plotfreq.py
executable file
·353 lines (299 loc) · 13.7 KB
/
plotfreq.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python
# Plotfreq plots spectral data from the buffer and allows
# interactive selection of frequency bands for further processing
#
# Plotfreq is part of the EEGsynth project (https://github.com/eegsynth/eegsynth)
#
# Copyright (C) 2017 EEGsynth project
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
from pyqtgraph.Qt import QtGui, QtCore
from scipy.interpolate import interp1d
from scipy.signal import butter, lfilter
import ConfigParser # this is version 2.x specific, on version 3.x it is called "configparser" and has a different API
import redis
import argparse
import numpy as np
import os
import pyqtgraph as pg
import sys
import time
from scipy.signal import butter, lfilter, detrend, iirnotch, filtfilt, decimate
from scipy.interpolate import interp1d
from scipy.fftpack import fft, fftfreq
if hasattr(sys, 'frozen'):
basis = sys.executable
elif sys.argv[0]!='':
basis = sys.argv[0]
else:
basis = './'
installed_folder = os.path.split(basis)[0]
# eegsynth/lib contains shared modules
sys.path.insert(0, os.path.join(installed_folder, '../../lib'))
import EEGsynth
import FieldTrip
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--inifile", default=os.path.join(installed_folder, os.path.splitext(os.path.basename(__file__))[0] + '.ini'), help="optional name of the configuration file")
args = parser.parse_args()
config = ConfigParser.ConfigParser()
config.read(args.inifile)
try:
r = redis.StrictRedis(host=config.get('redis','hostname'), port=config.getint('redis','port'), db=0)
response = r.client_list()
except redis.ConnectionError:
print "Error: cannot connect to redis server"
exit()
# combine the patching from the configuration file and Redis
patch = EEGsynth.patch(config, r)
del config
# this determines how much debugging information gets printed
debug = patch.getint('general','debug')
def butter_bandpass(lowcut, highcut, fs, order=9):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_filter(data, lowcut, highcut, fs, order=9):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = lfilter(b, a, data)
return y
def butter_lowpass(lowcut, fs, order=9):
nyq = 0.5 * fs
low = lowcut / nyq
b, a = butter(order, low, btype='low')
return b, a
def butter_lowpass_filter(data, lowcut, fs, order=9):
b, a = butter_lowpass(lowcut, fs, order=order)
y = lfilter(b, a, data)
return y
def notch(f0, fs, Q=30):
# Q = Quality factor
w0 = f0 / (fs / 2) # Normalized Frequency
b, a = iirnotch(w0, Q)
return b, a
def notch_filter(data, f0, fs, Q=30):
b, a = notch(f0, fs, Q=Q)
y = lfilter(b, a, data)
return y
try:
ftc_host = patch.getstring('fieldtrip','hostname')
ftc_port = patch.getint('fieldtrip','port')
if debug>0:
print 'Trying to connect to buffer on %s:%i ...' % (ftc_host, ftc_port)
ft_input = FieldTrip.Client()
ft_input.connect(ftc_host, ftc_port)
if debug>0:
print "Connected to input FieldTrip buffer"
except:
print "Error: cannot connect to input FieldTrip buffer"
exit()
hdr_input = None
while hdr_input is None:
if debug>0:
print "Waiting for data to arrive..."
hdr_input = ft_input.getHeader()
time.sleep(0.2)
print "Data arrived"
# read variables from ini/redis
chanlist = patch.getstring('arguments','channels').split(",")
chanarray = np.array(chanlist)
for i in range(len(chanarray)):
chanarray[i] = int(chanarray[i]) - 1 # since python using indexing from 0 instead of 1
numchannel = len(chanlist)
window = patch.getfloat('arguments', 'window') # in seconds
window = int(round(window*hdr_input.fSample)) # in samples
stepsize = patch.getfloat('arguments', 'stepsize') # in seconds
historysize = patch.getfloat('arguments', 'historysize') # in seconds
numhistory = int(historysize / stepsize) # number of observation in history
freqaxis = fftfreq(window, 1 / hdr_input.fSample)
history = np.empty((numchannel, freqaxis.shape[0], numhistory))
history[:] = np.NAN
lrate = patch.getfloat('arguments', 'learning_rate')
scalered = patch.getfloat('scale', 'red')
scaleblue = patch.getfloat('scale', 'blue')
offsetred = patch.getfloat('offset', 'red')
offsetblue = patch.getfloat('offset', 'blue')
winx = patch.getfloat('display', 'xpos')
winy = patch.getfloat('display', 'ypos')
winwidth = patch.getfloat('display', 'width')
winheight = patch.getfloat('display', 'height')
# initialize graphical window
app = QtGui.QApplication([])
win = pg.GraphicsWindow(title="EEGsynth")
win.setWindowTitle('EEGsynth')
win.setGeometry(winx, winy, winwidth, winheight)
# initialize graphical elements
text_redleft = pg.TextItem(".", anchor=(1, 0), color='r')
text_redright = pg.TextItem(".", anchor=(0, 0), color='r')
text_blueleft = pg.TextItem(".", anchor=(1, -1), color='b')
text_blueright = pg.TextItem(".", anchor=(0, -1), color='b')
text_redleft_hist = pg.TextItem(".", anchor=(1, 0), color='r')
text_redright_hist = pg.TextItem(".", anchor=(0, 0), color='r')
text_blueleft_hist = pg.TextItem(".", anchor=(1, -1), color='b')
text_blueright_hist = pg.TextItem(".", anchor=(0, -1), color='b')
# Enable antialiasing for prettier plots
pg.setConfigOptions(antialias=True)
# Initialize variables
freqplot = []
freqplot_hist = []
spect = []
spect_hist = []
redleft = []
redright = []
blueleft = []
blueright = []
redleft_hist = []
redright_hist = []
blueleft_hist = []
blueright_hist = []
FFT = []
FFT_old = []
FFT_hist = []
specmax = []
specmin = []
specmax_hist = []
specmin_hist = []
# Create panels for each channel
for ichan in range(numchannel):
channr = int(chanarray[ichan]) + 1
freqplot.append(win.addPlot(title="%s%s" % ('Spectrum channel ', channr)))
freqplot[ichan].setLabel('left', text = 'Power')
freqplot[ichan].setLabel('bottom', text = 'Frequency (Hz)')
spect.append(freqplot[ichan].plot(pen='w'))
redleft.append(freqplot[ichan].plot(pen='r'))
redright.append(freqplot[ichan].plot(pen='r'))
blueleft.append(freqplot[ichan].plot(pen='b'))
blueright.append(freqplot[ichan].plot(pen='b'))
freqplot_hist.append(win.addPlot(title="%s%s%s%s%s" % ('Averaged spectrum channel ', channr, ' (', historysize, 's)')))
freqplot_hist[ichan].setLabel('left', text = 'Power')
freqplot_hist[ichan].setLabel('bottom', text = 'Frequency (Hz)')
spect_hist.append(freqplot_hist[ichan].plot(pen='w'))
redleft_hist.append(freqplot_hist[ichan].plot(pen='r'))
redright_hist.append(freqplot_hist[ichan].plot(pen='r'))
blueleft_hist.append(freqplot_hist[ichan].plot(pen='b'))
blueright_hist.append(freqplot_hist[ichan].plot(pen='b'))
win.nextRow()
# initialize as lists
specmin.append(0)
specmax.append(0)
specmin_hist.append(0)
specmax_hist.append(0)
FFT.append(0)
FFT_old.append(0)
FFT_hist.append(0)
# print frequency at lines
freqplot[0].addItem(text_redleft)
freqplot[0].addItem(text_redright)
freqplot[0].addItem(text_blueleft)
freqplot[0].addItem(text_blueright)
freqplot_hist[0].addItem(text_redleft_hist)
freqplot_hist[0].addItem(text_redright_hist)
freqplot_hist[0].addItem(text_blueleft_hist)
freqplot_hist[0].addItem(text_blueright_hist)
def update():
global specmax, specmin, specmax_hist, specmin_hist, FFT_old, FFT_hist, redfreq, redwidth, bluefreq, bluewidth, counter, history
# get last data
last_index = ft_input.getHeader().nSamples
begsample = (last_index-window)
endsample = (last_index-1)
data = ft_input.getData([begsample, endsample])
print "reading from sample %d to %d" % (begsample, endsample)
# demean and detrend data before filtering to reduce edge artefacts and center timecourse
# data = data - np.sum(data, axis=0)/float(len(data))
data = detrend(data, axis=0)
# Notch filter - DOES NOT WORK
# data = notch_filter(data, 10, hdr_input.fSample, 30)
# taper data
taper = np.hanning(len(data))
data = data*taper[:, np.newaxis]
# shift data to next sample
history = np.roll(history, 1, axis=2)
for ichan in range(numchannel):
channr = int(chanarray[ichan])
# current FFT with smoothing
FFT_temp = abs(fft(data[:, int(chanarray[ichan])]))
FFT[ichan] = FFT_temp * lrate + FFT_old[ichan] * (1-lrate)
FFT_old[ichan] = FFT[ichan]
# update history with current FFT
history[ichan, :, numhistory-1] = FFT_temp
FFT_hist = np.nanmean(history, axis=2)
# user-selected frequency band
arguments_freqrange = patch.getstring('arguments', 'freqrange').split("-")
arguments_freqrange = [float(s) for s in arguments_freqrange]
freqrange = np.greater(freqaxis, arguments_freqrange[0]) & np.less_equal(freqaxis, arguments_freqrange[1])
# update panels
spect[ichan].setData(freqaxis[freqrange], FFT[ichan][freqrange])
spect_hist[ichan].setData(freqaxis[freqrange], FFT_hist[ichan][freqrange])
# adapt the vertical scale to the running mean of max
specmax[ichan] = float(specmax[ichan]) * (1-lrate) + lrate * max(FFT[ichan][freqrange])
specmin[ichan] = float(specmin[ichan]) * (1-lrate) + lrate * min(FFT[ichan][freqrange])
specmax_hist[ichan] = float(specmax_hist[ichan]) * (1-lrate) + lrate * max(FFT_hist[ichan][freqrange])
specmin_hist[ichan] = float(specmin_hist[ichan]) * (1-lrate) + lrate * min(FFT_hist[ichan][freqrange])
freqplot[ichan].setYRange(specmin[ichan], specmax[ichan])
freqplot_hist[ichan].setYRange(specmin_hist[ichan], specmax_hist[ichan])
# update plotted lines
redfreq = patch.getfloat('input', 'redfreq', default=10./arguments_freqrange[1])
redfreq = EEGsynth.rescale(redfreq, slope=scalered, offset=offsetred) * arguments_freqrange[1]
redwidth = patch.getfloat('input', 'redwidth', default=1./arguments_freqrange[1])
redwidth = EEGsynth.rescale(redwidth, slope=scalered, offset=offsetred) * arguments_freqrange[1]
bluefreq = patch.getfloat('input', 'bluefreq', default=20./arguments_freqrange[1])
bluefreq = EEGsynth.rescale(bluefreq, slope=scaleblue, offset=offsetblue) * arguments_freqrange[1]
bluewidth = patch.getfloat('input', 'bluewidth', default=4./arguments_freqrange[1])
bluewidth = EEGsynth.rescale(bluewidth, slope=scaleblue, offset=offsetblue) * arguments_freqrange[1]
redleft[ichan].setData(x=[redfreq-redwidth, redfreq-redwidth], y=[specmin[ichan], specmax[ichan]])
redright[ichan].setData(x=[redfreq+redwidth, redfreq+redwidth], y=[specmin[ichan], specmax[ichan]])
blueleft[ichan].setData(x=[bluefreq-bluewidth, bluefreq-bluewidth], y=[specmin[ichan], specmax[ichan]])
blueright[ichan].setData(x=[bluefreq+bluewidth, bluefreq+bluewidth], y=[specmin[ichan], specmax[ichan]])
redleft_hist[ichan].setData(x=[redfreq-redwidth, redfreq-redwidth], y=[specmin_hist[ichan], specmax_hist[ichan]])
redright_hist[ichan].setData(x=[redfreq+redwidth, redfreq+redwidth], y=[specmin_hist[ichan], specmax_hist[ichan]])
blueleft_hist[ichan].setData(x=[bluefreq-bluewidth, bluefreq-bluewidth], y=[specmin_hist[ichan], specmax_hist[ichan]])
blueright_hist[ichan].setData(x=[bluefreq+bluewidth, bluefreq+bluewidth], y=[specmin_hist[ichan], specmax_hist[ichan]])
# update labels at plotted lines
text_redleft.setText('%0.1f' % (redfreq-redwidth))
text_redleft.setPos(redfreq-redwidth, specmax[0])
text_redright.setText('%0.1f' % (redfreq+redwidth))
text_redright.setPos(redfreq+redwidth, specmax[0])
text_blueleft.setText('%0.1f' % (bluefreq-bluewidth))
text_blueleft.setPos(bluefreq-bluewidth, specmax[0])
text_blueright.setText('%0.1f' % (bluefreq+bluewidth))
text_blueright.setPos(bluefreq+bluewidth, specmax[0])
text_redleft_hist.setText('%0.1f' % (redfreq-redwidth))
text_redleft_hist.setPos(redfreq-redwidth, specmax_hist[0])
text_redright_hist.setText('%0.1f' % (redfreq+redwidth))
text_redright_hist.setPos(redfreq+redwidth, specmax_hist[0])
text_blueleft_hist.setText('%0.1f' % (bluefreq-bluewidth))
text_blueleft_hist.setPos(bluefreq-bluewidth, specmax_hist[0])
text_blueright_hist.setText('%0.1f' % (bluefreq+bluewidth))
text_blueright_hist.setPos(bluefreq+bluewidth, specmax_hist[0])
key = "%s.%s.%s" % (patch.getstring('output', 'prefix'), 'redband', 'low')
r.set(key, redfreq-redwidth)
key = "%s.%s.%s" % (patch.getstring('output', 'prefix'), 'redband', 'high')
r.set(key, redfreq+redwidth)
key = "%s.%s.%s" % (patch.getstring('output', 'prefix'), 'blueband', 'low')
r.set(key, bluefreq-bluewidth)
key = "%s.%s.%s" % (patch.getstring('output', 'prefix'), 'blueband', 'high')
r.set(key, bluefreq+bluewidth)
# Set timer for update
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.setInterval(10) # timeout in milliseconds
timer.start(int(round(stepsize*1000))) # in milliseconds
# Wait until there is enough data
begsample = -1
while begsample<0:
hdr_input = ft_input.getHeader()
begsample = int(hdr_input.nSamples - window)
# Start
QtGui.QApplication.instance().exec_()