Files
Chronos/tests/Chronos.Core.Tests/Motion/FourierTests.cs
T
Alby96andClaude Opus 5.5 5f27a86406
CI / Build and test (pull_request) Failing after 2m3s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / Docker image (pull_request) Skipped
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 4s
refactor!: rename Titano to Chronos and move CI and releases to Gitea
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout. The release cross-compiles the Windows programs, checks their
  icon, version and DPI manifest, packs installers and zips, publishes the
  Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only
  tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow) and
  pe-check.

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 11:34:48 +02:00

184 lines
5.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}