mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 22:08:05 +00:00
normalization of methods
This commit is contained in:
@@ -39,7 +39,7 @@ public class BpfTests
|
||||
|
||||
// 1. Span Mode
|
||||
double[] spanOutput = new double[series.Count];
|
||||
Bpf.Calculate(series.Values.ToArray(), spanOutput, lowerPeriod, upperPeriod);
|
||||
Bpf.Batch(series.Values.ToArray(), spanOutput, lowerPeriod, upperPeriod);
|
||||
|
||||
// 2. TSeries Batch Mode
|
||||
var bpfBatch = new Bpf(lowerPeriod, upperPeriod);
|
||||
@@ -111,7 +111,7 @@ public class BpfTests
|
||||
double[] input = Enumerable.Repeat(100.0, 500).ToArray();
|
||||
double[] output = new double[500];
|
||||
|
||||
Bpf.Calculate(input, output, 10, 20);
|
||||
Bpf.Batch(input, output, 10, 20);
|
||||
|
||||
// Last value should be close to 0
|
||||
Assert.True(Math.Abs(output[^1]) < 1e-6);
|
||||
|
||||
@@ -32,9 +32,9 @@ public class BpfValidationTests
|
||||
double[] out100 = new double[T];
|
||||
|
||||
// Instantiate BPF with LowerPeriod=40 (HP cutoff), UpperPeriod=10 (LP cutoff)
|
||||
Bpf.Calculate(sine5, out5, 40, 10);
|
||||
Bpf.Calculate(sine15, out15, 40, 10);
|
||||
Bpf.Calculate(sine100, out100, 40, 10);
|
||||
Bpf.Batch(sine5, out5, 40, 10);
|
||||
Bpf.Batch(sine15, out15, 40, 10);
|
||||
Bpf.Batch(sine100, out100, 40, 10);
|
||||
|
||||
// Analysis of results (last 100 samples to avoid warmup)
|
||||
double amp5 = GetAmplitude(out5);
|
||||
|
||||
+28
-14
@@ -115,7 +115,7 @@ public sealed class Bpf : AbstractBase
|
||||
double[] values = source.Values.ToArray();
|
||||
double[] results = new double[values.Length];
|
||||
|
||||
Calculate(values, results, LowerPeriod, UpperPeriod);
|
||||
Batch(values, results, LowerPeriod, UpperPeriod);
|
||||
|
||||
TSeries output = [];
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
@@ -181,24 +181,15 @@ public sealed class Bpf : AbstractBase
|
||||
return Last;
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
public static TSeries Batch(TSeries source, int lowerPeriod, int upperPeriod)
|
||||
{
|
||||
_state = default;
|
||||
_state.LastValid = double.NaN;
|
||||
_p_state = default;
|
||||
Last = default;
|
||||
var indicator = new Bpf(lowerPeriod, upperPeriod);
|
||||
return indicator.Update(source);
|
||||
}
|
||||
|
||||
public override void Prime(ReadOnlySpan<double> source, TimeSpan? step = null)
|
||||
{
|
||||
foreach (double val in source)
|
||||
{
|
||||
Update(new TValue(DateTime.UtcNow, val), isNew: true);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int lowerPeriod, int upperPeriod)
|
||||
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int lowerPeriod, int upperPeriod)
|
||||
{
|
||||
if (source.Length != output.Length)
|
||||
{
|
||||
@@ -267,6 +258,29 @@ public sealed class Bpf : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
_state = default;
|
||||
_state.LastValid = double.NaN;
|
||||
_p_state = default;
|
||||
Last = default;
|
||||
}
|
||||
|
||||
public override void Prime(ReadOnlySpan<double> source, TimeSpan? step = null)
|
||||
{
|
||||
foreach (double val in source)
|
||||
{
|
||||
Update(new TValue(DateTime.UtcNow, val), isNew: true);
|
||||
}
|
||||
}
|
||||
|
||||
public static (TSeries Results, Bpf Indicator) Calculate(TSeries source, int lowerPeriod, int upperPeriod)
|
||||
{
|
||||
var indicator = new Bpf(lowerPeriod, upperPeriod);
|
||||
TSeries results = indicator.Update(source);
|
||||
return (results, indicator);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unsubscribes from the source publisher if one was provided during construction.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user