我有一个处理草图,每秒都会读取声级。它将它输出到一个文件,最终结果如下:
17.51.11, 0.05900923, 0.059008658
17.51.12, 0.4037964, 0.40379566
17.51.13, 0.10111399, 0.101112984
17.51.14, 0.5614912, 0.56148833
17.51.15, 0.45223546, 0.45223528
17.51.16, 0.1956841, 0.19568439
17.51.17, 0.17803124, 0.17803174
(这两个值是左右音频输入,如果可能,我也想平均这些值!)
节点导入并拆分这些数据,并将其输出到图表(使用chart.js) - 但是很明显,对于任何实际的时间长度,图表将是不可读的,或者是愚蠢的长。所以我希望获得每分钟数据的平均值。我不知道怎么做 - 而且我担心搞乱时间戳。
var http = require('http');
var fs = require('fs');
var results = [];
var left = [];
var right = [];
var timestamp = [];
var rightfin = [];
var timefin = [];
var leftfin = [];
fs.readFile('20.8.2015_17.51.9.txt', "utf-8", function (err, data) {
if (err) throw err;
results = data.split("\n");
});
function callback() {
var lines = results;
for (var line in lines) {
var x = [];
x = lines[line].split(", ");
timestamp.push("'" + x[0] + "'");
//doesn't seem to work without adding quotes.
left.push(x[1] * 1000);
right.push(x[2] * 1000);
}
}
http.createServer(function (request, response) {
callback();
var leftfin = left.toString('utf8');
var rightfin = right.toString('utf8');
var timefin = timestamp.toString('utf8');
fs.readFile('chart.js', function (err, data) {
if (err) { throw err };
response.writeHead(200, { 'Content-Type': 'text/html' });
var chartjs = data.toString('utf-8');
response.write("<html>\n<head><meta charset='utf-8'><script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>\n<title>Sound Levels</title>\n</head><body onLoad='init()'><script type = 'text/javascript'>\n" + chartjs);
response.write("window.onLoad = function() \n{init();};\n function init() {");
response.write("\nvar ctx = $('#line').get(0).getContext('2d'); \nvar data = { \nlabels: [");
response.write(timefin);
response.write("], \ndatasets: [ \n{ \nlabel: 'My First dataset', \nfillColor: 'rgba(220,220,220,0.5)', \nstrokeColor: 'rgba(220,220,220,0.8)', \nhighlightFill: 'rgba(220,220,220,0.75)',\nhighlightStroke: 'rgba(220,220,220,1)', \ndata: [");
response.write(leftfin);
response.write("]}, \n{ \nlabel: 'My Second dataset', \nfillColor: 'rgba(151,187,205,0.5)', \nstrokeColor: 'rgba(151,187,205,0.8)', \nhighlightFill: 'rgba(151,187,205,0.75)', \nhighlightStroke: 'rgba(151,187,205,1)', \ndata: [");
response.write(rightfin);
response.write("] \n} ]\n}; \nvar myBarChart = new Chart(ctx).Bar(data);}</script>");
response.write(" <center> <canvas id='line' width='7500' height='400'> </canvas></center> </body></html>");
response.end();
});
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
答案 0 :(得分:1)
以下是回调版本:
var parseLines = function(lines) {
// make sure we have lines, or return a default
if (lines == null || !lines.length) { return [0.0, 0.0]; }
var left_sum = 0.0
, right_sum = 0.0
, line, i, left, right;
// use for(;;) instead of for in, to exclude properties that are not values
for (i = 0; i < lines.length; i += 1) {
line = lines[i].split(',');
// trim each number from whitespace and make it its a float
left = parseFloat(line[1].trim());
right = parseFloat(line[2].trim());
if (isNaN(left) || isNaN(right)) { continue; }
left_sum += left;
right_sum += right;
}
// return an array of [ left_average, right_average ]
return [ left_sum / lines.length, right_sum / lines.length ];
};
var averages = parseLines(results);