-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsketch_171031a.pde
461 lines (403 loc) · 14.5 KB
/
sketch_171031a.pde
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* Reading and Visualizing EEG Data
* edited by TAPAN PANDE
* Reads in EEG data through the microphone input of the
* computer, then displays the signal in time, its frequency
* components, and then averages of frequencies that estimate
* the concentration of different brain waves.
*
* For reference, the frequency bars are ordered/classified as
* follows:
*
* 1 - blue -------- delta
* 2 - blue/purple - theta
* 3 - purple ------ alpha
* 4 - purple/red -- low beta
* 5 - dark red ---- mid beta
* 6 - red --------- high beta
*
* This sketch will measure all brain waves, from 0 - 30 Hz. It does
* best, however, measuring alpha waves across the occipital lobe.
* To view this function, play the program, click the window to
* make sure its in "focus", and hit the "a" key to bandpass the alpha
* waves only. The alpha wave bar is the 3rd one (purple), and should
* increase in height by 2-3x when you close your eyes and relax
* (you'll see it for a second or two after you open your eyes, before it
* averages back down).
* /
/* One issue: when taking the FFT of the data, it seems as if
the frequency bands have a bandwidth of 1.33 instead of 1, as
60Hz noise peaks out at band 45. This is worked around by using
the scaleFreq parameter, which is used frequently. */
import processing.serial.*;
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import javax.sound.sampled.*;
//Important constants that may need to be changed.
float timeScale = 50; //scales the amplitude of time-domain data, can be changed
static float normalScale = 50;
static float alphaScale = 100;
static int freqAvgScale = 50; //does same for averages of frequency data
static int alphaCenter = 12;
static int alphaBandwidth = 2; //really bandwidth divided by 2
static int betaCenter = 24;
static int betaBandwidth = 2;
//Variables used to store data functions/effects.
Minim minim;
Serial myPort;
final int timeLength = 240;
float[] timeSignal = new float[timeLength];
FFT fft;
NotchFilter notch;
LowPassSP lpSP;
LowPassFS lpFS;
BandPass betaFilter;
BandPass alphaFilter;
//Constants mainly used for scaling the data to readable sizes.
int windowLength = 840;
int windowHeight = 500;
int FFTheight;
float scaling[] = {.00202,.002449/2,.0075502/2,.00589,.008864,.01777};
int FFTrectWidth = 6;
float scaleFreq = 1.33f;
float timeDomainAverage = 0;
//Variables used to handle bad data
int cutoffHeight = 200; //frequency height to throw out "bad data" for averaging after
float absoluteCutoff = 1.5;
boolean absoluteBadDataFlag; //data that is bad because it's way too far out of our desired range --
// ex: shaking your head for a second
boolean averageBadDataFlag; //data that's bad because it spikes too far outside of the average for
//that second --
// ex: blinking your eyes for a split second
//Constants used to create a running average of the data.
float[][] averages;
int averageLength = 200; //averages about the last 5 seconds worth of data
int averageBins = 6; //we have 6 types of brain waves
int counter = 0;
// There are four ways of reading the data into the system:
final int INPUT_FROM_RAW_AUDIO = 0;
final int INPUT_FROM_BUFFERED_AUDIO = 1;
final int INPUT_FROM_SERIAL_PORT = 2;
final int INPUT_FROM_TEST_DATA = 3;
int readDataFrom = INPUT_FROM_BUFFERED_AUDIO;
boolean debugTraces = false; // swtich off the 'averageBadData flags', which slows things down a lot
final int NUM_CHANNELS = 1;
AudioInput in;
void setup()
{background(200);
// For serial input, you need an Arduino to feed the data back to processing.
if (readDataFrom == INPUT_FROM_SERIAL_PORT) {
int serialIndex = 0;
myPort = new Serial(this, Serial.list()[serialIndex], 9600);
}
//initialize array of averages for running average calculation
averages = new float[averageBins][averageLength];
for (int i = 0; i < averageBins; i++){
for (int j = 0; j < averageLength; j++){
averages[i][j] = 0;
}
}
//set some drawing parameters
windowLength = 840;
windowHeight = 500;
FFTheight = windowHeight - 200;
//initialize minim, as well as some filters
minim = new Minim(this);
if (debugTraces) {
minim.debugOn();
}
notch = new NotchFilter(60, 10, 32768);
lpSP = new LowPassSP(40, 32768);
lpFS = new LowPassFS(60, 32768);
betaFilter = new BandPass(betaCenter/scaleFreq,betaBandwidth/scaleFreq,32768);
alphaFilter = new BandPass(alphaCenter/scaleFreq,alphaBandwidth/scaleFreq,32768);
// initialize values in array that will be used for input
for (int i = 0; i < timeLength; i++){
timeSignal[i] = 0;
}
//initialize FFT
fft = new FFT(256, 256);
fft.window(FFT.HAMMING);
//println(CORNERS);
rectMode(CORNERS);
println(fft.getBandWidth());
Mixer.Info[] mixerInfo;
mixerInfo = AudioSystem.getMixerInfo();
println("Available mixers:");
for(int i = 0; i < mixerInfo.length; i++) {
println(i +" : " + mixerInfo[i].getName());
}
// We could introduce a P5 UI to select the mixer here
int mixerID = 3;
Mixer mixer = AudioSystem.getMixer(mixerInfo[mixerID]);
minim.setInputMixer(mixer);
in = minim.getLineIn(Minim.MONO, timeLength);
if (in == null) {
exit();
}
}
void settings() {
size(1000, 800);
}
void draw()
{
switch(readDataFrom) {
case INPUT_FROM_BUFFERED_AUDIO:
fillTimeSignalWithAudioData();
break;
case INPUT_FROM_TEST_DATA:
fillTimeSignalWithTestData();
break;
}
/*badDataFlag handles any "artifacts" we may pick up while recording the data.
Artifacts are essentially imperfections in the data recording -- they can come
from muscle movements, blinking, anything that disturbs the electrodes. If the
program encounters a set of data that spikes out of a reasonable window
(controlled by the variable cutoffHeight), it won't consider that data
when computing the running average.
*/
absoluteBadDataFlag = false;
averageBadDataFlag = false;
background(0); //make sure the background color is black
stroke(255); //and that time data is drawn in white
line(0,100,windowLength,100); //line separating time and frequency data
drawSignalData();
//check for spikes relative to other data
for (int i = 0; i < windowLength - 1; i++){
if (abs(getData(i+1, windowLength)) > timeDomainAverage*4)
averageBadDataFlag = true;
}
displayText();
displayFreqAverages();
counter++;
}
void fillTimeSignalWithAudioData() {
for(int i=0;i<timeLength;++i) {
int waveOffset = round((i * in.bufferSize()) / timeLength);
timeSignal[i] = in.left.get(waveOffset);
}
}
void fillTimeSignalWithTestData() {
shiftNtimes(timeSignal, 1);
timeSignal[timeLength - 1] = (counter & 0xff) / 255.0;
}
float getData(int index, int ofTotal) {
int remappedIndex;
switch(readDataFrom) {
case INPUT_FROM_RAW_AUDIO:
remappedIndex = round((index * in.bufferSize())/ofTotal);
return in.left.get(remappedIndex);
case INPUT_FROM_BUFFERED_AUDIO:
case INPUT_FROM_TEST_DATA:
case INPUT_FROM_SERIAL_PORT:
remappedIndex = round((index * timeLength)/ofTotal);
return timeSignal[remappedIndex];
}
return 0;
}
void keyPressed(){
if (key == 'w'){
fft.window(FFT.HAMMING);
}
if (key == 'e'){
fft.window(FFT.NONE);
}
}
void serialEvent(Serial p){
// As above, this does not appear to get used, but I've the code anyway
// in case you're wondering what it should be.
while (p.available() > 0){
if (readDataFrom == INPUT_FROM_SERIAL_PORT) {
shiftNtimes(timeSignal, 1);
timeSignal[timeLength-1] = p.read()/255.0;
}
}
}
//Shifts all elements in myArray numShifts times left, resulting in the
//[0-numShift] elements being pushed off, and the last numShift elements
//becoming zero. Does this for all data channels.
public void shiftNtimes(float[] myArray, int numShifts){
int timesShifted = 0;
while (timesShifted < numShifts){
for (int i = 0; i < NUM_CHANNELS; i++){
for (int j = 0; j < timeLength - 1; j++){
myArray[j] = myArray[j + 1];
}
myArray[(int)timeLength - 1] = 0;
timesShifted++;
}
}
}
//Draw the signal in time and frequency.
void drawSignalData(){
timeDomainAverage = 0;
for(int i = 0; i < windowLength - 1; i++)
{
float dataValue = getData(i, windowLength);
float nextValue = getData(i+1, windowLength);
stroke(255,255,255);
//data that fills our window is normalized to +-1, so we want to throw out
//sets that have data that exceed this by the factor absoluteCutoff
if (abs(dataValue) * timeScale/normalScale > .95){
//if (abs(in.left.get(i*round(in.bufferSize()/windowLength)))*timeScale/normalScale > .95){
absoluteBadDataFlag = true;
fill(250,250,250);
stroke(150,150,150);
}
//Draw the time domain signal.
line(i, 50 + dataValue*timeScale,
i+1, 50 + nextValue*timeScale);
timeDomainAverage += abs(dataValue);
//Draw un-averaged frequency bands of signal.
if (i < (windowLength - 1)/2){
//set colors for each type of brain wave
if (i <= round(3/scaleFreq)){
fill(0,0,250); //delta
stroke(25,0,225);
}
if (i >= round(4/scaleFreq) && i <= round((alphaCenter - alphaBandwidth)/scaleFreq)-1){
fill(50,0,200); //theta
stroke(75,0,175);
}
if (i >= round((alphaCenter - alphaBandwidth)/scaleFreq) &&
i <= round((alphaCenter + alphaBandwidth)/scaleFreq)){
fill(100,0,150); //alpha
stroke(125,0,125);
}
if (i >= round((alphaCenter + alphaBandwidth)/scaleFreq)+1 &&
i <= round((betaCenter-betaBandwidth)/scaleFreq)-1){
fill(150,0,100); //low beta
stroke(175,0,75);
}
if (i >= round((betaCenter - betaBandwidth)/scaleFreq) &&
i <= round((betaCenter + betaBandwidth)/scaleFreq)){
fill(200,0,50); //midrange beta
stroke(225,0,25);
}
if (i >= round((betaCenter + betaBandwidth)/scaleFreq)+1 && i <= round(30/scaleFreq)){
fill(250,0,0); //high beta
stroke(255,0,10);
}
if (i >= round(32/scaleFreq)){
fill(240,240,240); //rest of stuff, mainly noise
stroke(200,200,200);
}
if (i == round(60/scaleFreq)){
fill(200,200,200); //color 60 Hz a different tone of grey,
stroke(150,150,150); //to see how much noise is in data
}
//draw the actual frequency bars
rect(FFTrectWidth*i, FFTheight, FFTrectWidth*(i+1), FFTheight - fft.getBand(i)/10);
}
}
//divide the average by how many time points we have
timeDomainAverage = timeDomainAverage / (windowLength - 1);
}
//Give user textual information on data being thrown out and filters we have active.
void displayText(){
//show user when data is being thrown out
text("absoluteBadDataFlag = " + absoluteBadDataFlag, windowLength - 200, 120);
if (absoluteBadDataFlag == true && debugTraces)
{
println("absoluteBadDataFlag = " + absoluteBadDataFlag);
println(counter);
}
text("averageBadDataFlag = " + averageBadDataFlag, windowLength - 200, 140);
if (averageBadDataFlag == true && debugTraces)
{
println("averageBadDataFlag = " + averageBadDataFlag);
println(counter);
}
//and when a filter is being applied to the data
text("alpha filter is " + in.hasEffect(alphaFilter),
windowLength - 200, 160);
text("beta filter is " + in.hasEffect(betaFilter),
windowLength - 200, 180);
}
//Compute and display averages for each brain wave for the past ~5 seconds.
void displayFreqAverages(){
//show averages of alpha, beta, etc. waves
for (int i = 0; i < 6; i++){
float avg = 0; //raw data for amplitude of section of frequency
int lowFreq = 0;
int hiFreq = 0;
//Set custom frequency ranges to be averaged.
if(i == 0){
lowFreq = 0;
hiFreq = 3;
fill(0,0,250);
stroke(25,0,225);
}
if(i == 1){
lowFreq = 3;
hiFreq = 7;
fill(50,0,200);
stroke(75,0,175);
}
if(i == 2){
lowFreq = alphaCenter - alphaBandwidth;
hiFreq = alphaCenter + alphaBandwidth;
fill(100,0,150);
stroke(125,0,125);
}
if(i == 3){
lowFreq = 12;
hiFreq = 15;
fill(150,0,100);
stroke(175,0,75);
}
if(i == 4){
lowFreq = betaCenter - betaBandwidth;
hiFreq = betaCenter + betaBandwidth;
fill(200,0,50);
stroke(225,0,25);
}
if(i == 5){
lowFreq = 20;
hiFreq = 30;
fill(250,0,0);
stroke(255,0,10);
}
//Convert frequencies we want to the actual FFT bands. Because of our
//FFT parameters, these happen to be equal (each band has a 1 Hz width).
int lowBound = fft.freqToIndex(lowFreq);
int hiBound = fft.freqToIndex(hiFreq);
//Scale the band number, because of the issue outlined at very beginning of
//program.
lowBound = round(lowBound/scaleFreq);
hiBound = round(hiBound/scaleFreq);
//get average for frequencies in range
for (int j = lowBound; j <= hiBound; j++){
avg += fft.getBand(j);
}
avg /= (hiBound - lowBound + 1);
// Scale the bars so that it fits our window a little better.
for (int k = 0; k < 6; k++)
{
if (i == k)
{
avg *= scaling[i]*freqAvgScale;
}
}
//update our array for the moving average (only if our data is "good")
if (absoluteBadDataFlag == false && averageBadDataFlag == false){
averages[i][counter%averageLength] = avg;
}
//calculate the running average for each frequency range
float sum = 0;
for (int k = 0; k < averageLength; k++){
sum += averages[i][k];
}
sum = sum / averageLength;
//draw averaged/smoothed frequency ranges
rect(i*width/6, height, (i+1)*width/6, height - sum);
}
}
// always close Minim audio classes when you are done with them
void stop()
{
in.close();
minim.stop();
super.stop();
}