feat(validation): add input validation for Bilateral and Blma constructors; enhance Butter and Mgdi calculations with NaN handling

This commit is contained in:
Miha Kralj
2025-12-25 20:53:56 -08:00
parent ac8b2dbb3f
commit 0d077c24d8
15 changed files with 195 additions and 66 deletions
+10 -2
View File
@@ -24,7 +24,7 @@ public class ButterTests
{
var source = new double[10];
var destination = new double[5];
Assert.Throws<ArgumentOutOfRangeException>(() => Butter.Calculate(source, destination, 5));
Assert.Throws<ArgumentOutOfRangeException>(() => Butter.Calculate(source, destination, 5, double.NaN));
}
[Fact]
@@ -59,6 +59,14 @@ public class ButterTests
Assert.Equal(100, result.Value);
}
[Fact]
public void Initial_NaN_Input_ReturnsNaN()
{
var butter = new Butter(10);
var result = butter.Update(new TValue(DateTime.UtcNow, double.NaN));
Assert.True(double.IsNaN(result.Value));
}
[Fact]
public void AllModes_ProduceSameResult()
{
@@ -74,7 +82,7 @@ public class ButterTests
var tValues = series.Values.ToArray();
var spanInput = new ReadOnlySpan<double>(tValues);
var spanOutput = new double[tValues.Length];
Butter.Calculate(spanInput, spanOutput, period);
Butter.Calculate(spanInput, spanOutput, period, double.NaN);
double spanResult = spanOutput[^1];
// 3. Streaming Mode
+5 -3
View File
@@ -68,6 +68,7 @@ public sealed class Butter : AbstractBase
{
_state = new State();
_p_state = new State();
Last = new TValue(0, double.NaN);
}
public override void Reset()
@@ -97,6 +98,7 @@ public sealed class Butter : AbstractBase
if (double.IsNaN(input.Value) || double.IsInfinity(input.Value))
{
// Return Last (initialized to NaN) if no valid input has been seen yet
return Last;
}
@@ -126,7 +128,7 @@ public sealed class Butter : AbstractBase
{
var result = new TSeries();
Span<double> output = new double[source.Count];
Calculate(source.Values, output, _period);
Calculate(source.Values, output, _period, double.NaN);
for (int i = 0; i < source.Count; i++)
{
@@ -148,7 +150,7 @@ public sealed class Butter : AbstractBase
return result;
}
public static void Calculate(ReadOnlySpan<double> source, Span<double> destination, int period)
public static void Calculate(ReadOnlySpan<double> source, Span<double> destination, int period, double initialLast)
{
if (period < 2)
{
@@ -183,7 +185,7 @@ public sealed class Butter : AbstractBase
double x = source[i];
if (double.IsNaN(x) || double.IsInfinity(x))
{
destination[i] = i > 0 ? destination[i - 1] : 0;
destination[i] = i > 0 ? destination[i - 1] : initialLast;
continue;
}
double y = i < 2