This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Indoor/math/MovingStdDevTS.h
2018-10-25 11:50:12 +02:00

61 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* © Copyright 2014 Urheberrechtshinweis
* Alle Rechte vorbehalten / All Rights Reserved
*
* Programmcode ist urheberrechtlich geschuetzt.
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
* Keine Verwendung ohne explizite Genehmigung.
* (vgl. § 106 ff UrhG / § 97 UrhG)
*/
#ifndef MOVINGSTDDEVTS_H
#define MOVINGSTDDEVTS_H
#include "MovingAverageTS.h"
/**
* moving stadnard-deviation using a given time-region
*/
template <typename T> class MovingStdDevTS {
private:
MovingAverageTS<double> avg;
MovingAverageTS<double> avg2;
public:
/** ctor with the window-size to use */
MovingStdDevTS(const Timestamp window, const T zeroElement) : avg(window, zeroElement), avg2(window, zeroElement) {
;
}
/** add a new entry */
void add(const Timestamp ts, const T& data) {
// E(x)
avg.add(ts, data);
// E(x^2)
avg2.add(ts, data*data);
}
/** get the current std-dev */
T get() const {
const double e = avg.get();
const double e2 = avg2.get();
const double var = e2 - e*e;
//if (var < 0) {return 0;}
return std::sqrt(var);
}
};
#endif // MOVINGSTDDEVTS_H