Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Others/MovingAverage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package testing;

import java.util.ArrayList;


/**
* Simple class that adds values and returns the average over a set interval.
* AKA Interpolation.
*/
public class MovingAverage {

double intervalSize = 5;
ArrayList<Double> data = new ArrayList<Double>();

public MovingAverage(double size) {
this.intervalSize = size;
}

public MovingAverage() {
this(5);
}

//Adds a new data point and returns average.
public double register(double d) {
data.add(d);

//If theres more data than the set interval, cleave the data.
if(data.size() > intervalSize) {
cleave();
}

//Returns average of data set.
return sum()/data.size();
}

//Removes most outdated data point.
//Private to protect from null pointers.
private void cleave() {
data.remove(0);
}

//Adds all the data points. Riveting.
//Private to protect from null pointers.
private double sum() {
double s = 0;

for(int i = 0; i < data.size(); i++) {
s+=data.get(i);
}

return s;
}

}