Add a class to movingAvg class for calculation of averages for angle data

This commit is contained in:
Ulrich Meine
2026-07-06 22:31:28 +02:00
parent fb181c1b01
commit da4c66bec2
2 changed files with 128 additions and 6 deletions
+27
View File
@@ -22,6 +22,7 @@ class movingAvg
int getCount() { return m_nbrReadings; }
void reset();
T* getReadings() { return m_readings; }
T to2PI(T a);
private:
int m_interval; // number of data points for the moving average
@@ -33,6 +34,32 @@ class movingAvg
T* m_readings; // pointer to the dynamically allocated interval array
};
// moving average for angle type of data (wind, course, rotation)
// makes sense for data types double and float; angle in radians [0..2pi]
template <typename T>
class movingAvgAngle
{
public:
movingAvgAngle(int interval)
: m_interval{interval}, m_nbrReadings{0}, m_sumSin{0}, m_sumCos{0}, m_next{0}, m_buffer{nullptr} {}
~movingAvgAngle() { delete[] m_buffer; }
void begin();
T reading(T newReading);
T getAvg();
T getAvg(int nPoints);
int getCount() { return m_nbrReadings; }
void reset();
T* getReadings() { return m_buffer; }
T to2PI(T a);
private:
int m_interval; // number of data points for the moving average
int m_nbrReadings; // number of readings
double m_sumSin, m_sumCos; // sum for angle values should always be double for precision reasons, regardless of class type
int m_next; // index to the next reading
T* m_buffer; // pointer to the dynamically allocated interval array
};
// Include the implementation to satisfy template instantiation requirements
#include "movingAvg.tpp"