public void Test1() { var d = new FastGaussianDistributionF(); Assert.AreEqual(0, d.ExpectedValue); Assert.AreEqual(1, d.StandardDeviation); var random = new Random(123); // Create 100 random values and check if they are valid. for (int i = 0; i < 100; i++) { float r = d.Next(random); Assert.IsTrue(-3 <= r); Assert.IsTrue(r <= 3); } d.ExpectedValue = 100; d.StandardDeviation = 10; for (int i = 0; i < 100; i++) { float r = d.Next(random); Assert.IsTrue(70f <= r); Assert.IsTrue(r <= 130); } }
public void Test1() { var d = new FastGaussianDistributionF(); Assert.AreEqual(0, d.ExpectedValue); Assert.AreEqual(1, d.StandardDeviation); var random = new Random(123); // Create 100 random values and check if they are valid. for (int i=0; i<100; i++) { float r = d.Next(random); Assert.IsTrue(-3 <= r); Assert.IsTrue(r <= 3); } d.ExpectedValue = 100; d.StandardDeviation = 10; for (int i = 0; i < 100; i++) { float r = d.Next(random); Assert.IsTrue(70f <= r); Assert.IsTrue(r <= 130); } }
// Creates texture containing h0. private void InitializeH0Spectrum(GraphicsDevice graphicsDevice) { // See also comments in InitializeCpuFft(). var n = TextureSize; var h0 = new Vector2F[(n + 1) * (n + 1)]; var random = new Random(Seed); var distribution = new FastGaussianDistributionF(0, 1); // TODO: Do not compute the inner parts of this h0 and _h0 in PerformCpuFft() twice. float oneOverSqrt2 = 1 / (float)Math.Sqrt(2); for (int i = 0; i <= n / 2; i++) { for (int x = -i; x <= i; x++) { for (int y = -i; y <= i; y++) { if (x > -i && x < i && y > -i && y < i) { y = i - 1; continue; } Vector2F xi = new Vector2F(distribution.Next(random), distribution.Next(random)); Vector2F k = new Vector2F(GetKx(x), GetKy(y)); //h0[GetIndex(x, n), GetIndex(y, n)] = h0[GetIndex(x, n) + (n + 1) * GetIndex(y, n)] = xi * (oneOverSqrt2 * (float)Math.Sqrt(GetPhillipsSpectrum(k))); } } } H0Spectrum.SafeDispose(); H0Spectrum = new Texture2D(graphicsDevice, n + 1, n + 1, false, SurfaceFormat.Vector2); H0Spectrum.SetData(h0); }
private void InitializeCpuFft() { var n = CpuSize; if (_h0 == null || _h0.GetLength(0) != n + 1) _h0 = new Vector2F[n + 1, n + 1]; var random = new Random(Seed); var distribution = new FastGaussianDistributionF(0, 1); // Create h0. float oneOverSqrt2 = 1 / (float)Math.Sqrt(2); // Instead of simply iterating over all elements of h0, we fill it in concentric // rectangles from the center to the border. This way, a simulation with N = 16 // will use the same frequencies as a simulation with N = 32 because the inner h0s // are initialized with the same random numbers!!! for (int i = 0; i <= n / 2; i++) { for (int x = -i; x <= i; x++) { for (int y = -i; y <= i; y++) { // Skip elements of the inner part which was already initialized. if (x > -i && x < i && y > -i && y < i) { y = i - 1; continue; } Vector2F xi = new Vector2F(distribution.Next(random), distribution.Next(random)); Vector2F k = new Vector2F(GetKx(x), GetKy(y)); _h0[GetIndex(x, n), GetIndex(y, n)] = xi * (oneOverSqrt2 * (float)Math.Sqrt(GetPhillipsSpectrum(k))); } } } if (_oneOverKLength == null || _oneOverKLength.GetLength(0) != n) { _oneOverKLength = new float[n, n]; _omega = new float[n, n]; _h = new Vector2F[n, n]; _N = new Vector2F[n, n]; _D = new Vector2F[n, n]; } // Create _oneOverKLength and _omega for (int x = -n / 2; x < n / 2; x++) { for (int y = -n / 2; y < n / 2; y++) { float length = new Vector2F(GetKx(x), GetKy(y)).Length; _omega[GetIndex(x, n), GetIndex(y, n)] = GetOmega(length); // Avoid division by zero. if (length < 1e-8f) length = 1e-8f; _oneOverKLength[GetIndex(x, n), GetIndex(y, n)] = 1 / length; } } }