コード例 #1
0
        /// <summary>
        /// This is the Wikipedia "Spline Interpolation" article example.
        /// </summary>
        private static void TestSplineOnWikipediaExample()
        {
            // Create the test points.
            float[] x = new float[] { -1.0f, 0.0f, 3.0f };
            float[] y = new float[] { 0.5f, 0.0f, 3.0f };

            Console.WriteLine("x: {0}", ArrayUtil.ToString(x));
            Console.WriteLine("y: {0}", ArrayUtil.ToString(y));

            // Create the upsampled X values to interpolate
            int n = 20;

            float[] xs       = new float[n];
            float   stepSize = (x[x.Length - 1] - x[0]) / (n - 1);

            for (int i = 0; i < n; i++)
            {
                xs[i] = x[0] + i * stepSize;
            }

            // Fit and eval
            CubicSpline spline = new CubicSpline();

            float[] ys = spline.FitAndEval(x, y, xs);

            Console.WriteLine("xs: {0}", ArrayUtil.ToString(xs));
            Console.WriteLine("ys: {0}", ArrayUtil.ToString(ys));

            // Plot
            string path = @"..\..\spline-wikipedia.png";

            PlotSplineSolution("Cubic Spline Interpolation - Wikipedia Example", x, y, xs, ys, path);

            // Try slope, spline is already computed at this point
            float[] slope     = spline.EvalSlope(xs);
            string  slopePath = @"..\..\spline-wikipedia-slope.png";

            PlotSplineSolution("Cubic Spline Interpolation - Wikipedia Example - Slope", x, y, xs, ys, slopePath, slope);
        }
コード例 #2
0
ファイル: CubicSpline.cs プロジェクト: RupeWard/AudioProject
        /// <summary>
        /// Fit the input x,y points using a 'geometric' strategy so that y does not have to be a single-valued
        /// function of x.
        /// </summary>
        /// <param name="x">Input x coordinates.</param>
        /// <param name="y">Input y coordinates, do not need to be a single-valued function of x.</param>
        /// <param name="nOutputPoints">How many output points to create.</param>
        /// <param name="xs">Output (interpolated) x values.</param>
        /// <param name="ys">Output (interpolated) y values.</param>
        public static void FitGeometric(float[] x, float[] y, int nOutputPoints, out float[] xs, out float[] ys)
        {
            // Compute distances
            int n = x.Length;

            float[] dists = new float[n];             // cumulative distance
            dists[0] = 0;
            float totalDist = 0;

            for (int i = 1; i < n; i++)
            {
                float dx   = x[i] - x[i - 1];
                float dy   = y[i] - y[i - 1];
                float dist = (float)Math.Sqrt(dx * dx + dy * dy);
                totalDist += dist;
                dists[i]   = totalDist;
            }

            // Create 'times' to interpolate to
            float dt = totalDist / (nOutputPoints - 1);

            float[] times = new float[nOutputPoints];
            times[0] = 0;

            for (int i = 1; i < nOutputPoints; i++)
            {
                times[i] = times[i - 1] + dt;
            }

            // Spline fit both x and y to times
            CubicSpline xSpline = new CubicSpline();

            xs = xSpline.FitAndEval(dists, x, times);

            CubicSpline ySpline = new CubicSpline();

            ys = ySpline.FitAndEval(dists, y, times);
        }
コード例 #3
0
        /// <summary>
        /// Static all-in-one method to fit the splines and evaluate at X coordinates.
        /// </summary>
        /// <param name="x">Input. X coordinates to fit.</param>
        /// <param name="y">Input. Y coordinates to fit.</param>
        /// <param name="xs">Input. X coordinates to evaluate the fitted curve at.</param>
        /// <param name="startSlope">Optional slope constraint for the first point. Single.NaN means no constraint.</param>
        /// <param name="endSlope">Optional slope constraint for the final point. Single.NaN means no constraint.</param>
        /// <param name="debug">Turn on console output. Default is false.</param>
        /// <returns>The computed y values for each xs.</returns>
        public static float[] Compute(float[] x, float[] y, float[] xs, float startSlope = float.NaN, float endSlope = float.NaN, bool debug = false)
        {
            CubicSpline spline = new CubicSpline();

            return(spline.FitAndEval(x, y, xs, startSlope, endSlope, debug));
        }
コード例 #4
0
ファイル: Arm.cs プロジェクト: bencms/Kinect_LynxMotion6
        public void setArm(float [] alpha, int time, SerialPort port)
        {
            string result;
            int servo;
            float[] angle = new float[6];
            float[] newMusecAngles;
            //sets all the servos of the arm

            //checks to see if the angles lie within the limits
            for (int i = 0; i < 6; i++)
            {
                if (alpha[i] < alphaLims[0, i])
                {
                    alpha[i] = alphaLims[0, i];
                }

                if (alpha[i] > alphaLims[2, i])
                {
                    alpha[i] = alphaLims[2, i];
                }

            }

            //goes through each alpha converts it to musecs
            //use spline stuff.
            // Create the test points.
            for (int i = 0; i < 6; i++)
            {
                float[] x = new float[] { alphaLims[0, i],alphaLims[1,i], alphaLims[2,i]};
                float[] y = new float[] { musecLims[0, i], musecLims[1, i], musecLims[2, i]};

                float[] xs = new float[1];
                xs[0] = alpha[i];

                CubicSpline spline = new CubicSpline();
                newMusecAngles = spline.FitAndEval(x, y, xs, false);
                angle[i] = newMusecAngles[0];
                //string check = string.Format("{0}", newMusecAngles[0]);
                //Console.WriteLine(check);
                //Console.ReadLine();
            }
            //DEBUG
            //Console.WriteLine("Done conversion");
            //Console.Write("conversion check: ");
            //string check2 = string.Format("{0}", angle);
            //Console.WriteLine(check2);

            //cycles through all of the Angles sending them to the serial port one by one.
            for (int i = 0; i < 6; i++)
            {
                //angle = conversion

                servo = servoId[i];
                result = string.Format("#{0} P{1}  T{2} {3}", servo, angle[i], time, Environment.NewLine);
                port.Write(result);
            }

            //prints a statement when finished
            //Console.WriteLine("Done and dusted");
            //string name = Console.ReadLine();
        }
コード例 #5
0
ファイル: Kinect.cs プロジェクト: bencms/Kinect_LynxMotion6
        private double mapDist(double coordinate)
        {
            float[] x = new float[] { (float)kneexlims[0], (float)kneexlims[1], (float)kneexlims[2] };
             float[] y = new float[] { (float)finglims[0], (float)finglims[1], (float)finglims[2] };

             float[] xs = new float[1];
             xs[0] = (float)coordinate;

             CubicSpline spline = new CubicSpline();
             distance = spline.FitAndEval(x, y, xs, false);
             return distance[0];
        }
コード例 #6
0
ファイル: Kinect.cs プロジェクト: bencms/Kinect_LynxMotion6
        private double mapAngle(double coordinate)
        {
            float[] x = new float[] {(float)xslidelims[0], (float)xslidelims[1], (float)xslidelims[2] };
             float[] y = new float[] { (float)brotlims[0], (float)brotlims[1], (float)brotlims[2] };

             float[] xs = new float[1];
             xs[0] = (float)coordinate;

             CubicSpline spline = new CubicSpline();
             angle= spline.FitAndEval(x, y, xs, false);
             return angle[0];
        }
コード例 #7
0
        /// <summary>
        /// This is the Wikipedia "Spline Interpolation" article example.
        /// </summary>
        private static void TestSplineOnWikipediaExample()
        {
            //파일 읽고 -> 클래스에 파일 데이터(nm,n,k)저장 -> stringToSingle -> 계산 -> 그래프 그리기(Plot)
            //파일 입력
            string[] Silines  = File.ReadAllLines("C://SiN.txt", Encoding.Default);
            string[] WaveData = File.ReadAllLines("c://SiO2 2nm_on_Si.dat", Encoding.Default);


            List <WaveLengthData> waveLengthDatas = new List <WaveLengthData>();
            List <SiData>         ReadData        = new List <SiData>();
            List <SiNData>        SINDATA         = new List <SiNData>();
            List <SIO2Data>       sIO2Datas       = new List <SIO2Data>();


            //x,y 배열
            float ValX = 0.0f, ValY1 = 0.0f, ValY2 = 0.0f;

            //파일 읽기위해 여백 및 기타 제거
            char[] replace = { ' ', ',', '\t', '\n', Convert.ToChar(10), Convert.ToChar(13) };

            //파일 생성 및 데이터 add
            using (StreamWriter newdatFile = new StreamWriter(new FileStream("SiN_new.txt", FileMode.Create)))
            {
                //// Create the test points.
                //float[] x = new float[] { };
                //float[] yToN = new float[] { };
                //float[] yToK = new float[] { };

                foreach (var line in Silines)
                {
                    string[] splitData = line.Split(replace, StringSplitOptions.RemoveEmptyEntries);
                    if (ReadData.Count <= 1)
                    {
                        ReadData.Add(new SiData());
                    }
                    if (ReadData.Count > 1)
                    {
                        ReadData.Add(
                            new SiData
                        {
                            NM = splitData[0],
                            N  = splitData[1],
                            K  = splitData[2]
                        });
                    }
                }

                List <float> ListOfFloatNM = new List <float>();
                List <float> ListOfFloatN  = new List <float>();
                List <float> ListOfFloatK  = new List <float>();

                for (int i = 3; i < Silines.Length; i++)
                {
                    ValX  = float.Parse(ReadData[i].NM);
                    ValY1 = float.Parse(ReadData[i].N);
                    ValY2 = float.Parse(ReadData[i].K);


                    ListOfFloatNM.Add(ValX);
                    ListOfFloatN.Add(ValY1);
                    ListOfFloatK.Add(ValY2);
                }
                //ListOfFloatNM.RemoveRange(0, 3);
                //ListOfFloatN.RemoveRange(0, 3);
                //ListOfFloatK.RemoveRange(0, 3);

                float[] x    = ListOfFloatNM.ToArray();
                float[] yToN = ListOfFloatN.ToArray();
                float[] yToK = ListOfFloatK.ToArray();


                for (int i = 0; i < yToN.Length; i++)
                {
                    x[i] = (float)Math.Round(x[i], 5);

                    yToN[i] = (float)Math.Round(yToN[i], 5);

                    yToK[i] = (float)Math.Round(yToK[i], 5);
                }



                // silineNum 1470
                // 배열의 길이는 1466

                int     n        = 2;
                float[] xs       = new float[n + 1];
                float   stepSize = (x[x.Length - 1] - x[0]) / (n - 1);
                for (int i = 0; i < n; i++)
                {
                    xs[i] = x[0] + i + stepSize;
                }

                //fit and eval
                CubicSpline spline = new CubicSpline();

                float[] ysToN = spline.FitAndEval(x, yToN, xs);
                float[] ysToK = spline.FitAndEval(x, yToN, xs);

                //plot
                string pathToN = "spline-Si-ToN.png";
                string pathToK = "spline-Si-ToK.png";

                PlotSplineSolution("DolargeSplineToN", x, yToN, xs, ysToN, pathToN);
                PlotSplineSolution("DolargeSplineToK", x, yToK, xs, ysToN, pathToK);
            }
        }