in src/toMidi.ts [137:154]
function meanStdDev(array: number[][]): [number, number] {
const [sum, sumSquared, count] = array.reduce(
(prev, row) => {
// Calculate N * E[x], N * E[x^2] and N
const [rowSum, rowSumsSquared, rowCount] = row.reduce(
(p, value) => [p[0] + value, p[1] + value * value, p[2] + 1],
[0, 0, 0],
);
return [prev[0] + rowSum, prev[1] + rowSumsSquared, prev[2] + rowCount];
},
[0, 0, 0],
);
// E[x]
const mean = sum / count;
// sqrt( (1 / (N - 1)) * (E[x^2] - E[x]^2 / N))
const std = Math.sqrt((1 / (count - 1)) * (sumSquared - (sum * sum) / count));
return [mean, std];
}