klhztrader/KLHZ.Trader.Core.Math/Declisions/Utils/SignalProcessing.cs

71 lines
2.4 KiB
C#

namespace KLHZ.Trader.Core.Math.Declisions.Utils
{
public static class SignalProcessing
{
public static (DateTime[] timestamps, decimal[] values) InterpolateData(DateTime[] timestamps, decimal[] values, TimeSpan timeStep)
{
var res = new List<decimal>();
var res2 = new List<DateTime>();
var startTime = new DateTime(timestamps[0].Year, timestamps[0].Month, timestamps[0].Day, 0, 0, 0, DateTimeKind.Utc);
var dt = timestamps[0] - startTime;
var totalSteps = System.Math.Ceiling((timestamps[timestamps.Length - 1] - timestamps[0]).TotalSeconds / timeStep.TotalSeconds);
var deltaSeconds = System.Math.Floor(dt.TotalSeconds / timeStep.TotalSeconds);
startTime = startTime.AddSeconds(deltaSeconds * timeStep.TotalSeconds);
var firstBound = startTime;
var secondBound = startTime + timeStep;
var bound = 0;
for (int i = 0; i < totalSteps; i++)
{
var count = 0;
var sum = 0m;
for (int i1 = bound; i1 < timestamps.Length; i1++)
{
if (timestamps[i1] > firstBound && timestamps[i1] <= secondBound)
{
count++;
sum += values[i1];
}
else if (count != 0)
{
bound = i1;
break;
}
}
if (count != 0)
{
res.Add(sum / count);
res2.Add(secondBound);
}
else if (bound < timestamps.Length - 2)
{
res.Add(values[bound]);
res2.Add(secondBound);
}
firstBound += timeStep;
secondBound += timeStep;
}
return (res2.ToArray(), res.ToArray());
}
public static decimal[] CalcDiffs(decimal[] values)
{
if (values.Length < 1) throw new ArgumentException();
var resArray = new decimal[values.Length - 1];
for (int i = 1; i < values.Length; i++)
{
resArray[i - 1] = values[i] - values[i - 1];
}
return resArray;
}
}
}