2023-08-28 12:31:18 +02:00
|
|
|
/* A moving average window.
|
2019-04-12 09:48:31 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2019-04-12 09:48:31 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-06-23 16:26:44 +02:00
|
|
|
#include <cstddef>
|
2019-04-16 07:59:04 +02:00
|
|
|
#include <villas/dsp/window.hpp>
|
2019-04-12 09:48:31 +02:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace dsp {
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
template <typename T> class MovingAverageWindow : public Window<T> {
|
2019-04-12 09:48:31 +02:00
|
|
|
|
|
|
|
protected:
|
2023-09-07 13:19:19 +02:00
|
|
|
T state;
|
2019-04-12 09:48:31 +02:00
|
|
|
|
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
MovingAverageWindow(size_t len, T i = 0) : Window<T>(len, i), state(i) {}
|
2019-04-12 09:48:31 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
T update(T in) {
|
|
|
|
T out = Window<T>::update(in);
|
2019-04-12 09:48:31 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
state += in;
|
|
|
|
state -= out;
|
2019-04-12 09:48:31 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return state / (double)Window<T>::getLength();
|
|
|
|
}
|
2019-04-12 09:48:31 +02:00
|
|
|
};
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
} // namespace dsp
|
|
|
|
} // namespace villas
|