klhztrader/KLHZ.Trader.Core.Tests/FFTTests.cs

114 lines
4.2 KiB
C#

using KLHZ.Trader.Core.Math.Declisions.Utils;
using MathNet.Numerics;
using MathNet.Numerics.IntegralTransforms;
using System.Security.Cryptography;
namespace KLHZ.Trader.Core.Tests
{
public class FFTTests
{
[Test]
public static void Test()
{
//var da = new List<float>();
//for (int i = 0; i < 100; i++)
//{
// da.Add((float)System.Math.Sin(0.5 * i)+(float)System.Math.Cos(0.5 * i));
//}
//var start = da.ToArray();
//var arrv = da.Select(d => new Complex32(d, 0)).ToArray();
//Fourier.Forward(arrv);
//var tem = arrv.Select(a => Complex32.Abs(a)).ToArray();
//var s = tem.Sum();
//Fourier.Inverse(arrv);
//var res = arrv.Select(a => a.Real).ToArray();
//for (int i = 0; i < 1000; i++)
//{
// var d = res[i] - start[i];
//}
}
[Test]
public static void CalcHarmonyPeriodTest1()
{
var period1 = FFT.CaclHarmonycPeriod(TimeSpan.FromHours(1), 1000, 1);
var period2 = FFT.CaclHarmonycPeriod(TimeSpan.FromHours(1), 1000, 2);
var period3 = FFT.CaclHarmonycPeriod(TimeSpan.FromHours(1), 1000, 3);
var period4 = FFT.CaclHarmonycPeriod(TimeSpan.FromHours(1), 1000, 4);
}
[Test]
public static void CalcPhaseRangeForMax_Sin()
{
var res = FFT.CalcPhaseRangeFoxMax(1, 0, 0, 0.001);
Assert.IsTrue(res.min < System.Math.PI / 2);
Assert.IsTrue(res.min > System.Math.PI / 2 * 0.9);
Assert.IsTrue(res.max > System.Math.PI / 2);
Assert.IsTrue(res.max < System.Math.PI / 2 * 1.1);
}
[Test]
public static void CalcPhaseRangeForMin_Sin()
{
var res = FFT.CalcPhaseRangeFoxMin(1, 0, 0, 0.001);
Assert.IsTrue(res.min < 3*System.Math.PI / 2);
Assert.IsTrue(res.min > 3*System.Math.PI / 2 * 0.9);
Assert.IsTrue(res.max > 3*System.Math.PI / 2);
Assert.IsTrue(res.max < 3*System.Math.PI / 2 * 1.1);
}
[Test]
public static void CalcPhaseRangeForMax_MinusSin()
{
var res = FFT.CalcPhaseRangeFoxMax(-1, 0, 0, 0.001);
Assert.IsTrue(res.min < 3*System.Math.PI / 2);
Assert.IsTrue(res.min > 3*System.Math.PI / 2 * 0.9);
Assert.IsTrue(res.max > 3*System.Math.PI / 2);
Assert.IsTrue(res.max < 3*System.Math.PI / 2 * 1.1);
}
[Test]
public static void CalcPhaseRangeForMax_Cos()
{
var res = FFT.CalcPhaseRangeFoxMax(0, 1, 0, 0.001);
Assert.IsTrue(res.min < System.Math.PI * 2);
Assert.IsTrue(res.min > System.Math.PI * 2 * 0.9);
Assert.IsTrue(res.max > 0);
Assert.IsTrue(res.max < System.Math.PI * 2*0.1);
}
[Test]
public static void CalcPhaseRangeForMin_Cos()
{
var res = FFT.CalcPhaseRangeFoxMin(0, 1, 0, 0.001);
Assert.IsTrue(res.min < System.Math.PI);
Assert.IsTrue(res.min > System.Math.PI * 0.9);
Assert.IsTrue(res.max > System.Math.PI);
Assert.IsTrue(res.max < System.Math.PI * 1.1);
}
[Test]
public static void CalcPhaseRangeForMax_CosWithShift()
{
var res = FFT.CalcPhaseRangeFoxMax(0, 1, System.Math.PI / 2, 0.001);
Assert.IsTrue(res.min < 3 * System.Math.PI / 2);
Assert.IsTrue(res.min > 3 * System.Math.PI / 2 * 0.9);
Assert.IsTrue(res.max > 3 * System.Math.PI / 2);
Assert.IsTrue(res.max < 3 * System.Math.PI / 2 * 1.1);
}
[Test]
public static void CalcPhaseRangeForMax_CosWithShift2()
{
var res = FFT.CalcPhaseRangeFoxMax(0, 1, - System.Math.PI / 2, 0.001);
Assert.IsTrue(res.min < System.Math.PI / 2);
Assert.IsTrue(res.min > System.Math.PI / 2 * 0.9);
Assert.IsTrue(res.max > System.Math.PI / 2);
Assert.IsTrue(res.max < System.Math.PI / 2 * 1.1);
}
}
}