- 首页 > 睿知 > it技术 > >
///
/// 分段线性插值,将一组数插值为所需点数
///
/// 待插值的数据数组
/// 【数值算法|分段线性插值 C#代码】插值点数
/// 插值后的数据数组
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;
}
推荐阅读