Esempio n. 1
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));
        }
Esempio n. 2
0
        public static string[] TesteMathNetInterpolation()
        {
            double DataPoints = 1000;

            // Create the data to be fitted
            //var x = new List<double> { 1, 8, 16, 25, 30 }; // 5 pontos

            var x = new List <double> {
                1, 2, 3, 4, 16, 30
            };                                                           // 5 pontos
            //var x = new List<double> { 1, 2, 3, 4, 5, 10, 15, 20, 25, 30 }; // 10 pontos

            var y = new List <double> {
                8.33, 9.25, 9.16, 8.43, 9.5, 9.14
            };
            //var y = new List<double> { 8.33, 9.37, 8.47, 9.32, 9.11, 9.04, 8.93, 9.49, 8.29, 8.88
            //var y = new List<double> { 10.255, 10.064, 9.961, 9.945, 9.930 , 9.37, 9.65, 9.95, 10.40, 10.88};
            //var y = new List<double> { 3, 3, 3, 3, 15, 40, 50, 60, 70, 80 };

            //Lembre-se sempre de modificar as variacoes de X e Y da plotagem

            var xAkima  = new List <double>();
            var yAkima  = new List <double>();
            var xLinear = new List <double>();
            var yLinear = new List <double>();

            /// Interpolação Linear
            var linearInterpolation = Interpolate.Linear(x.ToArray(), y.ToArray());

            /// Interpolação Polinomial
            //var PolinomialInterpolation = new NevillePolynomialInterpolation(x.ToArray(), y.ToArray());

            /// Interpolação Akima Spline
            var akimaInterpolation = CubicSpline.InterpolateAkima(x.ToArray(), y.ToArray());


            var ep = 0;
            var a  = x.Min();
            var b  = x.Max();

            #region Akima Interpolation
            for (int i = 0; i <= DataPoints; i++)
            {
                /// nomalizedForm = (b−a) (x−min / max − min) + a
                /// b = valor maximo do intervalo que os numeros devem ficar
                /// a = valor minimo do intervalo que os numeros devem ficar
                /// max = valor maximo do intervalo atual
                /// min = valor minimo do intervalo atual
                double normalized  = ((b + ep) - (a - ep)) * (i / DataPoints) + (a - ep);
                var    yInterpoled = akimaInterpolation.Interpolate(normalized);
                xAkima.Add(normalized);
                yAkima.Add(yInterpoled);
            }
            #endregion

            #region Linear Interpolation
            for (int i = 0; i <= DataPoints; i++)
            {
                double normalized  = ((b + ep) - (a - ep)) * (i / DataPoints) + (a - ep);
                var    yInterpoled = linearInterpolation.Interpolate(normalized);
                xLinear.Add(normalized);
                yLinear.Add(yInterpoled);
            }
            #endregion

            var pointsX = new List <double>();
            var pointSY = new List <double>();

            List <Point> logLinear = new List <Point>();
            List <Point> logAkima  = new List <Point>();

            #region Normalizar pontos dos periodos
            for (int i = 1; i <= 30; i++)
            {
                var cY = akimaInterpolation.Interpolate(i);

                pointsX.Add(i);
                pointSY.Add(cY);
                logAkima.Add(new Point(i, cY));
            }
            for (int i = 1; i <= 30; i++)
            {
                var cY = linearInterpolation.Interpolate(i);

                pointsX.Add(i);
                pointSY.Add(cY);
                logLinear.Add(new Point(i, cY));
            }
            #endregion

            //---plotar solução-- -
            PlotSolution(NameMethod,
                         x.ToArray(), y.ToArray(),
                         xAkima.ToArray(), yAkima.ToArray(),
                         xLinear.ToArray(), yLinear.ToArray(),
                         @"..\..\" + NameMethod + ".png");

            string[] output = new string[] { "", "" };
            foreach (Point p in logAkima)
            {
                output[0] += p.ToString() + "\n";
            }
            foreach (Point p in logLinear)
            {
                output[1] += p.ToString() + "\n";
            }

            return(output);
        }
Esempio n. 3
0
        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();
        }
Esempio n. 4
0
        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];
        }
Esempio n. 5
0
        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];
        }
Esempio n. 6
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);
            }
        }