-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientBIT.html
782 lines (711 loc) · 36.3 KB
/
ClientBIT.html
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
<html>
<style>
body{
height:100%;
font: 12px sans-serif;
}
.line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
}
svg{
background-color: #F1F3F3;
}
.hover-line {
stroke: #DC143C;
stroke-width: 2px;
stroke-dasharray: 3,3;
}
.focus circle {
fill: #F1F3F3;
stroke: #DC143C;
stroke-width: 3px;
}
</style>
<script language="javascript" type="text/javascript" src="d3/d3.js"></script>
<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var sample_rate = 1000,
fft_res=501,
record_dur=-1,
data_RAW1 = Array.apply(null, new Array(sample_rate)).map(Number.prototype.valueOf, 512),
data_RAW2 = Array.apply(null, new Array(sample_rate)).map(Number.prototype.valueOf, 512),
data_EEG1 = Array.apply(null, new Array(fft_res)).map(Number.prototype.valueOf, 1),
data_EEG2 = Array.apply(null, new Array(fft_res)).map(Number.prototype.valueOf, 1),
data_PPG = Array.apply(null, new Array(sample_rate)).map(Number.prototype.valueOf, 512),
data_HR = [],
data_VALNC=[],
data_FOCUS=[],
data_MEDI=[],
line_RAW,
line_EEG,
line_VALNC,
line_FOCUS,
line_MEDI,
x_RAW,
y_RAW,
y_EEG,
chart_RAW1, //RAW_L
chart_RAW2, //RAW_R
chart_EEG1, //EEG_L
chart_EEG2, //EEG_R
chart_PPG, //PPG
chart_HR, //HR
chart_VALNC, //VALENCE
chart_FOCUS, //FOCUS
chart_MEDI, //MEDITATION
queue=[],// contains all peaks corresponding time (ORDERED, in ms) in the PPG's previous 10seconds
hr=60 //current hr;
var hr_max=0,
hr_min=0,
emo_valnc_max=0, //NOTA, AO ATRIBUIR VALOR ZERO ESTOU A ASSUMIR QUE EXISTIRAO VALORES POSITIVOS & NEGATIVOS!!!!
emo_valnc_min=0,
engagement_max=0,
engagement_min=0,
meditation_max=0,
meditation_min=0;
var focus_EEG1, // define each line chart's highlighting variable
focus_EEG2,
focus_HR,
focus_VALNC,
focus_FOCUS,
focus_MEDI;
//------------------------------------------------------------
function getPosition(string, subString, index) {
return string.split(subString, index).join(subString).length;
}
// Establish a connection to the ServerBIT
var ws = new WebSocket("ws://localhost:9005");
// Define the boolean data type as used in Python
var True = true;
var False = false;
ws.onopen = function () {
};
// Process the responses sent by the ServertBIT
ws.onmessage = function (e) {
msg = e.data.toString();
// Log the response onto the HTML body
if (msg.indexOf("read") < 0) {
$("body").html($("body").html() + msg + "<br/>")
}
// Evaluate the respose
//var unprocessed_data = e.data.substring(0,getPosition(e.data,'[',8));
eval(e.data)
};
// Detect when the page is unloaded or close
window.onbeforeunload = function () {
// Request ServerBIT to close the connection to BITalino
ws.send("device.close()");
// Request ServerBIT to shut down
ws.send("server.shutdown()");
ws.onclose = function () {
};
ws.close()
};
// Process the server messages related with the server
server = new function () {
this.connected = function (msg) {
// When a connection to ServerBIT is established, open the connection to the device
ws.send("server.BITalino('20:15:12:22:81:23')")
};
this.BITalino = function (msg) {
if (msg) {
// When a connection to the device is established start the acquisition
ws.send("device.start(1000, [0,1,2,3,4,5])")
}
}
};
// Process the server messages related with the <device></device>
device = new function () {
var auxv = 0;
this.start = function (msg) {
// When the device starts the acquisition read samples
ws.send("device.read(1000)[:,-6:]")
};
this.read = function (msg) {
//--------------------- Message properly recieved ----------------------------------------------------------------------
// When a set of samples is read request more samples
ws.send("device.read(1000)[:,-6:]");
var dimensions = [ msg.length, msg[0].length ];
var in0 = msg[0]; //RAW_LEFT
var in1 = msg[1]; //RAW_RIGHT
var in2 = msg[2]; //PPG
var in3 = msg[3]; //AAC_X
var in4 = msg[4]; //AAC_Y
var in5 = msg[5]; //AAC_Z
var in6 = msg[6]; //EEG_LEFT
var in7 = msg[7]; //EEG_RIGHT
data_EEG1=msg[6];
data_EEG2=msg[7];
//determine left theta wave spike
var left_theta_spike=Math.max(in6[4],in6[5],in6[6],in6[7]); //4Hz,5Hz,6Hz,7Hz
//determine right theta wave spike
var right_theta_spike=Math.max(in7[4],in7[5],in7[6],in7[7]); //4Hz,5Hz,6Hz,7Hz
//determine left theta wave spike //8Hz,9Hz,10Hz,11Hz,12Hz,13Hz
var left_alpha_spike=Math.max(in6[8],in6[9],in6[10],in6[11],in6[12],in6[13]);
//determine right theta wave spike //8Hz,9Hz,10Hz,11Hz,12Hz,13Hz
var right_alpha_spike=Math.max(in7[8],in7[9],in7[10],in7[11],in6[12],in6[13]);
//determine left theta wave spike
var left_beta_spike=Math.max(in6[17],in6[18],in6[19],in6[20]);//17Hz,18Hz,19Hz,20Hz
//determine right theta wave spike
var right_beta_spike=Math.max(in7[17],in7[18],in7[19],in7[20]);//17Hz,18Hz,19Hz,20Hz
var avg_theta_freq=0.5*left_theta_spike+0.5*right_theta_spike;
var avg_alpha_freq=0.5*left_alpha_spike+0.5*right_alpha_spike;
var avg_beta_freq=0.5*left_beta_spike+0.5*right_beta_spike;
var valence =right_alpha_spike-left_alpha_spike; //emotional valence formula
var engagement = (avg_beta_freq/(avg_alpha_freq+avg_theta_freq)).toFixed(4); //engagement formula
var meditation = 0.5*(avg_theta_freq+avg_alpha_freq); //meditation formula
//determine max and min; used for linear scaling of domain
if(emo_valnc_max<valence){emo_valnc_max=valence;}
if(emo_valnc_min>valence){ emo_valnc_min=valence;}
if(engagement_max<engagement){ engagement_max=engagement; }
if(engagement_min>engagement){ engagement_min=engagement; }
if(meditation_max<meditation){ meditation_max=meditation; }
if(meditation_min>meditation){ meditation_min=meditation; }
data_VALNC.push(valence); //store emotional valence in arraylist's corresponding second of the recording session
data_FOCUS.push(engagement); //store engagement metric in arraylist's corresponding second of the recording session
data_MEDI.push(meditation); //store meditation metric in arraylist's corresponding second of the recording session
record_dur+=1;
//--------------------- create rt infovis
hr=estimateHR();
var svg0 = d3.select("svg#RAW_LEFT");
var svg1 = d3.select("svg#RAW_RIGHT");
var svg_EEG2 = d3.select("svg#EEG_LEFT");
var svg_EEG1 = d3.select("svg#EEG_RIGHT");
var svg4 = d3.select("svg#PPG");
var svg_HR = d3.select("svg#HR");
var svg_VALNC = d3.select("svg#VALNC");
var svg_FOCUS = d3.select("svg#FOCUS");
var svg_MEDI = d3.select("svg#MEDI");
// if previous plots already exists, remove them
if (!svg0.select("g").empty()) { svg0.select("g").remove(); svg0.selectAll("text").remove(); }
if (!svg1.select("g").empty()) { svg1.select("g").remove(); svg1.selectAll("text").remove();}
if (!svg_EEG2.select("g").empty()) { svg_EEG2.select("g").remove(); svg_EEG2.selectAll("text").remove(); }
if (!svg_EEG1.select("g").empty()) { svg_EEG1.select("g").remove(); svg_EEG1.selectAll("text").remove(); }
if (!svg4.select("g").empty()) { svg4.select("g").remove(); svg4.selectAll("text").remove(); }
if (!svg_HR.select("g").empty()) { svg_HR.select("g").remove(); svg_HR.selectAll("text").remove(); }
if (!svg_VALNC.select("g").empty()) { svg_VALNC.select("g").remove(); svg_VALNC.selectAll("text").remove(); }
if (!svg_FOCUS.select("g").empty()) { svg_FOCUS.select("g").remove(); svg_FOCUS.selectAll("text").remove(); }
if (!svg_MEDI.select("g").empty()) { svg_MEDI.select("g").remove(); svg_MEDI.selectAll("text").remove(); }
var margin = {top: 20, right: 20, bottom: 20, left: 40},
width = +svg0.attr("width"),
height = +svg0.attr("height") - margin.top - margin.bottom;
svg0.append("text").attr("x", "50%").attr("y", "15%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("FP2 RAW");
svg1.append("text").attr("x", "50%").attr("y","15%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("FP1 RAW");
svg_EEG2.append("text").attr("x", "50%").attr("y", "15%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("FP2 Power Spectrum");
svg_EEG1.append("text").attr("x", "50%").attr("y","15%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("FP1 Power Spectrum");
svg4.append("text").attr("x", "50%").attr("y", "15%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("Photoplethysmogram");
svg4.append("text").attr("x", "50%").attr("y", "22%").attr("text-anchor", "middle").attr("id","bpm_display")
.style("font-size", "16px").text(hr.toString()+" bpm");
svg_HR.append("text").attr("x", "50%").attr("y", "15%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("Heart Rate");
svg_VALNC.append("text").attr("x", "50%").attr("y", "15%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("Emotional Valence");
svg_FOCUS.append("text").attr("x", "50%").attr("y", "15%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("Engagement");
svg_MEDI.append("text").attr("x", "50%").attr("y", "15%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("Meditation");
chart_RAW1 = svg0.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart_RAW2 = svg1.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart_EEG1 = svg_EEG1.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart_EEG2 = svg_EEG2.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart_PPG = svg4.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart_HR = svg_HR.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart_VALNC = svg_VALNC.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart_FOCUS = svg_FOCUS.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart_MEDI = svg_MEDI.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
console.log(record_dur);
x_RAW = d3.scaleLinear().domain([1, sample_rate-1]).range([0, width-margin.left*1.2]); //used for PPG & RAW INPUT
x_EEG = d3.scaleLinear().domain([0, fft_res - 400 - 1]).range([0, width-margin.left*1.2]); //used for EEGs
x_amass = d3.scaleLinear().domain([0, record_dur]).range([0, width-margin.left*1.2]); //used for cummulative plots
y_RAW = d3.scaleLinear().domain([1024, 0]).range([0, height-margin.top]);
y_EEG = d3.scaleLog().domain([10000000, 1]).range([0, height-margin.top]);
y_HR = d3.scaleLinear().domain([hr_max, hr_min]).range([0, height-margin.top]); //used for HR cummulative plot
y_VALNC = d3.scaleLinear().domain([emo_valnc_max, emo_valnc_min]).range([0, height-margin.top]); //used for VALENCE
y_FOCUS = d3.scaleLinear().domain([engagement_max, engagement_min]).range([0, height-margin.top]); //used for ENGAGEMENT
y_MEDI = d3.scaleLinear().domain([meditation_max, meditation_min]).range([0, height-margin.top]); //used for MEDITATION
line_RAW = d3.line() //used for PPG & RAW INPUT
.x(function (d, i) { return x_RAW(i);})
.y(function (d, i) { return y_RAW(d);});
line_EEG = d3.line()
.x(function (d, i) { return x_EEG(i); })
.y(function (d, i) { return y_EEG(d); });
line_HR = d3.line()
.x(function(d, i){ return x_amass(i); })
.y(function(d, i){ return y_HR(d); });
line_VALNC = d3.line()
.x(function(d, i){ return x_amass(i); })
.y(function(d, i){ return y_VALNC(d); });
line_FOCUS = d3.line()
.x(function(d, i){ return x_amass(i); })
.y(function(d, i){ return y_FOCUS(d); });
line_MEDI = d3.line()
.x(function(d, i){ return x_amass(i); })
.y(function(d, i){ return y_MEDI(d); });
chart_RAW1.append("defs")
.append("clipPath")
.attr("id", "clip0") //attr must differ
.append("rect")
.attr("width", width)
.attr("height", height);
chart_RAW1.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y_RAW(0) + ")")
.call(d3.axisBottom(x_RAW));
chart_RAW1.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_RAW));
chart_RAW2.append("defs").append("clipPath")
.attr("id", "clip1")
.append("rect")
.attr("width", width)
.attr("height", height);
chart_RAW2.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y_RAW(0) + ")")
.call(d3.axisBottom(x_RAW));
chart_RAW2.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_RAW));
chart_EEG1.append("defs").append("clipPath")
.attr("id", "clip2")
.append("rect")
.attr("width", width)
.attr("height", height);
chart_EEG1.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y_EEG(1) + ")")
.call(d3.axisBottom(x_EEG));
chart_EEG1.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_EEG));
chart_EEG2.append("defs").append("clipPath")
.attr("id", "clip2")
.append("rect")
.attr("width", width)
.attr("height", height);
chart_EEG2.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y_EEG(1) + ")")
.call(d3.axisBottom(x_EEG));
chart_EEG2.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_EEG));
chart_PPG.append("defs").append("clipPath")
.attr("id", "clip4")
.append("rect")
.attr("width", width)
.attr("height", height);
chart_PPG.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y_RAW(0) + ")")
.call(d3.axisBottom(x_RAW));
chart_PPG.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_RAW));
chart_HR.append("defs").append("clipPath")
.attr("id", "clip5")
.append("rect")
.attr("width", width)
.attr("height", height);
chart_HR.append("g")
.attr("class", "axis axis--x_amass")
.attr("transform", "translate(0," + y_HR(0) + ")")
.call(d3.axisBottom(x_amass).ticks(Math.ceil(record_dur/10.0)+1));
chart_HR.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_HR));
chart_HR.append("g")
.append("path")
.attr("id","path_5")
.attr("class", "line")
.attr("d",line_HR(data_HR));
chart_VALNC.append("defs").append("clipPath")
.attr("id", "clip6")
.append("rect")
.attr("width", width)
.attr("height", height);
chart_VALNC.append("g")
.attr("class", "axis axis--x_amass")
.attr("transform", "translate(0," + y_VALNC(0) + ")")
.call(d3.axisBottom(x_amass).ticks(Math.ceil(record_dur/10.0)+1));
chart_VALNC.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_VALNC));
chart_VALNC.append("g")
.append("path")
.attr("id","path_6")
.attr("class", "line")
.attr("d",line_VALNC(data_VALNC));
chart_FOCUS.append("defs").append("clipPath")
.attr("id", "clip7")
.append("rect")
.attr("width", width)
.attr("height", height);
chart_FOCUS.append("g")
.attr("class", "axis axis--x_amass")
.attr("transform", "translate(0," + y_FOCUS(0) + ")")
.call(d3.axisBottom(x_amass).ticks(Math.ceil(record_dur/10.0)+1));
chart_FOCUS.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_FOCUS));
chart_FOCUS.append("g")
.append("path")
.attr("id","path_7")
.attr("class", "line")
.attr("d",line_FOCUS(data_FOCUS));
chart_MEDI.append("defs").append("clipPath")
.attr("id", "clip8")
.append("rect")
.attr("width", width)
.attr("height", height);
chart_MEDI.append("g")
.attr("class", "axis axis--x_amass")
.attr("transform", "translate(0," + y_MEDI(0) + ")")
.call(d3.axisBottom(x_amass).ticks(Math.ceil(record_dur/10.0)+1));
chart_MEDI.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_MEDI));
chart_MEDI.append("g")
.append("path")
.attr("id","path_8")
.attr("class", "line")
.attr("d",line_MEDI(data_MEDI));
drawHighlight();
chart_RAW1.append("g")
.attr("clip-path", "url(#clip0)")
.append("path")
.datum(data_RAW1)
.attr("class", "line")
.transition()
.duration(50)
.ease(d3.easeLinear)
.on("start", tick0);
chart_RAW2.append("g")
.attr("clip-path", "url(#clip1)")
.append("path")
.datum(data_RAW2)
.attr("class", "line")
.transition()
.duration(50)
.ease(d3.easeLinear)
.on("start", tick1);
chart_EEG1.append("g")
.attr("clip-path", "url(#clip2)")
.append("path")
.datum(data_EEG1)
.attr("class", "line")
.attr("d", line_EEG);
chart_EEG2.append("g")
.attr("clip-path", "url(#clip3)")
.append("path")
.datum(data_EEG2)
.attr("class", "line")
.attr("d", line_EEG);
chart_PPG.append("g")
.attr("clip-path", "url(#clip4)")
.append("path")
.datum(data_PPG)
.attr("class", "line")
.transition()
.duration(50)
.ease(d3.easeLinear)
.on("start", tick4);
d3.select("#path_5")
.attr("d",line_HR(data_HR));
d3.select("#path_6")
.attr("d",line_VALNC(data_VALNC));
d3.select("#path_7")
.attr("d",line_FOCUS(data_FOCUS));
d3.select("#path_8")
.attr("d",line_MEDI(data_MEDI));
function tick0() {
if (in0.length > 49) { // Push a new data point onto the back.
for (var i = 0; i < 50; i++) {
data_RAW1.push(in0.shift());
}
d3.select(this) // Redraw the line.
.attr("d", line_RAW)
.attr("transform", null);
// Slide it to the left.
d3.active(this)
.attr("transform", "translate(" + x_RAW(-1) + ",0)")
.transition()
.on("start", tick0);
for (var i = 0; i < 50; i++) {
data_RAW1.shift(); // Pop the old data point off the front.
}
}
}
function tick1() {
if (in1.length > 49) { // Push a new data point onto the back.
for (var i = 0; i < 50; i++) {
data_RAW2.push(in1.shift());
}
d3.select(this) // Redraw the line.
.attr("d", line_RAW)
.attr("transform", null);
// Slide it to the left.
d3.active(this)
.attr("transform", "translate(" + x_RAW(-1) + ",0)")
.transition()
.on("start", tick1);
for (var i = 0; i < 50; i++) {
data_RAW2.shift(); // Pop the old data point off the front.
}
}
}
function tick4() {
if (in2.length > 49) { // Push a new data point onto the back.
// console.log("data_PPG = "+data_PPG);
// console.log("in4 = "+in2);
for (var i = 0; i < 50; i++) {
data_PPG.push(in2.shift());
}
d3.select(this) // Redraw the line.
.attr("d", line_RAW)
.attr("transform", null);
// Slide it to the left.
d3.active(this)
.attr("transform", "translate(" + x_RAW(-1) + ",0)")
.transition()
.on("start", tick4);
for (var i = 0; i < 50; i++) {
data_PPG.shift(); // Pop the old data point off the front.
}
}
}
function estimateHR(){
var aux_queue=[];
queue = queue.map(function(x) { return x+1000;});//update time of previous peak occurance (they are now 1sec late)
var pos = in2.indexOf(1023); //LOOK FOR PEAK OCCURENCEs
while (~pos) { //This bitwise not operator returns false if -1 and true if positive. A perfect match for indexOf which returns the index 0 ... n if found and -1 if not.
aux_queue.push(pos); //add peaks occurence QUEUE
pos = in2.indexOf(1023, pos + 1); // use old position incremented to keep searching for peak occurence
}
while(aux_queue.length>0){ //insert peak occurence in adequate order to queue
queue.push(aux_queue.pop());
}
while(queue[0]>10000){ //Remove peaks more than 10seconds' old
queue.shift()
}
var diff=0;
for(var i = 0; i<queue.length-1;i++){ //Add differences between each peak occurence
diff+=queue[i]-queue[i+1]
}
//averages the distances and converts this ms value to BPM HR
var hr = Math.round(60000/(diff*1.0/(queue.length-1)))
if(hr_max<hr){ hr_max=hr; }
if(hr_min>hr){ hr_min=hr; }
data_HR.push(hr);
return hr;
}
function drawHighlight(){
focus_EEG2 = chart_EEG2.append("g")
.attr("class", "focus")
.style("display", "none");
focus_EEG2.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus_EEG2.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus_EEG2.append("circle")
.attr("r", 4);
focus_EEG2.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus_EEG1 = chart_EEG1.append("g")
.attr("class", "focus")
.style("display", "none");
focus_EEG1.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus_EEG1.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus_EEG1.append("circle")
.attr("r", 4);
focus_EEG1.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus_HR = chart_HR.append("g")
.attr("class", "focus")
.style("display", "none");
focus_HR.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus_HR.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus_HR.append("circle")
.attr("r", 4);
focus_HR.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus_VALNC = chart_VALNC.append("g")
.attr("class", "focus")
.style("display", "none");
focus_VALNC.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus_VALNC.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus_VALNC.append("circle")
.attr("r", 4);
focus_VALNC.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus_FOCUS = chart_FOCUS.append("g")
.attr("class", "focus")
.style("display", "none");
focus_FOCUS.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus_FOCUS.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus_FOCUS.append("circle")
.attr("r", 4);
focus_FOCUS.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus_MEDI = chart_MEDI.append("g")
.attr("class", "focus")
.style("display", "none");
focus_MEDI.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus_MEDI.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus_MEDI.append("circle")
.attr("r", 4);
focus_MEDI.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
svg_EEG2.on("mouseover", function() { focus_EEG2.style("display", null); })
.on("mouseout", function() { focus_EEG2.style("display", "none"); })
.on("mousemove",update_mouse_pos_EEG2);
svg_EEG1.on("mouseover", function() { focus_EEG1.style("display", null); })
.on("mouseout", function() { focus_EEG1.style("display", "none"); })
.on("mousemove",update_mouse_pos_EEG1);
svg_HR.on("mouseover", function() { focus_HR.style("display", null); })
.on("mouseout", function() { focus_HR.style("display", "none"); })
.on("mousemove",update_mouse_pos_HR);
svg_VALNC.on("mouseover", function() { focus_VALNC.style("display", null); })
.on("mouseout", function() { focus_VALNC.style("display", "none"); })
.on("mousemove",update_mouse_pos_VALNC);
svg_FOCUS.on("mouseover", function() { focus_FOCUS.style("display", null); })
.on("mouseout", function() { focus_FOCUS.style("display", "none"); })
.on("mousemove",update_mouse_pos_FOCUS);
svg_MEDI.on("mouseover", function() { focus_MEDI.style("display", null); })
.on("mouseout", function() { focus_MEDI.style("display", "none"); })
.on("mousemove",update_mouse_pos_MEDI);
}
function update_mouse_pos_EEG2(){
//get x axis position of mouse
svg_x_mouse_pos = Math.round(x_EEG.invert(d3.mouse(this)[0]-margin.left)); //subtract by 3.5 because of axis transform
focus_EEG2.attr("transform", "translate(" + x_EEG(svg_x_mouse_pos) + "," + y_EEG(data_EEG2[svg_x_mouse_pos]) + ")");
focus_EEG2.select("text").text(function() { return data_EEG2[svg_x_mouse_pos]; }).style("fill", "red");
focus_EEG2.select(".x-hover-line").attr("y2", height-y_EEG(data_EEG2[svg_x_mouse_pos]));
focus_EEG2.select(".y-hover-line").attr("x2", width + width);
}
function update_mouse_pos_EEG1(){
//get x axis position of mouse
svg_x_mouse_pos = Math.round(x_EEG.invert(d3.mouse(this)[0]-margin.left)); //subtract by 3.5 because of axis transform
focus_EEG1.attr("transform", "translate(" + x_EEG(svg_x_mouse_pos) + "," + y_EEG(data_EEG1[svg_x_mouse_pos]) + ")");
focus_EEG1.select("text").text(function() { return data_EEG1[svg_x_mouse_pos]; }).style("fill", "red");
focus_EEG1.select(".x-hover-line").attr("y2", height-y_EEG(data_EEG1[svg_x_mouse_pos]));
focus_EEG1.select(".y-hover-line").attr("x2", width + width);
}
function update_mouse_pos_HR(){
//get x axis position of mouse
svg_x_mouse_pos = Math.round(x_amass.invert(d3.mouse(this)[0]-margin.left)); //subtract by 3.5 because of axis transform
focus_HR.attr("transform", "translate(" + x_amass(svg_x_mouse_pos) + "," + y_HR(data_HR[svg_x_mouse_pos]) + ")");
focus_HR.select("text").text(function() { return data_HR[svg_x_mouse_pos]; }).style("fill", "red");
focus_HR.select(".x-hover-line").attr("y2", height-margin.bottom -y_HR(data_HR[svg_x_mouse_pos]));
focus_HR.select(".y-hover-line").attr("x2", width + width);
}
function update_mouse_pos_VALNC(){
//get x axis position of mouse
svg_x_mouse_pos = Math.round(x_amass.invert(d3.mouse(this)[0]-margin.left)); //subtract by 3.5 because of axis transform
focus_VALNC.attr("transform", "translate(" + x_amass(svg_x_mouse_pos) + "," + y_VALNC(data_VALNC[svg_x_mouse_pos]) + ")");
focus_VALNC.select("text").text(function() { return data_VALNC[svg_x_mouse_pos]; }).style("fill", "red");
focus_VALNC.select(".x-hover-line").attr("y2", height-margin.bottom -y_VALNC(data_VALNC[svg_x_mouse_pos]));
focus_VALNC.select(".y-hover-line").attr("x2", width + width);
}
function update_mouse_pos_FOCUS(){
//get x axis position of mouse
svg_x_mouse_pos = Math.round(x_amass.invert(d3.mouse(this)[0]-margin.left)); //subtract by 3.5 because of axis transform
focus_FOCUS.attr("transform", "translate(" + x_amass(svg_x_mouse_pos) + "," + y_FOCUS(data_FOCUS[svg_x_mouse_pos]) + ")");
focus_FOCUS.select("text").text(function() { return data_FOCUS[svg_x_mouse_pos]; }).style("fill", "red");
focus_FOCUS.select(".x-hover-line").attr("y2", height-margin.bottom -y_FOCUS(data_FOCUS[svg_x_mouse_pos]))
.attr("y1", 0);
focus_FOCUS.select(".y-hover-line").attr("x2", width + width);
}
function update_mouse_pos_MEDI(){
//get x axis position of mouse
svg_x_mouse_pos = Math.round(x_amass.invert(d3.mouse(this)[0]-margin.left)); //subtract by 3.5 because of axis transform
focus_MEDI.attr("transform", "translate(" + x_amass(svg_x_mouse_pos) + "," + y_MEDI(data_MEDI[svg_x_mouse_pos]) + ")");
focus_MEDI.select("text").text(function() { return data_MEDI[svg_x_mouse_pos]; }).style("fill", "red");
focus_MEDI.select(".x-hover-line").attr("y2", height-margin.bottom -y_MEDI(data_MEDI[svg_x_mouse_pos]));
focus_MEDI.select(".y-hover-line").attr("x2", width + width);
}
};
this.version = function (msg) {
};
this.stop = function (msg) {
};
this.close = function (msg) {
}
};
// Process the server messages related with exceptions
sys = new function () {
this.exception = function (msg) {
alert(msg.toString());
}
};
console.log("end of script");
</script>
<body>
<section style="width:100%;height:33%;margin:auto; margin-bottom: 12px ">
<svg id="RAW_LEFT" width="740" height="263"></svg>
<svg id="RAW_RIGHT" width="740" height="263"></svg>
</section>
<section style="width:100%;height:33%;margin:auto; margin-bottom: 3px ">
<svg id="EEG_LEFT" width="740" height="100%"></svg>
<svg id="EEG_RIGHT" width="740" height="100%"></svg>
</section>
<section style="width:100%;height:33%;margin:auto; margin-bottom: 3px ">
<svg id="PPG" width="740" height="100%"></svg>
<svg id="HR" width="740" height="100%"></svg>
</section>
<section style="width:100%;height:33%;margin:auto; margin-bottom: 3px;">
<svg id="VALNC" width="740" height="100%"></svg>
<svg id="FOCUS" width="740" height="100%"></svg>
</section>
<section style="width:100%;height:33%;margin:auto ">
<svg id="MEDI" width="740" height="100%"></svg>
</section>
</body>
</html>