-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAveragingFilter.java
More file actions
55 lines (50 loc) · 1.22 KB
/
Copy pathAveragingFilter.java
File metadata and controls
55 lines (50 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* AveragingFilter extends Filter, enforcing a double input and
* keeping track of the number of inputs there has been.
* It simple overrides the processInput function such that the
* next output is set to be the average. This is simple arithemetic:
* size++;
* average = (((average * (size - 1)) + input) / size);
* @author gmh73
*
*/
public class AveragingFilter extends ScalarFilter{
private int size;
private double average;
/**
* Constructs an Averaging Filter with initial size 0 and
* initial average 0. Previous output is null.
*/
public AveragingFilter(){
size = 0;
average = 0.0;
}
/**
* The new average is calculated and outputted
*/
@Override
protected void processInput(Double input) {
size++;
average = (((average * (size - 1)) + input) / size);
setOutput(average);
}
/**
* The default reset for averaging filter is to set the average
* to r and set the size to 1
*/
@Override
public void reset(Double r) {
reset(r, 1);
}
/**
* An alternate reset function sets the average to r and the
* size to i.
* The previous output remains the same.
* @param r the average
* @param i the size
*/
public void reset(Double r, int i) {
average = r;
size = i;
}
}