Class optimization

This commit is contained in:
Miha
2024-10-27 16:11:08 -07:00
parent b2fcdda785
commit 6c67a0cf31
77 changed files with 2634 additions and 1455 deletions
+71 -51
View File
@@ -1,4 +1,4 @@
using System;
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
@@ -31,6 +31,9 @@ public class Afirma : AbstractBase
private readonly double[] _armaBuffer;
private readonly int _n;
private readonly double _sx2, _sx3, _sx4, _sx5, _sx6, _den;
private readonly double _twoPi = 2.0 * Math.PI;
private readonly double _fourPi = 4.0 * Math.PI;
private readonly double _sixPi = 6.0 * Math.PI;
/// <param name="periods">The number of periods for the sinc filter calculation.</param>
/// <param name="taps">The number of filter taps (filter length). Must be odd number.</param>
@@ -56,7 +59,7 @@ public class Afirma : AbstractBase
_armaBuffer = new double[taps];
_n = (Taps - 1) / 2;
// Calculate least squares coefficients in the constructor
// Precalculate least squares coefficients
_sx2 = (2 * _n + 1) / 3.0;
_sx3 = _n * (_n + 1) / 2.0;
_sx4 = _sx2 * (3 * _n * _n + 3 * _n - 1) / 5.0;
@@ -78,6 +81,7 @@ public class Afirma : AbstractBase
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override void ManageState(bool isNew)
{
if (isNew)
@@ -87,6 +91,34 @@ public class Afirma : AbstractBase
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double CalculateSincWeight(double x)
{
return Math.Abs(x) < 1e-10 ? 1.0 : Math.Sin(x) / x;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double GetWindowWeight(int k, int tapsMinusOne)
{
switch (Window)
{
case WindowType.Rectangular:
return 1.0;
case WindowType.Hanning1:
return 0.50 - 0.50 * Math.Cos(_twoPi * k / tapsMinusOne);
case WindowType.Hanning2:
return 0.54 - 0.46 * Math.Cos(_twoPi * k / tapsMinusOne);
case WindowType.Blackman:
return 0.42 - 0.50 * Math.Cos(_twoPi * k / tapsMinusOne) + 0.08 * Math.Cos(_fourPi * k / tapsMinusOne);
case WindowType.BlackmanHarris:
return 0.35875 - 0.48829 * Math.Cos(_twoPi * k / tapsMinusOne) +
0.14128 * Math.Cos(_fourPi * k / tapsMinusOne) -
0.01168 * Math.Cos(_sixPi * k / tapsMinusOne);
default:
return 1.0;
}
}
protected override double Calculation()
{
ManageState(IsNew);
@@ -94,71 +126,59 @@ public class Afirma : AbstractBase
if (_index >= Taps)
{
double a0 = _buffer[_n];
double a1 = _buffer[_n] - _buffer[_n + 1];
double sx2y = 0.0;
double sx3y = 0.0;
for (int i = 0; i <= _n; i++)
{
sx2y += i * i * _buffer[_n - i];
sx3y += i * i * i * _buffer[_n - i];
}
sx2y = 2.0 * sx2y / _n / (_n + 1);
sx3y = 2.0 * sx3y / _n / (_n + 1);
double p = sx2y - a0 * _sx2 - a1 * _sx3;
double q = sx3y - a0 * _sx3 - a1 * _sx4;
double a2 = (p * _sx6 / _sx5 - q) / _den;
double a3 = (q * _sx4 / _sx5 - p) / _den;
for (int k = 0; k <= _n; k++)
{
_armaBuffer[_n - k] = a0 + k * a1 + k * k * a2 + k * k * k * a3;
}
CalculateAdaptiveCoefficients();
}
double result = 0.0;
for (int k = 0; k < Taps; k++)
{
result += _buffer[k] * _weights[k] / _wsum;
result += _buffer[k] * _weights[k];
}
IsHot = _index >= WarmupPeriod;
return result;
return result / _wsum;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CalculateAdaptiveCoefficients()
{
double a0 = _buffer[_n];
double a1 = _buffer[_n] - _buffer[_n + 1];
double sx2y = 0.0;
double sx3y = 0.0;
for (int i = 0; i <= _n; i++)
{
double i2 = i * i;
sx2y += i2 * _buffer[_n - i];
sx3y += i2 * i * _buffer[_n - i];
}
sx2y = 2.0 * sx2y / _n / (_n + 1);
sx3y = 2.0 * sx3y / _n / (_n + 1);
double p = sx2y - a0 * _sx2 - a1 * _sx3;
double q = sx3y - a0 * _sx3 - a1 * _sx4;
double a2 = (p * _sx6 / _sx5 - q) / _den;
double a3 = (q * _sx4 / _sx5 - p) / _den;
for (int k = 0; k <= _n; k++)
{
double k2 = k * k;
_armaBuffer[_n - k] = a0 + k * a1 + k2 * a2 + k2 * k * a3;
}
}
private double CalculateWeights()
{
double wsum = 0.0;
double centerTap = (Taps - 1) / 2.0;
int tapsMinusOne = Taps - 1;
for (int k = 0; k < Taps; k++)
{
double windowWeight;
switch (Window)
{
case WindowType.Rectangular:
windowWeight = 1.0;
break;
case WindowType.Hanning1:
windowWeight = 0.50 - 0.50 * Math.Cos(2.0 * Math.PI * k / (Taps - 1));
break;
case WindowType.Hanning2:
windowWeight = 0.54 - 0.46 * Math.Cos(2.0 * Math.PI * k / (Taps - 1));
break;
case WindowType.Blackman:
windowWeight = 0.42 - 0.50 * Math.Cos(2.0 * Math.PI * k / (Taps - 1)) + 0.08 * Math.Cos(4.0 * Math.PI * k / (Taps - 1));
break;
case WindowType.BlackmanHarris:
windowWeight = 0.35875 - 0.48829 * Math.Cos(2.0 * Math.PI * k / (Taps - 1)) + 0.14128 * Math.Cos(4.0 * Math.PI * k / (Taps - 1)) - 0.01168 * Math.Cos(6.0 * Math.PI * k / (Taps - 1));
break;
default:
windowWeight = 1.0;
break;
}
double sincWeight;
sincWeight = Math.Abs(k - centerTap) < 1e-10 ? 1.0 : Math.Sin(Math.PI * (k - centerTap) / Periods) / (Math.PI * (k - centerTap) / Periods);
double windowWeight = GetWindowWeight(k, tapsMinusOne);
double x = Math.PI * (k - centerTap) / Periods;
double sincWeight = CalculateSincWeight(x);
_weights[k] = windowWeight * sincWeight;
wsum += _weights[k];