Skip to content

Commit

Permalink
Rework of averageData (#41)
Browse files Browse the repository at this point in the history
* new average algorithm

* fixed casting type

* attempting to get rid of casting

* fixed casting and added comments
  • Loading branch information
AaronP15 authored and anushkakulk committed Jun 12, 2024
1 parent 2d61658 commit 0aee3d4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/utils/SensorDataUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,19 @@ namespace{
int getAverageOfSensorDataIntField(const std::vector<SensorData>& datas, int (*selectorFct)(const SensorData&)){
//TODO: we gotta be careful for int overflow here

int total = 0;
double i = 1;
double currentAvg = 0;
for (const auto& sensorData: datas){
total += selectorFct(sensorData);
// Aaron's Algorithm for Average
// The average is a simplified version of A' =(A(n-1) + a)/n
// where A' is the new average at n, A is the current average at n - 1, and a is the value at n
currentAvg *= 1 - (1.0 / i);
currentAvg += selectorFct(sensorData) / i;
i++;
}

// TODO: this cast is technically iffy, but once we switch to fixed size vectors we should be safe
return total / static_cast<int>(datas.size());
return static_cast<int>(currentAvg);
}
}

Expand Down

0 comments on commit 0aee3d4

Please sign in to comment.