forked from BarkleyUS/mindwave-python
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathOfflineRelaxAndMoveMindwave.py
206 lines (146 loc) · 5.12 KB
/
OfflineRelaxAndMoveMindwave.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
#coding: latin-1
#
# Run me with frameworkpython inside a virtual environment.
# Or install the environment.yml for Anaconda.
# This program uses Mindawave object to connect using bluetooth to Mindwave
# and get the raw eeg signals from there.
#
# It also plot the signal using matplotlib.
#
# Fs = 128
import socket,select
import json
import time, datetime, sys
import matplotlib.pyplot as plt
import sys
import pyaudio
volume = 0.5 # range [0.0, 1.0]
fs = 44100 # sampling rate, Hz, must be integer
duration = 0.01 # in seconds, may be float
fwave = 440.0 # sine frequency, Hz, may be float
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=fs,
output=True)
lamdalength = 10
Fs=128
show=False
# Please provide the number of sample points to take
if (len(sys.argv) > 1):
samplepoints = int(sys.argv[1])
else:
samplepoints = Fs*lamdalength
# def windowing(window, N):
# if len(window)>=N:
# if not False:
# awindow = np.asarray( window )
# fullsignal = fullsignal + window
# afullsignal = np.asarray( fullsignal )
# if (len(fullsignal) > 0):
# awindow = awindow - afullsignal.mean(0)
# o1 = psd(awindow[:,0])
# o2 = psd(awindow[:,1])
# print (o1, o2)
# features.append( [o1, o2] )
# # Slide window
# window = window[N/2:N]
# #window = window[1:N]
print ('Please remove the VGA connection that sometimes interfere with Mindwave')
import numpy as np
import mindwave, time
from pydub import AudioSegment
from pydub.playback import play
song = AudioSegment.from_wav("BoatHonk.wav")
explosion = AudioSegment.from_wav("explosion.wav")
import matplotlib.image as mpimg
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '../python-sumo/')
from interface import SumoController
from Plotter import Plotter
#headset = mindwave.Headset('/dev/tty.MindWaveMobile-DevA','ef47')
headset = mindwave.OfflineHeadset()
time.sleep(2)
plotter = Plotter(500,-500,500)
attention = 0
meditation = 0
eeg = 0
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d-%H-%M-%S')
filename = './data/eeg.'+st+'.dat'
f = open(filename, 'w')
window = []
N = 128
signaleeg = []
mistakes = 0
plt.title("Entrenamiento...")
try:
while (headset.poor_signal > 5):
print ("Headset signal noisy %d. Adjust the headset and the earclip." % (headset.poor_signal))
print ("Writing %d seconds output to %s" % (lamdalength,filename))
for i in range(0,samplepoints):
#time.sleep(.01)
headset.dequeue()
(count,eeg, attention, meditation, blink) = (headset.count, headset.raw_value, headset.attention, headset.meditation, headset.blink)
window.append( int(eeg) )
signaleeg.append( int (eeg) )
if len(window) > N:
window = window[N/2:N]
awindow = np.asarray(window)
#blink = np.sum(np.abs(awindow[0:N/2]))
plotter.plotdata( [eeg, 0, 0, blink])
#plotter.plotdata( [eeg, 0, 0])
samples = (np.sin(2*np.pi*np.arange(fs*duration)*(fwave+int(eeg))/fs)).astype(np.float32)
stream.write(volume*samples)
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d-%H-%M-%S.%f')
f.write( str(ts) + ' ' + str(count) + ' ' + str(eeg) + ' ' + str(attention) + ' ' + str(meditation) + ' ' + str(blink) + '\n')
finally:
pass
asignaleeg = np.asarray( signaleeg )
meanval = np.mean( signaleeg )
stdval = np.std( signaleeg )
print(meanval)
print(stdval)
headset.close()
headset.setupfile()
print("Ready to continue...")
sys.stdin.read(1)
plt.title("A Relajarse !!!!")
try:
while (headset.poor_signal > 5):
print ("Headset signal noisy %d. Adjust the headset and the earclip." % (headset.poor_signal))
print ("Writing %d seconds output to %s" % (lamdalength,filename))
for i in range(0,samplepoints):
#time.sleep(.01)
headset.dequeue()
(count,eeg, attention, meditation, blink) = (headset.count, headset.raw_value, headset.attention, headset.meditation, headset.blink)
window.append( int(eeg) )
signaleeg.append( int (eeg) )
if len(window) > N:
window = window[N/2:N]
awindow = np.asarray(window)
#blink = np.sum(np.abs(awindow[0:N/2]))
threshold = meanval+stdval
plotter.plotdata( [eeg, threshold, 0, blink])
#plotter.plotdata( [eeg, 0, 0])
if (int(eeg)>int(threshold)):
mistakes = mistakes + 1
play(song)
if (mistakes>3):
play(explosion)
print("Press a key. Endgame")
sys.stdin.read(1)
quit()
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d-%H-%M-%S.%f')
f.write( str(ts) + ' ' + str(count) + ' ' + str(eeg) + ' ' + str(attention) + ' ' + str(meditation) + ' ' + str(blink) + '\n')
finally:
headset.stop()
f.close()
if (False):
controller = SumoController()
controller.move(100, duration=0.5)
controller.store_pic()
sys.stdin.read(1)