计算标准偏差 - NaN误差

时间:2016-05-10 23:38:28

标签: javascript

我正在尝试计算标准偏差,但我一直得到一个NaN错误,已经有一段时间了,但我似乎无法发现错误来自何处,任何有用的帮助。阵列中的值是加速度计值,例如-1.416170358657837

function classifyMotion() {

/* array to hold last 300 samples */
var lastBatchOfSamples = [];

if (devArray > 1200) {

    /* Clear if more than 60s worth of data */
    devArray.length = 0;
}

/* retrieve last 300 samples and store in own array*/
lastBatchOfSamples = devArray.slice(Math.max(devArray.length - 300));

function standardDeviation(values) {

    var avg = average(values);

    var squareDiffs = lastBatchOfSamples.map(function(value) {
        var diff = value - avg;
        var sqrDiff = diff * diff;
        return sqrDiff;
    });

    var avgSquareDiff = average(squareDiffs);

    var stdDev = Math.sqrt(avgSquareDiff);
    return stdDev;
}

function average(lastBatchOfSamples) {
    var sum = lastBatchOfSamples.reduce(function(sum, value) {
        return sum + value;
    }, 0);
    test2 = sum;

    var avg = sum / lastBatchOfSamples.length;
    return avg;
}
var deviation = standardDeviation(lastBatchOfSamples);

}

0 个答案:

没有答案