Пример #1
0
    private void GetSmoothValuesSubdivide(double[] xs, double[] ys, out List <double> xts, out List <double> yts)
    {
        const int Divisions = 256;

        xts = new List <double>();
        yts = new List <double>();

        if (xs.Length > 2)
        {
            var spline = CubicSpline.InterpolatePchipSorted(xs, ys);

            for (var i = 0; i < xs.Length - 1; i++)
            {
                var a     = xs[i];
                var b     = xs[i + 1];
                var range = b - a;
                var step  = range / Divisions;

                var x0 = xs[i];
                xts.Add(x0);
                var yt0 = spline.Interpolate(xs[i]);
                yts.Add(yt0);

                for (var xt = a + step; xt < b; xt += step)
                {
                    var yt = spline.Interpolate(xt);
                    xts.Add(xt);
                    yts.Add(yt);
                }
            }

            var xn = xs[^ 1];
Пример #2
0
    private static IEnumerable <(double x, double y)> SplineInterpolate(double[] xs, double[] ys)
    {
        const int Divisions = 256;

        if (xs.Length > 2)
        {
            var spline = CubicSpline.InterpolatePchipSorted(xs, ys);

            for (var i = 0; i < xs.Length - 1; i++)
            {
                var a     = xs[i];
                var b     = xs[i + 1];
                var range = b - a;
                var step  = range / Divisions;

                var t0 = xs[i];

                var xt0 = spline.Interpolate(xs[i]);

                yield return(t0, xt0);

                for (var t = a + step; t < b; t += step)
                {
                    var xt = spline.Interpolate(t);

                    yield return(t, xt);
                }
            }

            var tn  = xs[^ 1];
Пример #3
0
        public static void InterpolatePolyLine(double[] xs, double[] ys, int count, out ObservableCollection <double> xValues, out ObservableCollection <double> yValues)
        {
            var a      = xs.Min();
            var b      = xs.Max();
            var range  = b - a;
            var step   = range / count;
            var spline = CubicSpline.InterpolatePchipSorted(xs, ys);

            xValues = new ObservableCollection <double>();
            yValues = new ObservableCollection <double>();

            for (var i = 0; i < count; i++)
            {
                var x = a + i * step;
                var y = spline.Interpolate(x);
                xValues.Add(x);
                yValues.Add(y);
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            double[] x = new double[]
            {
                2,
                6,
                18,
                36,
                72,
                144,
                432,
                1008
            };

            double[] y = new double[]
            {
                123,
                117,
                107,
                92,
                33,
                32,
                8,
                7
            };

            Console.WriteLine($"X Y");
            for (int i = 0; i < x.Length; i++)
            {
                Console.WriteLine($"{x[i]} {y[i]}");
            }

            Console.WriteLine($"Interpolated:");

            //var spline = CubicSpline.InterpolateNaturalSorted(x, y);
            //var spline = CubicSpline.InterpolateAkimaSorted(x, y);
            var spline = CubicSpline.InterpolatePchipSorted(x, y);
            //var spline = LinearSpline.InterpolateSorted(x, y);
            //var spline = NevillePolynomialInterpolation.InterpolateSorted(x, y);
            //var spline = Barycentric.InterpolateRationalFloaterHormannSorted(x, y);
            //var spline = BulirschStoerRationalInterpolation.InterpolateSorted(x, y);
            //var spline = LogLinear.InterpolateSorted(x, y);
            //var spline = StepInterpolation.InterpolateSorted(x, y);

            var min = x.Min();
            var max = x.Max();

            for (double t = min; t <= max; t += 1)
            {
                double interpolated = spline.Interpolate(t);
                //Console.WriteLine($"{t} {interpolated}");
            }


            // var (a, b) = SimpleRegression.Fit(x, y);
            // for (double t = min; t <= max; t += 1)
            // {
            //     double interpolated = a + b * t;
            //     Console.WriteLine($"{t} {interpolated}");
            // }

            Test2();
        }