REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
running_avg.h
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3//
4// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
5
6#pragma once
7
8#include <array>
9
10// Some helping functions for array conversions
11template <class T, std::size_t N>
12std::array<T, N> operator+(const std::array<T, N> &l, const std::array<T, N> &r) {
13 std::array<T, N> result;
14 for (std::size_t i = 0; i < N; i++)
15 result[i] = l[i] + r[i];
16 return result;
17}
18
19template <class T, std::size_t N>
20std::array<T, N> operator-(const std::array<T, N> &l, const std::array<T, N> &r) {
21 std::array<T, N> result;
22 for (std::size_t i = 0; i < N; i++)
23 result[i] = l[i] - r[i];
24 return result;
25}
26
27template <class T, std::size_t N> std::array<T, N> operator*(const std::array<T, N> &l, const float &r) {
28 std::array<T, N> result;
29 for (std::size_t i = 0; i < N; i++)
30 result[i] = l[i] * r;
31 return result;
32}
33
34template <class T, std::size_t N> std::array<T, N> operator*(const float &l, const std::array<T, N> &r) {
35 return r * l;
36}
37
38namespace utils {
39
40template <typename Type, typename OtherType, std::size_t Size, typename... Types,
41 class = typename std::enable_if<sizeof...(Types) != Size>::type>
42constexpr std::array<Type, Size> convert(const std::array<OtherType, Size> source, const Types... data);
43
44template <typename Type, typename OtherType, std::size_t Size, typename... Types,
45 class = typename std::enable_if<sizeof...(Types) == Size>::type, class = void>
46constexpr std::array<Type, Size> convert(const std::array<OtherType, Size> source, const Types... data);
47
48template <typename Type, typename OtherType, std::size_t Size, typename... Types, class>
49constexpr std::array<Type, Size> convert(const std::array<OtherType, Size> source, const Types... data) {
50 return convert<Type>(source, data..., static_cast<const Type>(source[sizeof...(data)]));
51}
52
53template <typename Type, typename OtherType, std::size_t Size, typename... Types, class, class>
54constexpr std::array<Type, Size> convert(const std::array<OtherType, Size> source, const Types... data) {
55 return std::array<Type, Size>{{data...}};
56}
57
58template <class T> class RunningAverage {
59private:
60 std::size_t n = 0;
61 T sum{};
62
63public:
64 void add(const T &data) {
65 n++;
66 sum = sum + data;
67 };
68
69 void clear() {
70 n = 0;
71 sum = {};
72 }
73
74 T get_average() const { return 1.0f / static_cast<float>(n) * sum; }
75};
76
77} // namespace utils
void add(const T &data)
Definition running_avg.h:64
constexpr std::array< Type, Size > convert(const std::array< OtherType, Size > source, const Types... data)
Definition running_avg.h:49
std::array< T, N > operator-(const std::array< T, N > &l, const std::array< T, N > &r)
Definition running_avg.h:20
std::array< T, N > operator*(const std::array< T, N > &l, const float &r)
Definition running_avg.h:27
std::array< T, N > operator+(const std::array< T, N > &l, const std::array< T, N > &r)
Definition running_avg.h:12