在这些情况下,直接下采样(每4只保留1次)不会起作用,所以我想到插值,但是stackoverflow当时没有解决方案。所以自己回答。
答案 0 :(得分:6)
我的解决方案的source。
插值数组的代码,
function interpolateArray(data, fitCount) {
var linearInterpolate = function (before, after, atPoint) {
return before + (after - before) * atPoint;
};
var newData = new Array();
var springFactor = new Number((data.length - 1) / (fitCount - 1));
newData[0] = data[0]; // for new allocation
for ( var i = 1; i < fitCount - 1; i++) {
var tmp = i * springFactor;
var before = new Number(Math.floor(tmp)).toFixed();
var after = new Number(Math.ceil(tmp)).toFixed();
var atPoint = tmp - before;
newData[i] = linearInterpolate(data[before], data[after], atPoint);
}
newData[fitCount - 1] = data[data.length - 1]; // for new allocation
return newData;
};
使用它的例子:
var originalArry = [1,5,3];
var newArry = interpolateArray([1,5,3],5);