Exemple #1
0
        private static void TestSpline()
        {
            int n = 6;

            // Create the data to be fitted
            float[] x    = new float[n];
            float[] y    = new float[n];
            Random  rand = new Random(1);

            for (int i = 0; i < n; i++)
            {
                x[i] = i;
                y[i] = (float)rand.NextDouble() * 10;
            }

            // Compute the x values at which we will evaluate the spline.
            // Upsample the original data by a const factor.
            int upsampleFactor = 10;
            int nInterpolated  = n * upsampleFactor;

            float[] xs = new float[nInterpolated];

            for (int i = 0; i < nInterpolated; i++)
            {
                xs[i] = (float)i * (n - 1) / (float)(nInterpolated - 1);
            }

            float[] ys = CubicSpline.Compute(x, y, xs, 0.0f, Single.NaN, true);

            string path = @"..\..\testSpline.png";

            PlotSplineSolution("Cubic Spline Interpolation - Random Data", x, y, xs, ys, path);
        }
Exemple #2
0
        private static void TestPerf()
        {
            int n = 10000;

            // Create the data to be fitted
            float[] x    = new float[n];
            float[] y    = new float[n];
            Random  rand = new Random(1);

            for (int i = 0; i < n; i++)
            {
                x[i] = i;
                y[i] = (float)rand.NextDouble() * 10;
            }

            // Compute the x values that we will evaluate the spline at.
            // Upsample the original data by a const factor.
            int upsampleFactor = 10;
            int nInterpolate   = n * upsampleFactor;

            float[] xs = new float[nInterpolate];

            for (int i = 0; i < nInterpolate; i++)
            {
                xs[i] = (float)i / upsampleFactor;
            }

            // For perf, test multiple reps
            int      reps  = 100;
            DateTime start = DateTime.Now;

            for (int i = 0; i < reps; i++)
            {
                float[] ys = CubicSpline.Compute(x, y, xs);
            }

            TimeSpan duration = DateTime.Now - start;

            Console.WriteLine("CubicSpline upsample from {0:n0} to {1:n0} points took {2:0.00} ms for {3} iterations ({2:0.000} ms per iteration)",
                              n, nInterpolate, duration.TotalMilliseconds, reps, duration.TotalMilliseconds / reps);

            // Compare to NRinC
            //float[] y2 = new float[n];
            //float[] ys2 = new float[nInterpolate];
            //start = DateTime.Now;

            //for (int i = 0; i < reps; i++)
            //{
            //	CubicSplineNR.Spline(x, y, y2);
            //	CubicSplineNR.EvalSpline(x, y, y2, xs, ys2);
            //}

            //duration = DateTime.Now - start;
            //Console.WriteLine("CubicSplineNR upsample from {0:n0} to {1:n0} points took {2:0.00} ms for {3} iterations ({2:0.000} ms per iteration)",
            //	n, nInterpolate, duration.TotalMilliseconds, reps, duration.TotalMilliseconds / reps);
        }