您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MathUtil.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * The method will increase the given number by 1. If the given counter is equal
  3. * or greater to {@link Number.MAX_SAFE_INTEGER} then it will be rolled back to
  4. * 1.
  5. * @param {number} number - An integer counter value to be incremented.
  6. * @return {number} the next counter value increased by 1 (see the description
  7. * above for exception).
  8. */
  9. export function safeCounterIncrement(number) {
  10. let nextValue = number;
  11. if (number >= Number.MAX_SAFE_INTEGER) {
  12. nextValue = 0;
  13. }
  14. return nextValue + 1;
  15. }
  16. /**
  17. * Calculates the average value of am Array of numbers.
  18. *
  19. * @param {Float32Array} valueArray - Array of numbers.
  20. * @returns {number} - Number array average.
  21. */
  22. export function calculateAverage(valueArray) {
  23. return valueArray.length > 0 ? valueArray.reduce((a, b) => a + b) / valueArray.length : 0;
  24. }
  25. /**
  26. * Calculates a unique hash for a given string similar to Java's
  27. * implementation of String.hashCode()
  28. *
  29. * @param {String} string - String whose hash has to be calculated.
  30. * @returns {number} - Unique hash code calculated.
  31. */
  32. export function hashString(string) {
  33. let hash = 0;
  34. for (let i = 0; i < string.length; i++) {
  35. hash += Math.pow(string.charCodeAt(i) * 31, string.length - i);
  36. /* eslint-disable no-bitwise */
  37. hash = hash & hash; // Convert to 32bit integer
  38. }
  39. return Math.abs(hash);
  40. }
  41. /**
  42. * Returns only the positive values from an array of numbers.
  43. *
  44. * @param {Float32Array} valueArray - Array of vad scores.
  45. * @returns {Array} - Array of positive numbers.
  46. */
  47. export function filterPositiveValues(valueArray) {
  48. return valueArray.filter(value => value >= 0);
  49. }
  50. /**
  51. * This class calculates a simple running average that continually changes
  52. * as more data points are collected and added.
  53. */
  54. export class RunningAverage {
  55. /**
  56. * Creates an instance of the running average calculator.
  57. */
  58. constructor() {
  59. this.average = 0;
  60. this.n = 0;
  61. }
  62. /**
  63. * Adds a new data point to the existing set of values and recomputes
  64. * the running average.
  65. * @param {number} value
  66. * @returns {void}
  67. */
  68. addNext(value) {
  69. if (typeof value !== 'number') {
  70. return;
  71. }
  72. this.n += 1;
  73. this.average = this.average + ((value - this.average) / this.n);
  74. }
  75. /**
  76. * Obtains the average value for the current subset of values.
  77. * @returns {number} - computed average.
  78. */
  79. getAverage() {
  80. return this.average;
  81. }
  82. }