SatNOGS-COMMS  4.1.0
A COMMS subsystem for CubeSats
Loading...
Searching...
No Matches
moving_avg.hpp
Go to the documentation of this file.
1/*
2 * SatNOGS-COMMS MCU software
3 *
4 * Copyright (C) 2025, Libre Space Foundation <http://libre.space>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GNU General Public License v3.0 or later
20 */
21
22#pragma once
23
24#include <etl/circular_buffer.h>
25#include <type_traits>
26
28{
29
30template <typename OutputT, typename InputT, size_t SIZE> class moving_avg
31{
32private:
33 OutputT m_sum{0};
34 etl::circular_buffer<InputT, SIZE> m_circ;
35
36public:
37 moving_avg(InputT init = 0) : m_sum(0)
38 {
39 for (size_t i = 0; i < SIZE; i++) {
40 m_circ.push(init);
41 m_sum += init;
42 }
43 }
44
45 void
46 reset(InputT val)
47 {
48 for (size_t i = 0; i < SIZE; i++) {
49 operator()(val);
50 }
51 }
52
54 operator()(InputT sample)
55 {
56 m_sum += sample;
57 m_sum -= m_circ.front();
58 m_circ.push(sample);
59 return *this;
60 }
61
62 OutputT
63 operator()() const
64 {
65 return m_sum / OutputT{SIZE};
66 }
67};
68
69} // namespace satnogs::comms::utils
moving_avg & operator()(InputT sample)