Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MathUtil.ts 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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: number): 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 an Array of numbers.
  18. *
  19. * @param {Float32Array} valueArray - Array of numbers.
  20. * @returns {number} - Number array average.
  21. */
  22. export function calculateAverage(valueArray: Float32Array): number {
  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: string): number {
  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 {number[]} - Array of positive numbers.
  46. */
  47. export function filterPositiveValues(valueArray: Float32Array): number[] {
  48. return Array.from(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. private average: number;
  56. private n: number;
  57. /**
  58. * Creates an instance of the running average calculator.
  59. */
  60. constructor() {
  61. this.average = 0;
  62. this.n = 0;
  63. }
  64. /**
  65. * Adds a new data point to the existing set of values and recomputes
  66. * the running average.
  67. * @param {number} value
  68. * @returns {void}
  69. */
  70. addNext(value: number): void {
  71. if (typeof value !== 'number') {
  72. return;
  73. }
  74. this.n += 1;
  75. this.average = this.average + ((value - this.average) / this.n);
  76. }
  77. /**
  78. * Obtains the average value for the current subset of values.
  79. * @returns {number} - computed average.
  80. */
  81. getAverage(): number {
  82. return this.average;
  83. }
  84. }
  85. /**
  86. * Subtracts the two numbers passed or returns 0 if any of the arguments are not a number.
  87. *
  88. * @param {*} x - The number we subtract from.
  89. * @param {*} y - The number we subtract.
  90. * @returns {number} - x - y or 0 if x or y is not a number.
  91. */
  92. export function safeSubtract(x: any, y: any): number {
  93. return isValidNumber(x) && isValidNumber(y) ? x - y : 0;
  94. }
  95. /**
  96. * Checks if the given value is a valid number.
  97. *
  98. * @param n - The value to check.
  99. * @returns - `true` if the value is a valid number, `false` otherwise.
  100. */
  101. export function isValidNumber(n: any): boolean {
  102. const v = Number.parseInt(n, 10);
  103. return Number.isFinite(v); // Filter out NaN and Infinity.
  104. }