フーリエ変換とゼルニケ波面を用いたフラウンホーファーPSF生成を実装

このコミットが含まれているのは:
routersys committed 2026-06-23 08:40:38 +09:00
1 親 235d83b6bd
コミット 1cc18c8fe5
3ファイル変更
+265

検索条件に一致するファイルはありません

+92
ファイルの表示
@@ -0,0 +1,92 @@
using System.Numerics;
namespace WaveOptics.Fourier;
internal static class FastFourierTransform
{
public static void Forward(Span<Complex> values) => Transform(values, false);
public static void Inverse(Span<Complex> values) => Transform(values, true);
public static void Forward2D(Complex[] values, int width, int height) => Transform2D(values, width, height, false);
public static void Inverse2D(Complex[] values, int width, int height) => Transform2D(values, width, height, true);
static void Transform(Span<Complex> values, bool inverse)
{
var count = values.Length;
if (count == 0 || !BitOperations.IsPow2((uint)count))
throw new ArgumentException(nameof(values));
var target = 0;
for (var source = 1; source < count; source++)
{
var bit = count >> 1;
while ((target & bit) != 0)
{
target ^= bit;
bit >>= 1;
}
target ^= bit;
if (source < target)
(values[source], values[target]) = (values[target], values[source]);
}
for (var length = 2; length <= count; length <<= 1)
{
var angle = (inverse ? 2d : -2d) * Math.PI / length;
var step = Complex.FromPolarCoordinates(1d, angle);
var half = length >> 1;
for (var offset = 0; offset < count; offset += length)
{
var factor = Complex.One;
for (var index = 0; index < half; index++)
{
var even = values[offset + index];
var odd = values[offset + index + half] * factor;
values[offset + index] = even + odd;
values[offset + index + half] = even - odd;
factor *= step;
}
}
}
if (!inverse)
return;
var scale = 1d / count;
for (var index = 0; index < count; index++)
values[index] *= scale;
}
static void Transform2D(Complex[] values, int width, int height, bool inverse)
{
ArgumentNullException.ThrowIfNull(values);
if (values.Length != width * height)
throw new ArgumentException(nameof(values));
if (!BitOperations.IsPow2((uint)width) || !BitOperations.IsPow2((uint)height))
throw new ArgumentException(nameof(width));
for (var y = 0; y < height; y++)
{
var row = values.AsSpan(y * width, width);
if (inverse)
Inverse(row);
else
Forward(row);
}
var column = new Complex[height];
for (var x = 0; x < width; x++)
{
for (var y = 0; y < height; y++)
column[y] = values[y * width + x];
if (inverse)
Inverse(column);
else
Forward(column);
for (var y = 0; y < height; y++)
values[y * width + x] = column[y];
}
}
}
+139
ファイルの表示
@@ -0,0 +1,139 @@
using System.Numerics;
using WaveOptics.Abstractions;
using WaveOptics.Fourier;
namespace WaveOptics.Optics;
public sealed class FraunhoferPsfGenerator : IPsfGenerator
{
const double TwoPi = 2 * Math.PI;
public OpticalApiVersion ApiVersion => OpticalApiVersion.Current;
public OpticalCapabilities Capabilities => OpticalCapabilities.MonochromaticPsf
| OpticalCapabilities.CircularAperture
| OpticalCapabilities.RegularPolygonAperture
| OpticalCapabilities.CentralObstruction
| OpticalCapabilities.ZernikeAberration
| OpticalCapabilities.DirectConvolution;
public PsfGenerationResult Generate(PsfDescriptor descriptor)
{
ArgumentNullException.ThrowIfNull(descriptor);
var gridSize = descriptor.PupilGridSize;
var pupilDiameter = descriptor.PupilDiameterSamples;
var pupil = new Complex[gridSize * gridSize];
var center = gridSize / 2;
var pupilRadius = pupilDiameter / 2d;
var rotation = descriptor.BladeRotationDegrees * Math.PI / 180d;
var openSampleCount = 0;
for (var y = 0; y < gridSize; y++)
{
var normalizedY = (y - center) / pupilRadius;
for (var x = 0; x < gridSize; x++)
{
var normalizedX = (x - center) / pupilRadius;
if (!IsInsideAperture(normalizedX, normalizedY, descriptor, rotation))
continue;
var waves = ZernikeWavefront.Evaluate(normalizedX, normalizedY, descriptor.Aberration);
pupil[y * gridSize + x] = Complex.FromPolarCoordinates(1d, TwoPi * waves);
openSampleCount++;
}
}
if (openSampleCount == 0)
throw new InvalidOperationException();
FastFourierTransform.Forward2D(pupil, gridSize, gridSize);
var intensity = new double[pupil.Length];
var fullEnergy = 0d;
for (var y = 0; y < gridSize; y++)
{
var sourceY = (y + center) % gridSize;
for (var x = 0; x < gridSize; x++)
{
var sourceX = (x + center) % gridSize;
var value = pupil[sourceY * gridSize + sourceX].Magnitude;
value *= value;
intensity[y * gridSize + x] = value;
fullEnergy += value;
}
}
if (!double.IsFinite(fullEnergy) || fullEnergy <= 0)
throw new InvalidOperationException();
var inverseEnergy = 1d / fullEnergy;
for (var index = 0; index < intensity.Length; index++)
intensity[index] *= inverseEnergy;
var wavelengthMicrometers = descriptor.WavelengthNanometers / 1000d;
var focalPlaneSamplePitch = wavelengthMicrometers * descriptor.FNumber * pupilDiameter / gridSize;
var kernel = new double[descriptor.KernelSize * descriptor.KernelSize];
var kernelRadius = descriptor.KernelSize / 2;
var unnormalizedKernelEnergy = 0d;
for (var y = 0; y < descriptor.KernelSize; y++)
{
var physicalY = (y - kernelRadius) * descriptor.SensorPixelPitchMicrometers;
var sampleY = center + physicalY / focalPlaneSamplePitch;
for (var x = 0; x < descriptor.KernelSize; x++)
{
var physicalX = (x - kernelRadius) * descriptor.SensorPixelPitchMicrometers;
var sampleX = center + physicalX / focalPlaneSamplePitch;
var value = SampleBilinear(intensity, gridSize, sampleX, sampleY);
kernel[y * descriptor.KernelSize + x] = value;
unnormalizedKernelEnergy += value;
}
}
if (!double.IsFinite(unnormalizedKernelEnergy) || unnormalizedKernelEnergy <= 0)
throw new InvalidOperationException();
var psfKernel = new PsfKernel(descriptor.KernelSize, kernel);
var diagnostics = new PsfDiagnostics(
openSampleCount,
focalPlaneSamplePitch,
unnormalizedKernelEnergy,
psfKernel.Values.Span.ToArray().Max());
return new PsfGenerationResult(psfKernel, diagnostics);
}
static bool IsInsideAperture(double x, double y, PsfDescriptor descriptor, double rotation)
{
var radiusSquared = x * x + y * y;
var obstructionSquared = descriptor.CentralObstructionRatio * descriptor.CentralObstructionRatio;
if (radiusSquared > 1d || radiusSquared < obstructionSquared)
return false;
if (descriptor.ApertureShape == ApertureShape.Circular)
return true;
var radius = Math.Sqrt(radiusSquared);
if (radius == 0)
return descriptor.CentralObstructionRatio == 0;
var sector = TwoPi / descriptor.BladeCount;
var angle = Math.Atan2(y, x) - rotation;
var folded = angle - sector * Math.Round(angle / sector);
var boundary = Math.Cos(Math.PI / descriptor.BladeCount) / Math.Cos(folded);
return radius <= boundary;
}
static double SampleBilinear(double[] values, int size, double x, double y)
{
if (x < 0 || y < 0 || x > size - 1 || y > size - 1)
return 0;
var x0 = (int)Math.Floor(x);
var y0 = (int)Math.Floor(y);
var x1 = Math.Min(x0 + 1, size - 1);
var y1 = Math.Min(y0 + 1, size - 1);
var tx = x - x0;
var ty = y - y0;
var top = values[y0 * size + x0] * (1 - tx) + values[y0 * size + x1] * tx;
var bottom = values[y1 * size + x0] * (1 - tx) + values[y1 * size + x1] * tx;
return top * (1 - ty) + bottom * ty;
}
}
+34
ファイルの表示
@@ -0,0 +1,34 @@
using WaveOptics.Abstractions;
namespace WaveOptics.Optics;
internal static class ZernikeWavefront
{
static readonly double Sqrt3 = Math.Sqrt(3);
static readonly double Sqrt5 = Math.Sqrt(5);
static readonly double Sqrt6 = Math.Sqrt(6);
static readonly double Sqrt8 = Math.Sqrt(8);
public static double Evaluate(double x, double y, WavefrontAberration aberration)
{
var radiusSquared = x * x + y * y;
var radiusFourth = radiusSquared * radiusSquared;
var defocus = Sqrt3 * (2 * radiusSquared - 1);
var astigmatismVertical = Sqrt6 * (x * x - y * y);
var astigmatismOblique = 2 * Sqrt6 * x * y;
var comaHorizontal = Sqrt8 * (3 * radiusSquared - 2) * x;
var comaVertical = Sqrt8 * (3 * radiusSquared - 2) * y;
var trefoilHorizontal = Sqrt8 * x * (x * x - 3 * y * y);
var trefoilVertical = Sqrt8 * y * (3 * x * x - y * y);
var spherical = Sqrt5 * (6 * radiusFourth - 6 * radiusSquared + 1);
return aberration.DefocusWaves * defocus
+ aberration.AstigmatismVerticalWaves * astigmatismVertical
+ aberration.AstigmatismObliqueWaves * astigmatismOblique
+ aberration.ComaHorizontalWaves * comaHorizontal
+ aberration.ComaVerticalWaves * comaVertical
+ aberration.TrefoilHorizontalWaves * trefoilHorizontal
+ aberration.TrefoilVerticalWaves * trefoilVertical
+ aberration.SphericalWaves * spherical;
}
}