using Chronos.Core.Motion; using Chronos.Testing; namespace Chronos.Core.Tests.Motion; public sealed class FourierTests { private readonly TestContext _context; public FourierTests(TestContext context) => _context = context; [Theory] [Case(1)] [Case(2)] [Case(64)] [Case(1024)] [Case(4096)] public void PowerOfTwoRoundTrip(int n) { var (re, im) = RandomSignal(n, seed: n); var (r0, i0) = ((double[])re.Clone(), (double[])im.Clone()); Fourier.Forward(re, im); Fourier.Inverse(re, im); var error = MaxError(r0, i0, re, im); _context.Log($"n = {n}: round-trip max error {error:E2}"); Assert.LessOrEqual(error, 1e-5); } [Theory] [Case(3)] [Case(7)] [Case(13)] [Case(97)] [Case(251)] [Case(1009)] public void BluesteinMatchesDirectDftForPrimes(int n) { var (re, im) = RandomSignal(n, seed: n); var (er, ei) = Dft(re, im); Fourier.Forward(re, im); var error = MaxError(er, ei, re, im) / double.Sqrt(n); _context.Log($"n = {n}: Bluestein vs direct DFT, max error / √n {error:E2}"); Assert.LessOrEqual(error, 1e-9); } [Theory] [Case(12)] [Case(100)] [Case(1000)] public void CompositeLengthsRoundTrip(int n) { var (re, im) = RandomSignal(n, seed: 3 * n); var (r0, i0) = ((double[])re.Clone(), (double[])im.Clone()); Fourier.Forward(re, im); Fourier.Inverse(re, im); Assert.LessOrEqual(MaxError(r0, i0, re, im), 1e-5); } [Theory] [Case(64)] [Case(100)] [Case(15)] public void RealTransformMatchesComplexAndRoundTrips(int n) { var (signal, _) = RandomSignal(n, seed: 11 * n); var bins = n / 2 + 1; var hr = new double[bins]; var hi = new double[bins]; Fourier.ForwardReal(signal, hr, hi); var re = (double[])signal.Clone(); var im = new double[n]; Fourier.Forward(re, im); Assert.LessOrEqual(MaxError(re.AsSpan(0, bins).ToArray(), im.AsSpan(0, bins).ToArray(), hr, hi), 1e-9); var back = new double[n]; Fourier.InverseReal(hr, hi, back); for (var i = 0; i < n; i++) Assert.Approximately(signal[i], back[i], 1e-9); } [Theory] [Case(64, 64)] [Case(48, 36)] [Case(256, 160)] public void TwoDimensionalRoundTripAndShiftTheorem(int width, int height) { var (re, im) = RandomSignal(width * height, seed: width + height); var (r0, i0) = ((double[])re.Clone(), (double[])im.Clone()); Fourier.Forward2D(re, im, width, height); // A unit impulse at (1, 0) transforms to e^(−2πiu/W) on every row: check one bin of a separate impulse. var dr = new double[width * height]; var di = new double[width * height]; dr[1] = 1; Fourier.Forward2D(dr, di, width, height); var (s, c) = double.SinCos(-2 * double.Pi * 3 / width); Assert.Approximately(c, dr[2 * width + 3], 1e-12); Assert.Approximately(s, di[2 * width + 3], 1e-12); Fourier.Inverse2D(re, im, width, height); Assert.LessOrEqual(MaxError(r0, i0, re, im), 1e-5); } [Test] public void ParsevalHolds() { const int n = 512; var (re, im) = RandomSignal(n, seed: 5); double time = 0, freq = 0; for (var i = 0; i < n; i++) time += re[i] * re[i] + im[i] * im[i]; Fourier.Forward(re, im); for (var i = 0; i < n; i++) freq += re[i] * re[i] + im[i] * im[i]; Assert.Approximately(time, freq / n, 1e-9 * time); } [Test] public void WindowsHaveExpectedShape() { var hann = new double[9]; Fourier.Hann(hann); Assert.Approximately(0, hann[0], 1e-15); Assert.Approximately(1, hann[4], 1e-15); Assert.Approximately(0.5, hann[2], 1e-12); Assert.Approximately(hann[1], hann[7], 1e-15); var rect = new double[9]; Fourier.Tukey(rect, 0); Assert.True(rect.All(v => v == 1)); var tukey = new double[101]; Fourier.Tukey(tukey, 0.5); Assert.Approximately(0, tukey[0], 1e-15); Assert.Equal(1.0, tukey[25]); Assert.Equal(1.0, tukey[75]); // Mid-taper (x = 0.125) is at half height; the flanks rise monotonically and mirror each other. Assert.InRange(tukey[12], 0.45, 0.5); Assert.InRange(tukey[13], 0.5, 0.55); Assert.Monotonic(tukey.Take(26).ToArray()); for (var i = 0; i < 101; i++) Assert.Approximately(tukey[i], tukey[100 - i], 1e-15); } [Test] public void PlansAreCached() => Assert.Same(Fourier.Plan(96), Fourier.Plan(96)); private static (double[] Re, double[] Im) RandomSignal(int n, int seed) { var random = new Random(seed); var re = new double[n]; var im = new double[n]; for (var i = 0; i < n; i++) { re[i] = random.NextDouble() * 2 - 1; im[i] = random.NextDouble() * 2 - 1; } return (re, im); } private static (double[] Re, double[] Im) Dft(double[] re, double[] im) { var n = re.Length; var or = new double[n]; var oi = new double[n]; for (var k = 0; k < n; k++) { double sr = 0, si = 0; for (var t = 0; t < n; t++) { var (s, c) = double.SinCos(-2 * double.Pi * ((long)k * t % n) / n); sr += re[t] * c - im[t] * s; si += re[t] * s + im[t] * c; } or[k] = sr; oi[k] = si; } return (or, oi); } private static double MaxError(double[] ar, double[] ai, double[] br, double[] bi) { double max = 0; for (var i = 0; i < ar.Length; i++) max = double.Max(max, double.Max(double.Abs(ar[i] - br[i]), double.Abs(ai[i] - bi[i]))); return max; } }