123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
-
- /**
- * The method will increase the given number by 1. If the given counter is equal
- * or greater to {@link Number.MAX_SAFE_INTEGER} then it will be rolled back to
- * 1.
- * @param {number} number - An integer counter value to be incremented.
- * @return {number} the next counter value increased by 1 (see the description
- * above for exception).
- */
- export function safeCounterIncrement(number) {
- let nextValue = number;
-
- if (number >= Number.MAX_SAFE_INTEGER) {
- nextValue = 0;
- }
-
- return nextValue + 1;
- }
-
- /**
- * Calculates the average value of am Array of numbers.
- *
- * @param {Float32Array} valueArray - Array of numbers.
- * @returns {number} - Number array average.
- */
- export function calculateAverage(valueArray) {
- return valueArray.length > 0 ? valueArray.reduce((a, b) => a + b) / valueArray.length : 0;
- }
-
-
- /**
- * Returns only the positive values from an array of numbers.
- *
- * @param {Float32Array} valueArray - Array of vad scores.
- * @returns {Array} - Array of positive numbers.
- */
- export function filterPositiveValues(valueArray) {
- return valueArray.filter(value => value >= 0);
- }
-
- /**
- * This class calculates a simple running average that continually changes
- * as more data points are collected and added.
- */
- export class RunningAverage {
- /**
- * Creates an instance of the running average calculator.
- */
- constructor() {
- this.average = 0;
- this.n = 0;
- }
-
- /**
- * Adds a new data point to the existing set of values and recomputes
- * the running average.
- * @param {number} value
- * @returns {void}
- */
- addNext(value) {
- if (typeof value !== 'number') {
- return;
- }
- this.n += 1;
- this.average = this.average + ((value - this.average) / this.n);
- }
-
- /**
- * Obtains the average value for the current subset of values.
- * @returns {number} - computed average.
- */
- getAverage() {
- return this.average;
- }
- }
|