-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScalarLinearFilter.java
More file actions
98 lines (92 loc) · 2.66 KB
/
Copy pathScalarLinearFilter.java
File metadata and controls
98 lines (92 loc) · 2.66 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.util.Iterator;
/**
* ScalarLinearFilter extends Filter, and implements the scalar filter
* described in the assignment. It uses NBuffer to remember previous
* inputs and outputs.
* @author gmh73
*
*/
public class ScalarLinearFilter extends ScalarFilter {
//The a values
private final Double[] a;
//The b values
private final Double[] b;
//The recorded inputs
private NBuffer<Double> inputs;
//The recorded outputs
private NBuffer<Double> outputs;
/**
* Sets initial values and sets the buffers to all zeroes
* @param a The a values as specified by the assignment
* @param b The b values as specified by the assignment
*/
public ScalarLinearFilter(Double[] a, Double[] b){
this.a = a;
this.b = b;
inputs = new NBuffer<Double>(a.length);
outputs = new NBuffer<Double>(b.length);
resetBuffers(0.0, 0.0);
}
/**
* Sets all items in the buffers to the given values
* @param prevInputs all the previous inputs
* @param prevOutputs all the previous outputs
*/
private void resetBuffers(Double prevInputs, Double prevOutputs) {
inputs.reset(prevInputs);
outputs.reset(prevOutputs);
}
/**
* The new output value needs to be calculated according
* to the description in the assignment, and then stored
* into our buffer. The new input is also saved.
*/
@Override
protected void processInput(Double input) {
outputs.push(input);
//The next output will be calculated by adding
//all the b_i*x_i terms then subtracting the
//a_i*y_i terms
Iterator<Double> itB = outputs.iterator();
Double nextOutput = 0.0;
int index = 0;
//Sum all the b terms
while(itB.hasNext()){
nextOutput += b[b.length - 1 - index]*itB.next();
index++;
}
Iterator<Double> itA = inputs.iterator();
index = 0;
//subtract all the a terms
while(itA.hasNext()){
nextOutput -= a[a.length - 1 - index] * itA.next();
index++;
}
//set and push the next output
setOutput(nextOutput);
inputs.push(nextOutput);
}
/**
* The reset operation described in the assignment is implemented here.
*/
@Override
public void reset(Double r) {
//The previous inputs are set to r
Double prevInputs = r;
//We must calculate the previous outputs
Double prevOutputsNumerator = 0.0;
Double prevOutputsDenominator = 1.0;
//The numerator is a summation of b
for(int i = 0; i < b.length; i++){
prevOutputsNumerator += b[i];
}
//The denominator is 1 + a summation of a
for(int i = 0; i < a.length; i++){
prevOutputsDenominator += a[i];
}
//The output is also multiplied by r
Double prevOutputs = (r * prevOutputsNumerator) / prevOutputsDenominator;
//set the previous values
resetBuffers(prevInputs, prevOutputs);
}
}