@svta/common-media-library
Version:
A common library for media playback in JavaScript
43 lines • 1.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Ewma = void 0;
/**
* Exponential Weighted Moving Average (EWMA)
*
* @group Throughput
*
* @beta
*/
class Ewma {
constructor(alpha) {
this.alpha = alpha;
this.estimate = 0;
this.totalDuration = 0;
}
/**
* Samples the value of the resource and updates the estimate.
*/
sample(weight, value) {
const adjustedAlpha = Math.pow(this.alpha, weight);
this.estimate = adjustedAlpha * value + (1 - adjustedAlpha) * this.estimate;
this.totalDuration += weight;
}
/**
* Returns the estimate of the value (in units, as `value` during sampling)
*
* For example, if the value is throughput (in bits per seconds), the estimate
* will also be in bits per seconds.
*/
getEstimate() {
const zeroFactor = 1 - Math.pow(this.alpha, this.totalDuration);
return this.estimate / zeroFactor;
}
/**
* Returns the total duration of the resource (in units, as `weight` during sampling)
*/
getTotalDuration() {
return this.totalDuration;
}
}
exports.Ewma = Ewma;
//# sourceMappingURL=Ewma.js.map