分段线性插值 C#代码

发布时间:2024-03-01 13:01

/// 
        /// 分段线性插值,将一组数插值为所需点数
        /// 
        /// 待插值的数据数组
        /// 插值点数
        /// 插值后的数据数组
        public static double[] Interpolation(double[] dataIn, int n)
        {
            double[] dataOut = new double[n];
            int lenIn = dataIn.Length;
            double[] a = new double[lenIn];
            double[] divIn = new double[lenIn];
            double[] divOut = new double[n];

            divIn[0] = 0;

            for (int i = 1; i < lenIn; i++)
            {
                divIn[i] = divIn[i - 1] + 1;
            }

            divOut[0] = 0;

            for (int i = 1; i < n; i++)
            {
                divOut[i] = divOut[i - 1] + lenIn / Convert.ToDouble(n);
            }
            int k = 0;
            for (int i = k; i < n; i++)
            {
                for (int j = 0; j < lenIn - 1; j++)
                {
                    if (divOut[i] >= divIn[j] && divOut[i] < divIn[j + 1])
                    {
                        dataOut[i] = (dataIn[j + 1] - dataIn[j]) * (divOut[i] - divIn[j]) / (divIn[j + 1] - divIn[j]) + dataIn[j];
                        k = i;
                    }
                }
            }

            return dataOut;
        }

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号