From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- dom/media/webrtc/libwebrtcglue/RunningStat.h | 48 ++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 dom/media/webrtc/libwebrtcglue/RunningStat.h (limited to 'dom/media/webrtc/libwebrtcglue/RunningStat.h') diff --git a/dom/media/webrtc/libwebrtcglue/RunningStat.h b/dom/media/webrtc/libwebrtcglue/RunningStat.h new file mode 100644 index 0000000000..7a0e88f193 --- /dev/null +++ b/dom/media/webrtc/libwebrtcglue/RunningStat.h @@ -0,0 +1,48 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ +/* Adapted from "Accurately computing running variance - John D. Cook" + http://www.johndcook.com/standard_deviation.html */ + +#ifndef RUNNING_STAT_H_ +#define RUNNING_STAT_H_ +#include + +namespace mozilla { + +class RunningStat { + public: + RunningStat() : mN(0), mOldM(0.0), mNewM(0.0), mOldS(0.0), mNewS(0.0) {} + + void Clear() { mN = 0; } + + void Push(double x) { + mN++; + + // See Knuth TAOCP vol 2, 3rd edition, page 232 + if (mN == 1) { + mOldM = mNewM = x; + mOldS = 0.0; + } else { + mNewM = mOldM + (x - mOldM) / mN; + mNewS = mOldS + (x - mOldM) * (x - mNewM); + + // set up for next iteration + mOldM = mNewM; + mOldS = mNewS; + } + } + + int NumDataValues() const { return mN; } + + double Mean() const { return (mN > 0) ? mNewM : 0.0; } + + double Variance() const { return (mN > 1) ? mNewS / (mN - 1) : 0.0; } + + double StandardDeviation() const { return sqrt(Variance()); } + + private: + int mN; + double mOldM, mNewM, mOldS, mNewS; +}; +} // namespace mozilla +#endif // RUNNING_STAT_H_ -- cgit v1.2.3