public void TestException() { float[] xValues = new float[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; float[] yValues = new float[] { 0, 4, 5, 3, 1, 2, 3, 7, 8, 9 }; // Setup scattered interpolation with radial basis functions. RadialBasisRegressionF rbr = new RadialBasisRegressionF(); rbr.BasisFunction = (x, i) => MathHelper.Gaussian(x, 1, 0, 100); // Use Gaussian function as basis function. for (int i = 0; i < xValues.Length; i++) rbr.Add(new Pair<VectorF, VectorF>(new VectorF(1, xValues[i]), new VectorF(1, yValues[i]))); rbr.Setup(); rbr.Compute(new VectorF(1, 0)); }
public void TestException() { float[] xValues = new float[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; float[] yValues = new float[] { 0, 4, 5, 3, 1, 2, 3, 7, 8, 9 }; // Setup scattered interpolation with radial basis functions. RadialBasisRegressionF rbr = new RadialBasisRegressionF(); rbr.BasisFunction = (x, i) => MathHelper.Gaussian(x, 1, 0, 100); // Use Gaussian function as basis function. for (int i = 0; i < xValues.Length; i++) { rbr.Add(new Pair <VectorF, VectorF>(new VectorF(1, xValues[i]), new VectorF(1, yValues[i]))); } rbr.Setup(); rbr.Compute(new VectorF(1, 0)); }
public void Test1() { float[] xValues = new float[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; float[] yValues = new float[] { 0, 4, 5, 3, 1, 2, 3, 7, 8, 9 }; // Setup scattered interpolation with radial basis functions. RadialBasisRegressionF rbr = new RadialBasisRegressionF(); rbr.BasisFunction = (x, i) => MathHelper.Gaussian(x, 1, 0, 0.5f); // Use Gaussian function as basis function. //rbr.BaseFunction = delegate(float x) { return x; }; // Use identity functions as basis function. for (int i = 0; i < xValues.Length; i++) rbr.Add(new Pair<VectorF, VectorF>(new VectorF(1, xValues[i]), new VectorF(1, yValues[i]))); rbr.Setup(); Assert.IsTrue(Numeric.AreEqual(yValues[0], rbr.Compute(new VectorF(1, 0))[0])); Assert.IsTrue(Numeric.AreEqual(yValues[2], rbr.Compute(new VectorF(1, 2))[0])); Assert.IsTrue(Numeric.AreEqual(yValues[3], rbr.Compute(new VectorF(1, 3))[0])); Assert.IsTrue(Numeric.AreEqual(yValues[4], rbr.Compute(new VectorF(1, 4))[0])); Assert.IsTrue(Numeric.AreEqual(yValues[8], rbr.Compute(new VectorF(1, 8))[0])); }
public void Test1() { float[] xValues = new float[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; float[] yValues = new float[] { 0, 4, 5, 3, 1, 2, 3, 7, 8, 9 }; // Setup scattered interpolation with radial basis functions. RadialBasisRegressionF rbr = new RadialBasisRegressionF(); rbr.BasisFunction = (x, i) => MathHelper.Gaussian(x, 1, 0, 0.5f); // Use Gaussian function as basis function. //rbr.BaseFunction = delegate(float x) { return x; }; // Use identity functions as basis function. for (int i = 0; i < xValues.Length; i++) { rbr.Add(new Pair <VectorF, VectorF>(new VectorF(1, xValues[i]), new VectorF(1, yValues[i]))); } rbr.Setup(); Assert.IsTrue(Numeric.AreEqual(yValues[0], rbr.Compute(new VectorF(1, 0))[0])); Assert.IsTrue(Numeric.AreEqual(yValues[2], rbr.Compute(new VectorF(1, 2))[0])); Assert.IsTrue(Numeric.AreEqual(yValues[3], rbr.Compute(new VectorF(1, 3))[0])); Assert.IsTrue(Numeric.AreEqual(yValues[4], rbr.Compute(new VectorF(1, 4))[0])); Assert.IsTrue(Numeric.AreEqual(yValues[8], rbr.Compute(new VectorF(1, 8))[0])); }
private void DoScatteredInterpolation() { // Create random points for a height field. // The height field is in the x/z plane. Height is along the y axis. var points = new List <Vector3>(); for (int i = 0; i < 9; i++) { float x = i * 10; float y = RandomHelper.Random.NextFloat(0, 20); float z = RandomHelper.Random.NextInteger(0, 44) * 2; points.Add(new Vector3(x, y, z)); } // Now we setup scattered interpolation. // The RadialBasisRegression class does one form of scattered interpolation: // Multiple regression analysis with radial basis functions. RadialBasisRegressionF rbf = new RadialBasisRegressionF(); // We must define a basis function which will be used to determine the influence // of each input data pair. The Gaussian bell curve is a good candidate for most // applications. // We set the standard deviation to 10. Choosing a higher standard deviation makes // the Gaussian bell curve wider and increases the influence of the data points. // If we choose a lower standard deviation, we limit the influence of the data points. rbf.BasisFunction = (x, i) => MathHelper.Gaussian(x, 1, 0, 10); // Feed the data points into the scattered interpolation instance. foreach (var point in points) { // For each random point we add a data pair (X, Y), where X is a 2D position // in the height field, and Y is the observed height. VectorF X = new VectorF(new[] { point.X, point.Z }); VectorF Y = new VectorF(new[] { point.Y }); var dataPair = new Pair <VectorF, VectorF>(X, Y); rbf.Add(dataPair); } // These were all data points. Now, we perform some precomputations. rbf.Setup(); // Finally, we create a height field. var heightField = new float[100, 100]; for (int x = 0; x < 100; x++) { for (int z = 0; z < 100; z++) { // The scattered interpolation instance can compute a height for // any 2D input vector. float y = rbf.Compute(new VectorF(new float[] { x, z }))[0]; heightField[x, z] = y; } } var debugRenderer = GraphicsScreen.DebugRenderer; debugRenderer.Clear(); // Draw the random data points. const float scale = 0.04f; Vector3 offset = new Vector3(-2, 0, -2); foreach (var point in points) { debugRenderer.DrawPoint(scale * point + offset, Color.Black, false); } // Draw the height field. const int stepSize = 2; for (int x = 0; x < 100; x += stepSize) { for (int z = 0; z < 100; z += stepSize) { float y0 = heightField[x, z]; if (x + stepSize < 100) { float y1 = heightField[x + stepSize, z]; debugRenderer.DrawLine( scale * new Vector3(x, y0, z) + offset, scale * new Vector3(x + stepSize, y1, z) + offset, Color.Black, false); } if (z + stepSize < 100) { float y2 = heightField[x, z + stepSize]; debugRenderer.DrawLine( scale * new Vector3(x, y0, z) + offset, scale * new Vector3(x, y2, z + stepSize) + offset, Color.Black, false); } } } }