diff --git a/wpimath/src/main/java/edu/wpi/first/math/filter/LinearFilter.java b/wpimath/src/main/java/edu/wpi/first/math/filter/LinearFilter.java index 3686825938..b4a1516d72 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/filter/LinearFilter.java +++ b/wpimath/src/main/java/edu/wpi/first/math/filter/LinearFilter.java @@ -147,17 +147,15 @@ public class LinearFilter { * stream-based online filtering (e.g., taking derivative of encoder samples in real-time). * * @param derivative The order of the derivative to compute. - * @param samples The number of samples to use to compute the given derivative. This must be one - * more than the order of derivative or higher. - * @param stencil List of stencil points. + * @param stencil List of stencil points. Its length is the number of samples to use to compute + * the given derivative. This must be one more than the order of the derivative or higher. * @param period The period in seconds between samples taken by the user. * @return Linear filter. * @throws IllegalArgumentException if derivative < 1, samples <= 0, or derivative >= * samples. */ @SuppressWarnings("LocalVariableName") - public static LinearFilter finiteDifference( - int derivative, int samples, int[] stencil, double period) { + public static LinearFilter finiteDifference(int derivative, int[] stencil, double period) { // See // https://en.wikipedia.org/wiki/Finite_difference_coefficient#Arbitrary_stencil_points // @@ -179,6 +177,8 @@ public class LinearFilter { "Order of derivative must be greater than or equal to one."); } + int samples = stencil.length; + if (samples <= 0) { throw new IllegalArgumentException("Number of samples must be greater than zero."); } @@ -236,7 +236,7 @@ public class LinearFilter { stencil[i] = -(samples - 1) + i; } - return finiteDifference(derivative, samples, stencil, period); + return finiteDifference(derivative, stencil, period); } /** Reset the filter state. */ diff --git a/wpimath/src/main/native/include/frc/filter/LinearFilter.h b/wpimath/src/main/native/include/frc/filter/LinearFilter.h index a72b486749..c19d95629e 100644 --- a/wpimath/src/main/native/include/frc/filter/LinearFilter.h +++ b/wpimath/src/main/native/include/frc/filter/LinearFilter.h @@ -180,7 +180,7 @@ class LinearFilter { * @tparam Derivative The order of the derivative to compute. * @tparam Samples The number of samples to use to compute the given * derivative. This must be one more than the order of - * derivative or higher. + * the derivative or higher. * @param stencil List of stencil points. * @param period The period in seconds between samples taken by the user. */ diff --git a/wpimath/src/test/java/edu/wpi/first/math/filter/LinearFilterTest.java b/wpimath/src/test/java/edu/wpi/first/math/filter/LinearFilterTest.java index 5e6d5bbb69..ec43cd578a 100644 --- a/wpimath/src/test/java/edu/wpi/first/math/filter/LinearFilterTest.java +++ b/wpimath/src/test/java/edu/wpi/first/math/filter/LinearFilterTest.java @@ -282,7 +282,7 @@ class LinearFilterTest { stencil[i] = -(samples - 1) / 2 + i; } - var filter = LinearFilter.finiteDifference(derivative, samples, stencil, h); + var filter = LinearFilter.finiteDifference(derivative, stencil, h); for (int i = (int) (min / h); i < (int) (max / h); ++i) { // Let filter initialize