Skip to content

Commit 421f5c8

Browse files
committed
fix LinearInterpolation dataset sorting
1 parent ee1fc80 commit 421f5c8

4 files changed

Lines changed: 43 additions & 6 deletions

File tree

src/main/java/frc/robot/lib/LinearInterpolation.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.nio.file.Paths;
77
import java.util.Arrays;
8+
import java.util.HashMap;
89
import java.util.List;
910

1011
import org.apache.commons.csv.CSVFormat;
@@ -19,7 +20,7 @@ public class LinearInterpolation {
1920
public double minX, maxX;
2021
public int numPoints = 0;
2122

22-
// Performs linear interpolation for a strictly monotonically increasing function.
23+
// Performs linear interpolation (Pass a filename relative to the src/main/deploy directory File extensions are not assumed)
2324
public LinearInterpolation(String filename) {
2425
try {
2526
CSVParser csvParser = CSVFormat.DEFAULT.parse(new FileReader(Filesystem.getDeployDirectory().toPath().resolve(Paths.get(filename)).toFile()));
@@ -39,10 +40,9 @@ public LinearInterpolation(String filename) {
3940
}
4041
}
4142
csvParser.close();
42-
Arrays.sort(xs);
43+
sortData();
4344
minX = xs[0];
4445
maxX = xs[xs.length - 1];
45-
Arrays.sort(ys);
4646
for (int i = 1; i < numPoints; i++) {
4747
// Linear interpolation (y = mx + b)
4848
slopes[i - 1] = (ys[i] - ys[i - 1]) / (xs[i] - xs[i - 1]);
@@ -71,4 +71,15 @@ public double calculate(double x) {
7171
return 0.0;
7272
}
7373
}
74+
75+
private void sortData() {
76+
HashMap<Double, Double> points = new HashMap<>();
77+
for(int i = 0; i < numPoints; i++) {
78+
points.put(xs[i], ys[i]);
79+
}
80+
Arrays.sort(xs);
81+
for(int i = 0; i < xs.length; i++) {
82+
ys[i] = points.get(xs[i]);
83+
}
84+
}
7485
}

src/test/java/frc/robot/lib/LinearInterpolationTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,37 @@ public class LinearInterpolationTest {
88

99
@Test
1010
public void testSimpleLinearInterpolation() {
11-
LinearInterpolation lInterp = new LinearInterpolation("src/test/resources/LinearInterpolData1.csv");
11+
LinearInterpolation lInterp = new LinearInterpolation("../../test/resources/LinearInterpolData1.csv");
1212
assertEquals(3, lInterp.calculate(3), 0.01);
1313

14-
lInterp = new LinearInterpolation("src/test/resources/LinearInterpolData2.csv");
14+
lInterp = new LinearInterpolation("../../test/resources/LinearInterpolData2.csv");
1515
assertEquals(3, lInterp.calculate(3), 0.01);
1616
}
1717

1818
@Test
1919
public void testLinearInterpolation() {
20-
LinearInterpolation lInterp = new LinearInterpolation("src/test/resources/LinearInterpolData3.csv");
20+
LinearInterpolation lInterp = new LinearInterpolation("../../test/resources/LinearInterpolData3.csv");
2121
assertEquals(2, lInterp.calculate(2), 0.01);
2222
assertEquals(3.5, lInterp.calculate(4), 0.01);
2323
}
2424

25+
@Test
26+
public void testNegativeSlopeLinearInterpolation() {
27+
LinearInterpolation lInterp = new LinearInterpolation("../../test/resources/LinearInterpolData4.csv");
28+
assertEquals(0.75, lInterp.calculate(0.25), 0.01);
29+
assertEquals(0.25, lInterp.calculate(0.75), 0.01);
30+
}
31+
32+
@Test
33+
public void testPiecewiseLinearInterpolation() {
34+
LinearInterpolation lInterp = new LinearInterpolation("../../test/resources/LinearInterpolData5.csv");
35+
assertEquals(1, lInterp.calculate(0), 0.01);
36+
assertEquals(0.75, lInterp.calculate(0.25), 0.01);
37+
assertEquals(0.25, lInterp.calculate(0.75), 0.01);
38+
assertEquals(0, lInterp.calculate(1), 0.01);
39+
assertEquals(0.25, lInterp.calculate(1.25), 0.01);
40+
assertEquals(0.75, lInterp.calculate(1.75), 0.01);
41+
assertEquals(1, lInterp.calculate(2), 0.01);
42+
}
43+
2544
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x,y
2+
0,1
3+
1,0
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
x,y
2+
0,1
3+
1,0
4+
2,1

0 commit comments

Comments
 (0)