feat(motion): add FFT plans and Kuglin–Hines phase correlation

Radix-2 Cooley–Tukey with SIMD butterflies and Bluestein chirp-z for
arbitrary lengths, cached plans, parallel 2D transforms, real-input
half-length transforms and Tukey/Hann windows. Phase correlation packs
both patches into one complex FFT, normalises the cross-power spectrum
with a Gaussian low-pass and refines the peak with a log-parabola fit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 09:19:18 +00:00
1 parent 800843d2ae
commit bda76ccff2
6 files changed
+1199

No files matched your search

@@ -0,0 +1,176 @@
using Titano.Core.Motion;
using Titano.Testing;
namespace Titano.Core.Tests.Motion;
public sealed class FourierTests
{
[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);
Assert.LessOrEqual(MaxError(r0, i0, re, im), 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 scale = double.Sqrt(n);
Assert.LessOrEqual(MaxError(er, ei, re, im) / scale, 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;
}
}