-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-emg.js
More file actions
146 lines (117 loc) · 3.77 KB
/
app-emg.js
File metadata and controls
146 lines (117 loc) · 3.77 KB
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
// define margins: ALWAYS first thing to do
var margin = {top: 50, right: 50 , bottom: 50, left: 50};
var width = 1800 - margin.left - margin.right,
height = 1000 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleLinear().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var valueline = d3.line()
.x(function(d) {return x(d.date); })
.y(function(d) {return y(d.number); })
;
// adding fancy rectangles
svg.append("rect")
.attr("width", width)
.attr("height", height)
.attr("fill", "#5C5B5A");
// Get the data
d3.csv("data_emg.csv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.date = +d.Time
d.number = +d.LAT;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([d3.min(data, function(d) {return d.number}), d3.max(data, function(d) { return d.number; })]);
// Setting the x-axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
svg.append("text")
.attr("x", width/2)
.attr("y", height+40)
.style("text-anchor", "middle")
.text("Time [s]");
// Setting the y-axis
svg.append("g")
.call(d3.axisLeft(y));
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0-margin.left)
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Value [uV]");
// Add the dataline
svg.append("path")
.data([data])
.attr("class", "line")
.attr("stroke", 'B2737B')
.attr("d", valueline)
;
//.attr("stroke-width", "2px");
});
function calcRMS(window) {
d3.select("path.area").remove();
d3.select("#rms").remove();
var window = Number(d3.select("#nValue").property("value"));
console.log(d3.select("#nValue").property("value"));
console.log(typeof window)
var rms = [];
d3.csv("data_emg.csv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.date = +d.Time
d.number = +d.LAT
rms.push(d.number);
});
// Modifying the rms array to the real rms values
for (var i=0; i<rms.length-window; i++) {
const a = Math.sqrt((d3.sum(rms.slice(i, i+window).map(x => Math.pow(x,2)))) / window)
rms[i] = a;
}
// var RMSline = d3.line()
// .x(function(d) {return x(d.date); })
// .y(function(d) {return y(d.number/3); });
var RMSline = d3.line()
.x(function(d) {return x(d.date); })
.y(function(d, i) {return y(rms[i]);})
// define the area; don't know why there is a 15 offset
var area = d3.area()
.x(function(d) { return x(d.date); })
.y0(height-15-d3.max(data, function(d) { return d.number; }))
.y1(function(d,i) { return y(rms[i]); });
svg.append("path")
.data([data])
.attr("stroke", "#FFE4BE")
.attr("fill-opacity", 0.0)
.attr("stroke-opacity",1.0)
.attr("stroke-width", "4px")
.attr("id", "rms")
.attr("d", RMSline)
.attr("stroke-dasharray", data.length + " " + data.length )
.attr("stroke-dashoffset", data.length)
.transition()
.duration(10000)
.ease(d3.easeLinear)
.attr("stroke-dashoffset", 0);
svg.append("path")
.data([data])
.attr("class", "area")
.attr("fill", '#07F63A')
.attr("fill-opacity", 0.0)
.attr("d", area)
// .attr("stroke-dasharray", data.length + " " + data.length )
// .attr("stroke-dashoffset", data.length)
.transition()
.duration(10000)
.ease(d3.easeLinear)
.attr("fill-opacity", 0.6);
;
});
};