share more code.

This commit is contained in:
Martin Preuss
2025-12-30 12:58:12 +01:00
parent 2a641f7a25
commit 0325d51c0e

View File

@@ -13,6 +13,9 @@
#include "./floatingavg.h" #include "./floatingavg.h"
static double _averageOverArray(const double *lastValues, int num);
AQDG_GRAPH_DATAPAIR_LIST *AQDG_Data_FloatingAverage(const AQDG_GRAPH_DATAPAIR_LIST *dpList, int num) AQDG_GRAPH_DATAPAIR_LIST *AQDG_Data_FloatingAverage(const AQDG_GRAPH_DATAPAIR_LIST *dpList, int num)
{ {
@@ -28,36 +31,19 @@ AQDG_GRAPH_DATAPAIR_LIST *AQDG_Data_FloatingAverage(const AQDG_GRAPH_DATAPAIR_LI
while(dp) { while(dp) {
AQDG_GRAPH_DATAPAIR *newDp; AQDG_GRAPH_DATAPAIR *newDp;
double v; double v;
int i;
v=AQDG_Graph_DataPair_GetValueY(dp); v=AQDG_Graph_DataPair_GetValueY(dp);
if (idx>=num) idx%=num;
idx=0; lastValues[idx++]=v;
lastValues[idx]=v;
idx++;
cnt++; cnt++;
if (cnt<num) { if (cnt<num) {
/* buffer not full, average over the values we already have */ /* buffer not full, average over the values we already have */
if (cnt) { v=_averageOverArray(lastValues, cnt);
double totalSum=0.0;
for (i=0; i<cnt; i++) {
totalSum+=lastValues[i];
}
v=totalSum/((double)cnt);
}
else
v=0.0;
} }
else { else {
double totalSum=0.0;
/* buffer full, average over full buffer */ /* buffer full, average over full buffer */
for (i=0; i<num; i++) { v=_averageOverArray(lastValues, num);
totalSum+=lastValues[i];
}
v=totalSum/((double)num);
} }
newDp=AQDG_Graph_DataPair_dup(dp); newDp=AQDG_Graph_DataPair_dup(dp);
@@ -73,3 +59,23 @@ AQDG_GRAPH_DATAPAIR_LIST *AQDG_Data_FloatingAverage(const AQDG_GRAPH_DATAPAIR_LI
double _averageOverArray(const double *lastValues, int num)
{
double v=0.0;
if (num) {
double totalSum=0.0;
int i;
/* average over full buffer */
for (i=0; i<num; i++) {
totalSum+=lastValues[i];
}
v=totalSum/((double)num);
}
return v;
}