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"
static double _averageOverArray(const double *lastValues, 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) {
AQDG_GRAPH_DATAPAIR *newDp;
double v;
int i;
v=AQDG_Graph_DataPair_GetValueY(dp);
if (idx>=num)
idx=0;
lastValues[idx]=v;
idx++;
idx%=num;
lastValues[idx++]=v;
cnt++;
if (cnt<num) {
/* buffer not full, average over the values we already have */
if (cnt) {
double totalSum=0.0;
for (i=0; i<cnt; i++) {
totalSum+=lastValues[i];
}
v=totalSum/((double)cnt);
}
else
v=0.0;
v=_averageOverArray(lastValues, cnt);
}
else {
double totalSum=0.0;
/* buffer full, average over full buffer */
for (i=0; i<num; i++) {
totalSum+=lastValues[i];
}
v=totalSum/((double)num);
v=_averageOverArray(lastValues, num);
}
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;
}