-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretroVIS.html
929 lines (853 loc) · 39.9 KB
/
retroVIS.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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
<!DOCTYPE html>
<html lang="en">
<script language="javascript" type="text/javascript" src="d3/d3.js"></script>
<head>
<meta charset="UTF-8">
<title>retroVIS</title>
</head>
<style>
/* The side navigation menu */
.sidenav {
height: 100%; /* 100% Full-height */
width: 0; /* 0 width - change this with JavaScript */
position: fixed; /* Stay in place */
z-index: 1; /* Stay on top */
top: 0;
left: 0;
background-color: #111; /* Black*/
overflow-x: hidden; /* Disable horizontal scroll */
padding-top: 60px; /* Place content 60px from the top */
transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
}
/* The navigation menu links */
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s
}
/* When you mouse over the navigation links, change their color */
.sidenav a:hover, .offcanvas a:focus{
color: #f1f1f1;
}
/* Position and style the close button (top right corner) */
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
#main {
transition: margin-left .5s;
padding: 0px;
}
/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
body{
height:100%;
font: 12px sans-serif;
}
.line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
}
.grid line {
stroke: lightgrey;
stroke-opacity: 0.7;
shape-rendering: crispEdges;
}
.grid path {
stroke-width: 0;
}
.logo {
text-align: center;
font-size: x-large;
color: lightgray;
position:fixed;
display: block;
top:0%;
width: 9%;
height: 3%;
border-style: inset;
margin: 1px 0px 5px 0px;
background-color: #111;
}
#slider {
border-style: outset;
position: fixed;
bottom: 1px;
width: 99%;
text-align:center;
background-color:white;
font-size: 20px;
}
#rangeSlider {
bottom: 15px;
width: 80%;
text-align:center;
border-style: outset;
}
svg{
background-color: #F1F3F3;
}
.hover-line {
stroke: #DC143C;
stroke-width: 2px;
stroke-dasharray: 3,3;
}
.focus circle {
fill: #F1F3F3;
stroke: #DC143C;
stroke-width: 3px;
}
.tick text{
font-size: 12px;
}
.tick line{
opacity: 0.2;
}
</style>
<body style="background: linear-gradient(to right, white, lightsteelblue, white); ">
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">RealTimeVIS</a>
<a href="#">Help</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
<div id="main">
<div>
<span class="logo" onclick="openNav()"><strong> Menu</strong></span>
</div>
<div id="slider">
<text id="time">00:00</text>
<input type='range' min='0' max='0' step='1' value='0' id='rangeSlider' />
<button type="button" id="start">Play</button>
<button type="button" id="stop">Pause</button>
</div>
<section style="width:100%;height:30%;margin-top:2%; margin-bottom:3px;">
<svg id="EEG1" width="744" height="232"></svg>
<svg id="EEG2" width="744" height="232"></svg>
</section>
<section style="width:100%;height:30%;margin-bottom: 3px ">
<svg id="PPG" width="49.5%" height="232"></svg>
<svg id="HR" width="49.5%" height="232"></svg>
</section>
<section style="width:100%;height:30%;margin-bottom: 3px">
<svg id="VALNC" width="49.5%" height="232"></svg>
<svg id="FOCUS" width="49.5%" height="232"></svg>
</section>
<section style="width:100%;height:30%;margin-bottom: 3px">
<svg id="MEDI" width="49.5%" height="323"></svg>
</section>
</div>
<div style="height:6%;"></div>
</body>
<script>
/* Set the width of the side navigation to 250px and the left margin of the page content to 250px and add a black background color to body */
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}
/* Set the width of the side navigation to 0 and the left margin of the page content to 0, and the background color of body to white */
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.body.style.backgroundColor = "white";
}
var sample_rate = 1000;
var fft_res = 501;
var a0=[], //channels with clean LEFT EEG recording data
a1=[], //channels with clean RIGHT EEG recording data
a2=[], //channels with processed PPG recording data
a3=[], //Heart Rate array
a4=[], //valence array
a5=[], //engagement array
a6=[]; //meditation array
var line_PPG,line_HR,line_VLNC,line_FOCUS,line_MEDI;
var y_EEG,y_PPG,y_VLNC,y_FOCUS,y_MEDI,y_HR,x_EEG,x_PPG,x_amass;
var data_PPG=Array.apply(null, new Array(sample_rate)).map(Number.prototype.valueOf, 512);
var chart0,
chart1,
chart2,
chart3,
chart4,
chart5,
chart6;
var curr_pos,max_pos;
var focus0,focus1,focus3,focus2,focus4,focus5,focus6;
var svg_x_mouse_pos;
var 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,
hr_max=0,
hr_min=0;
var rowToHtml = function( row ) {
var result = "";
for (key in row) {
result += key + ": " + row[key] + "<br/>"
}
return result;
}
//d3.text("data/testnh.csv", function(text) {
// var data = d3.csv.parseRows(text).map(function(row) {
// return row.map(function(value) {
// return +value;
// });
// });
// console.log(data);
//});
var previewCsvUrl = function( csvUrl ) {
d3.text( csvUrl, function( text ) {
var data = d3.csvParseRows(text).map(function(row){
return row.map(function(value){
return +value; //converts string to number and returns it
});
});
//---------------------File NOW uploaded into browser-------------------------------------------------------------------
console.log("file loaded");
//Set EEGs and PPG metrics to a0..a2 array
a0=data[0]; //EEG_LEFT
a1=data[1]; //EEG_RIGHT
a2=data[2]; //PROCESSED PPG
max_pos=a0.length/fft_res;
console.log(max_pos);
//DERIVE theta, alpha, beta & frequencies METRICS; DERIVE valence, engagement and meditation METRICS
derive_EEG_Metrics();
for(var sec = 0; sec<max_pos; sec+=1){
var curr_hr = estimateHR(sec);
if(hr_max<curr_hr){ hr_max=curr_hr; }
if(hr_min>curr_hr){ hr_min=curr_hr; }
a3.push(estimateHR(sec));
}
//Scalable Vector Graphic's code (graphical interface) below:
//select corresponding div to each new svg image
var svg0 = d3.select("svg#EEG1");
var svg1 = d3.select("svg#EEG2");
var svg2 = d3.select("svg#PPG");
var svg3 = d3.select("svg#HR");
var svg4 = d3.select("svg#VALNC");
var svg5 = d3.select("svg#FOCUS");
var svg6 = d3.select("svg#MEDI");
//remove any previously drawn svg images (needed in case of consecutive csv files opened in same session)
if (!svg0.select("g").empty()) {svg0.select("g").remove()}
if (!svg1.select("g").empty()) {svg1.select("g").remove()}
if (!svg2.select("g").empty()) {svg2.select("g").remove()}
if (!svg3.select("g").empty()) {svg3.select("g").remove()}
if (!svg4.select("g").empty()) {svg4.select("g").remove()}
if (!svg5.select("g").empty()) {svg5.select("g").remove()}
if (!svg6.select("g").empty()) {svg6.select("g").remove()}
if (!svg6.select("g").empty()) {svg6.select("g").remove()}
//set up margins for each graph
var margin = {top: 10, right: 0, bottom: 10, left: 50},
width = +svg0.attr("width"),
height = +svg0.attr("height") - margin.top - margin.bottom;
drawAllCharts();
drawHighlight();
//first plot:
redrawFFT();
redrawLineChart();
//plot charts according to range slider position
d3.select('#rangeSlider').on('input', function() {
curr_pos=d3.select("#rangeSlider").property("value");
updateSlider();
redrawFFT();
redrawLineChart();
});
//plot charts automatically if play button pressed
var myTimer;
d3.select("#start").on("click", function() {
clearInterval (myTimer);
myTimer = setInterval (function() {
updateSlider();
redrawFFT();
redrawLineChart();
}, 1000);
});
//stop automatic chart plotting if stop button pressed
d3.select("#stop").on("click", function() {
clearInterval (myTimer);
});
function drawAllCharts(){
//set up title of each svg image
svg0.append("text").attr("x", "50%").attr("y", "7%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("FP2 Power Spectrum");
svg1.append("text").attr("x", "50%").attr("y", "7%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("FP1 Power Spectrum");
svg2.append("text").attr("x", "50%").attr("y", "7%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("Photoplethysmogram");
svg2.append("text").attr("x", "50%").attr("y", "22%").attr("text-anchor", "middle").attr("id","bpm_display")
.style("font-size", "16px").text("60 bpm");
svg3.append("text").attr("x", "50%").attr("y", "7%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("Heart Rate");
svg4.append("text").attr("x", "50%").attr("y", "7%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("Emotional Valence");
svg5.append("text").attr("x", "50%").attr("y", "7%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("Engagement");
svg6.append("text").attr("x", "50%").attr("y", "7%").attr("text-anchor", "middle")
.style("font-size", "16px").style("text-decoration", "underline").text("Meditation");
//translate each svg image so we can have space for the axis
chart0= svg0.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart1= svg1.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart2= svg2.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart3= svg3.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart4= svg4.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart5= svg5.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
chart6= svg6.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//Now that we know the recording's duration we can set the slider's max
d3.select("#rangeSlider").attr("max",data[0].length/fft_res);
//Determine current position in slider (It's zero)
curr_pos=d3.select("#rangeSlider").property("value");
//Create a quantitative linear scale for each axis's distinct domain:
x_EEG = d3.scaleLinear().domain([1, fft_res-450-1]).range([0, width-margin.left*1.2]); //used for EEGs
x_PPG = d3.scaleLinear().domain([0, sample_rate - 1]).range([0, width-margin.left*1.2]); //used for PPG
//used for EMOTIONAL VALENCE, ENGAGEMENT & MEDITATION:
x_amass = d3.scaleLinear().domain([0, curr_pos+1]).range([0, width-margin.left*1.2]);
y_EEG = d3.scaleLog().domain([ 10000000 , 1]).range([0, height-margin.top]); //used for EEGs
y_PPG = d3.scaleLinear().domain([1024, 0]).range([0, height-margin.top]); //used for PPG
y_VLNC = 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
y_HR = d3.scaleLinear().domain([hr_max, hr_min]).range([0, height-margin.top]); //used for HR / time
// gridlines in x axis function
function make_x_gridlines() {
return d3.axisBottom(x_EEG)
.ticks(25)
}
// gridlines in y axis function
function make_y_gridlines() {
return d3.axisLeft(y_EEG)
.ticks(25)
}
line_EEG = d3.line()
.x(function (d, i) {return x_EEG(i+1);}) //return x(i+1) -- we discard 0Hz from t
.y(function (d, i) {return y_EEG(d);})
.curve(d3.curveCatmullRom);
line_PPG = d3.line()
.x(function (d, i) { return x_PPG(i);})
.y(function (d, i) { return y_PPG(d);});
line_HR = d3.line()
.x(function (d, i) {return x_amass(i);})
.y(function (d, i) {return y_HR(d);})
.curve(d3.curveCatmullRom);
line_VLNC = d3.line()
.x(function (d, i) {return x_amass(i);})
.y(function (d, i) {return y_VLNC(d);})
.curve(d3.curveCatmullRom);
line_FOCUS = d3.line()
.x(function (d, i) {return x_amass(i);})
.y(function (d, i) {return y_FOCUS(d);})
.curve(d3.curveCatmullRom);
line_MEDI = d3.line()
.x(function (d, i) {return x_amass(i);})
.y(function (d, i) {return y_MEDI(d);})
.curve(d3.curveCatmullRom);
svg0.on("mouseover", function() { focus0.style("display", null); })
.on("mouseout", function() { focus0.style("display", "none"); })
.on("mousemove",update_mouse_pos0);
svg1.on("mouseover", function() { focus1.style("display", null); })
.on("mouseout", function() { focus1.style("display", "none"); })
.on("mousemove",update_mouse_pos1);
svg2.on("mouseover", function() { focus2.style("display", null); })
.on("mouseout", function() { focus2.style("display", "none"); })
.on("mousemove",update_mouse_pos2);
svg3.on("mouseover", function() { focus3.style("display", null); })
.on("mouseout", function() { focus3.style("display", "none"); })
.on("mousemove",update_mouse_pos3);
svg4.on("mouseover", function() { focus4.style("display", null); })
.on("mouseout", function() { focus4.style("display", "none"); })
.on("mousemove",update_mouse_pos4);
svg5.on("mouseover", function() { focus5.style("display", null); })
.on("mouseout", function() { focus5.style("display", "none"); })
.on("mousemove",update_mouse_pos5);
svg6.on("mouseover", function() { focus6.style("display", null); })
.on("mouseout", function() { focus6.style("display", "none"); })
.on("mousemove",update_mouse_pos6);
chart0.append("defs").append("clipPath")
.attr("id", "clip0") //attr must differ
.append("rect")
.attr("width", width)
.attr("height", height);
chart0.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y_EEG(1) + ")")
.call(d3.axisBottom(x_EEG).ticks(25));
chart0.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_EEG));
chart0.append("g")
.attr("clip-path", "url(#clip0)")
.append("path")
.attr("id","path_0")
.attr("class", "line")
.attr("d", line_EEG(Array.apply(null, Array(50)).map(Number.prototype.valueOf,1))) //linearscale=0, logscale=1
.attr("transform", null);
chart1.append("defs").append("clipPath")
.attr("id", "clip1")
.append("rect")
.attr("width", width)
.attr("height", height);
chart1.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y_EEG(1) + ")")
.call(make_x_gridlines()
.tickSize(-height)
);
chart1.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_EEG));
chart1.append("g")
.attr("clip-path", "url(#clip1)")
.append("path")
.attr("id","path_1")
.attr("class", "line")
.attr("d", line_EEG(Array.apply(null, Array(50)).map(Number.prototype.valueOf,1)))//linearscale=0, logscale=1
.attr("transform", null);
chart2.append("defs").append("clipPath")
.attr("id", "clip2")
.append("rect")
.attr("width", width)
.attr("height", height);
chart2.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y_PPG(0) + ")")
.call(d3.axisBottom(x_PPG));
chart2.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_PPG));
chart2.append("g")
.attr("clip-path", "url(#clip1)")
.append("path")
.attr("id","path_2")
.datum(data_PPG)
.attr("class", "line")
.attr("d", line_PPG)
.attr("transform", null);
chart3.append("defs").append("clipPath")
.attr("id", "clip3")
.append("rect")
.attr("width", width)
.attr("height", height);
chart3.append("g")
.attr("class", "axis axis--x_amass")
.attr("transform", "translate(0," + y_HR(0) + ")")
.call(d3.axisBottom(x_amass).ticks(0,"s"));
chart3.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_HR));
chart3.append("g")
.attr("clip-path", "url(#clip3)")
.append("path")
.attr("id","path_3")
.attr("class", "line");
chart4.append("defs").append("clipPath")
.attr("id", "clip4")
.append("rect")
.attr("width", width)
.attr("height", height);
chart4.append("g")
.attr("class", "axis axis--x_amass")
.attr("transform", "translate(0," + y_VLNC(0) + ")")
.call(d3.axisBottom(x_amass).ticks(0));
chart4.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_VLNC));
chart4.append("g")
.attr("clip-path", "url(#clip4)")
.append("path")
.attr("id","path_4")
.attr("class", "line");
chart5.append("defs").append("clipPath")
.attr("id", "clip5")
.append("rect")
.attr("width", width)
.attr("height", height);
chart5.append("g")
.attr("class", "axis axis--x_amass")
.attr("transform", "translate(0," + y_FOCUS(0) + ")")
.call(d3.axisBottom(x_amass).ticks(0));
chart5.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_FOCUS));
chart5.append("g")
.attr("clip-path", "url(#clip5)")
.append("path")
.attr("id","path_5")
.attr("class", "line");
chart6.append("defs").append("clipPath")
.attr("id", "clip6")
.append("rect")
.attr("width", width)
.attr("height", height);
chart6.append("g")
.attr("class", "axis axis--x_amass")
.attr("transform", "translate(0," + y_MEDI(0) + ")")
.call(d3.axisBottom(x_amass).ticks(0));
chart6.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y_MEDI));
chart6.append("g")
.attr("clip-path", "url(#clip6)")
.append("path")
.attr("id","path_6")
.attr("class", "line");
}
//redrawLineChart interpolates xvalues (time on xx axis)
function redrawLineChart(){
var t = d3.transition().duration(500);
//update xx domain of HR Valence, Engagement & Meditation plots
x_amass = d3.scaleLinear().domain([0, curr_pos]).range([0, width-margin.left*1.2]);
d3.selectAll(".axis--x_amass").call(d3.axisBottom(x_amass).ticks(Math.ceil(curr_pos/10.0)));
line_VLNC = d3.line()
.x(function (d, i) {return x_amass(i);})
.y(function (d, i) {return y_VLNC(d);})
.curve(d3.curveCatmullRom);
line_FOCUS = d3.line()
.x(function (d, i) {return x_amass(i);})
.y(function (d, i) {return y_FOCUS(d);})
.curve(d3.curveCatmullRom);
line_MEDI = d3.line()
.x(function (d, i) {return x_amass(i);})
.y(function (d, i) {return y_MEDI(d);})
.curve(d3.curveCatmullRom);
line_HR = d3.line()
.x(function (d, i) {return x_amass(i);})
.y(function (d, i) {return y_HR(d);})
.curve(d3.curveCatmullRom);
for (var i = 0; i < 1000; i++) {
data_PPG.push(Number(a2[curr_pos*1000+i]));
}
d3.select("path#path_2.line")
.attr("d",line_PPG(a2.slice(curr_pos*1000,curr_pos*1000+2000)))
.attr("transform",null)
.transition()
.attr("transform","translate("+x_PPG(-1000)+",0)")
.duration(250);
hr=estimateHR(curr_pos);
d3.select("#bpm_display").text(hr.toString()+" bpm");
d3.select("#path_3")
.attr("d",line_HR(a3));
d3.select("#path_4")
.attr("d",line_VLNC(a4));
d3.select("#path_5")
.attr("d",line_FOCUS(a5));
d3.select("#path_6")
.attr("d",line_MEDI(a6));
for (var i = 0; i < 1000; i++) {
data_PPG.shift();
}
}
//redrawFFT interpolates yvalues (frequencies on xx axis)
function redrawFFT(){
// console.log(a0.slice(1+curr_pos*fft_res,curr_pos*fft_res+fft_res-450 -1));
curr_pos=d3.select("#rangeSlider").property("value");
d3.select("path#path_0.line")
.transition()
.duration(1000)
.attr("d",line_EEG(a0.slice(1+curr_pos*fft_res,curr_pos*fft_res+fft_res-450)));
d3.select("path#path_1.line")
.transition()
.duration(1000)
.attr("d",line_EEG(a1.slice(1+curr_pos*fft_res,curr_pos*fft_res+fft_res-450)));
}
function updateSlider(){
function n(n){ //helper function. Used to convert to MM:SS from #seconds
return n > 9 ? "" + n: "0" + n;
}
var b= d3.select("#rangeSlider");
var t = (+b.property("value") + 1) % (+b.property("max") + 1);
if (t == 0) {
t = +b.property("min");
}
b.property("value", t);
d3.select("#time").text(n(Math.floor(t / 60) || 0)+":"+n(t % 60));
curr_pos=d3.select("#rangeSlider").property("value");
}
function estimateHR(pos){
var queue=[];
if(pos<5){ //if we're in the first 5 seconds
var in2 = a2.slice(0,10000); //use first 10 secs to estimate hr
}else if(pos>max_pos-5){ //if we're in the last 5 seconds
var in2 = a2.slice(max_pos*1000-10000, max_pos*1000) //use last 10 secs to estimate hr
}else{
var in2 = a2.slice(pos * 1000-5000, pos * 1000+5000) //use [-5,5] seconds to estimate hr
}
var pos = in2.indexOf(1023); //LOOK FOR PEAK OCCURENCEs
//The 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.
while (~pos) {
queue.push(pos); //add peaks occurence QUEUE
pos = in2.indexOf(1023, pos + 1); // use old position incremented to keep searching for peak occurence
}
var diff=0;
var aux_comment=0;
for(var i = 0; i<queue.length-1;i++){ //Add distance (ms) between each peak occurence
diff+=queue[i+1]-queue[i]
aux_comment+=1;
}
//averages the distances and converts this ms value to BPM
var hr = Math.round(60000/(diff*1.0/(queue.length-1)))
return hr;
}
function derive_EEG_Metrics(){
for(var i = 4;i<data[0].length;i+=fft_res){//fft_res=501 frequencies. Each one corresponds to 1second (0-500Hz)
//determine left theta wave spike
var left_theta_spike=Math.max(data[0][i],data[0][i+1],data[0][i+2],data[0][i+3]); //4Hz,5Hz,6Hz,7Hz
//determine right theta wave spike
var right_theta_spike=Math.max(data[1][i],data[1][i+1],data[1][i+2],data[1][i+3]); //4Hz,5Hz,6Hz,7Hz
//determine left theta wave spike //8Hz,9Hz,10Hz,11Hz,12Hz,13Hz
var left_alpha_spike=Math.max(data[0][i+4],data[0][i+5],data[0][i+6],data[0][i+7],data[0][i+8],data[0][i+9]);
//determine right theta wave spike //8Hz,9Hz,10Hz,11Hz,12Hz,13Hz
var right_alpha_spike=Math.max(data[1][i+4],data[1][i+5],data[1][i+6],data[1][i+7],data[1][i+8],data[1][i+9]);
//determine left theta wave spike
var left_beta_spike=Math.max(data[0][i+13],data[0][i+14],data[0][i+15],data[0][i+16]);//17Hz,18Hz,19Hz,20Hz
//determine right theta wave spike
var right_beta_spike=Math.max(data[1][i+13],data[1][i+14],data[1][i+15],data[1][i+16]);//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; }
a4.push(valence); //store emotional valence in arraylist's corresponding second of the recording session
a5.push(engagement); //store engagement metric in arraylist's corresponding second of the recording session
a6.push(meditation); //store meditation metric in arraylist's corresponding second of the recording session
}
}
function drawHighlight(){
focus0 = chart0.append("g")
.attr("class", "focus")
.style("display", "none");
focus0.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus0.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus0.append("circle")
.attr("r", 4);
focus0.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus1 = chart1.append("g")
.attr("class", "focus")
.style("display", "none");
focus1.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus1.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus1.append("circle")
.attr("r", 4);
focus1.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus2 = chart2.append("g")
.attr("class", "focus")
.style("display", "none");
focus2.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus2.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus2.append("circle")
.attr("r", 4);
focus2.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus3 = chart3.append("g")
.attr("class", "focus")
.style("display", "none");
focus3.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus3.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus3.append("circle")
.attr("r", 4);
focus3.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus4 = chart4.append("g")
.attr("class", "focus")
.style("display", "none");
focus4.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus4.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus4.append("circle")
.attr("r", 4);
focus4.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus5 = chart5.append("g")
.attr("class", "focus")
.style("display", "none");
focus5.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus5.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus5.append("circle")
.attr("r", 4);
focus5.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus5 = chart5.append("g")
.attr("class", "focus")
.style("display", "none");
focus5.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus5.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus5.append("circle")
.attr("r", 4);
focus5.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
focus6 = chart6.append("g")
.attr("class", "focus")
.style("display", "none");
focus6.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
focus6.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
focus6.append("circle")
.attr("r", 4);
focus6.append("text")
.attr("x", -15)
.attr("dy", "-1.5em");
}
function update_mouse_pos0(){
//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
focus0.attr("transform", "translate(" + x_EEG(svg_x_mouse_pos) + "," + y_EEG(a0[curr_pos*fft_res+svg_x_mouse_pos]) + ")");
focus0.select("text").text(function() { return a0[curr_pos*501+svg_x_mouse_pos]; }).style("fill", "red");
focus0.select(".x-hover-line").attr("y2", height-margin.bottom -y_EEG(a0[curr_pos*501+svg_x_mouse_pos]));
focus0.select(".y-hover-line").attr("x2", width + width);
}
function update_mouse_pos1(){
//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
focus1.attr("transform", "translate(" + x_EEG(svg_x_mouse_pos) + "," + y_EEG(a1[curr_pos*fft_res+svg_x_mouse_pos]) + ")");
focus1.select("text").text(function() { return a1[curr_pos*501+svg_x_mouse_pos]; }).style("fill", "red");
focus1.select(".x-hover-line").attr("y2", height-margin.bottom -y_EEG(a1[curr_pos*501+svg_x_mouse_pos]));
focus1.select(".y-hover-line").attr("x2", width + width);
}
function update_mouse_pos2(){
//get x axis position of mouse
svg_x_mouse_pos = Math.round(x_PPG.invert(d3.mouse(this)[0]-margin.left)); //subtract by 50 because of axis transform
console.log(svg_x_mouse_pos);
focus2.attr("transform", "translate(" + x_PPG(svg_x_mouse_pos) + "," + y_PPG(a2[curr_pos*sample_rate+svg_x_mouse_pos+1000]) + ")");
focus2.select("text").text(function() { return a2[curr_pos*sample_rate+svg_x_mouse_pos+1000]; }).style("fill", "red");
focus2.select(".x-hover-line").attr("y2", height-margin.bottom -y_PPG(a2[curr_pos*sample_rate+svg_x_mouse_pos+1000]));
focus2.select(".y-hover-line").attr("x2", width + width);
}
function update_mouse_pos3(){
//get x axis position of mouse
svg_x_mouse_pos = Math.round(x_amass.invert(d3.mouse(this)[0]-margin.left));
focus3.attr("transform", "translate(" + x_amass(svg_x_mouse_pos) + "," + y_HR(a3[svg_x_mouse_pos]) + ")");
focus3.select("text").text(function() { return a3[svg_x_mouse_pos]; }).style("fill", "red");
focus3.select(".x-hover-line").attr("y2", height-y_HR(a3[svg_x_mouse_pos]));
focus3.select(".y-hover-line").attr("x2", width + width);
}
function update_mouse_pos4(){
//get x axis position of mouse
svg_x_mouse_pos = Math.round(x_amass.invert(d3.mouse(this)[0]-margin.left));
focus4.attr("transform", "translate(" + x_amass(svg_x_mouse_pos) + "," + y_VLNC(a4[svg_x_mouse_pos]) + ")");
focus4.select("text").text(function() { return a4[svg_x_mouse_pos]; }).style("fill", "red");
focus4.select(".x-hover-line").attr("y2", height - y_VLNC(a4[svg_x_mouse_pos]));
focus4.select(".y-hover-line").attr("x2", width + width);
}
function update_mouse_pos5(){
//get x axis position of mouse
svg_x_mouse_pos = Math.round(x_amass.invert(d3.mouse(this)[0]-margin.left));
focus5.attr("transform", "translate(" + x_amass(svg_x_mouse_pos) + "," + y_FOCUS(a5[svg_x_mouse_pos]) + ")");
focus5.select("text").text(function() { return a5[svg_x_mouse_pos]; }).style("fill", "red");
focus5.select(".x-hover-line").attr("y2", height-y_FOCUS(a5[svg_x_mouse_pos]));
focus5.select(".y-hover-line").attr("x2", width + width);
}
function update_mouse_pos6(){
//get x axis position of mouse
svg_x_mouse_pos = Math.round(x_amass.invert(d3.mouse(this)[0]-margin.left));
focus6.attr("transform", "translate(" + x_amass(svg_x_mouse_pos) + "," + y_MEDI(a6[svg_x_mouse_pos]) + ")");
focus6.select("text").text(function() { return a6[svg_x_mouse_pos]; }).style("fill", "red");
focus6.select(".x-hover-line").attr("y2", height-y_MEDI(a6[svg_x_mouse_pos]));
focus6.select(".y-hover-line").attr("x2", width + width);
}
})
//---------------------File NOT uploaded into browser-------------------------------------------------------------
}
d3.select("html")
.append("input")
.attr("type", "file")
.attr("accept", ".csv")
.style("margin", "1px 0px 5px 0px")
.style("top", "0%")
.style("border-style","outset")
.style("right", "0%")
.style("width","90%")
.style("height","3%")
.style("background-color","white")
.style("position", "absolute")
.style("font-weight","bold")
.on("change", function() {
console.log("loading file");
var file = d3.event.target.files[0];
if (file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
var dataUrl = evt.target.result;
// The following call results in an "Access denied" error in IE.
previewCsvUrl(dataUrl);
};
reader.readAsDataURL(file);
}
})
</script>
</html>