選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

MathUtil.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }
  83. /**
  84. * Subtracts the two numbers passed or returns 0 if any of the arguments are not a number.
  85. *
  86. * @param {*} x - The number we subtract from.
  87. * @param {*} y - The number we subtract.
  88. * @returns {number} - x - y or 0 if x or is not a number.
  89. */
  90. export function safeSubtract(x, y) {
  91. return !isNaN(x) && !isNaN(y) ? x - y : 0;
  92. }