本文介绍了如何通过3个点/数以指定数量的样本进行插值? (在C#中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我们有1、5和10,并且想要在这些点之间插值12点,我们应该得到:
So for example we have 1, 5, and 10 and we want to interpolate between these with 12 points, we should get:
1.0000
1.7273
2.4545
3.1818
3.9091
4.6364
5.4545
6.3636
7.2727
8.1818
9.0909
10.0000
假设我们有5分,10分和4分,再有12分,我们应该得到:
say we have 5, 10, and 4 and again 12 points, we should get:
5.0000
5.9091
6.8182
7.7273
8.6364
9.5455
9.4545
8.3636
7.2727
6.1818
5.0909
4.0000
推荐答案
这是遵循以下原则的通用解决方案:
This is a generalized solution that works by these principles:
- 执行线性插值
- 它会计算输入数组中的浮点索引"
- 此索引用于从输入数组中选择1(如果小数部分非常接近0)或2个数字
- 此索引的整数部分是基本输入数组索引
- 小数部分表示我们应该向下一个数组元素移动多远
这应该可以与您需要的任何大小的输入数组和输出集合一起使用.
This should work with whatever size input arrays and output collections you would need.
public IEnumerable<double> Interpolate(double[] inputs, int count)
{
double maxCountForIndexCalculation = count - 1;
for (int index = 0; index < count; index++)
{
double floatingIndex = (index / maxCountForIndexCalculation) * (inputs.Length - 1);
int baseIndex = (int)floatingIndex;
double fraction = floatingIndex - baseIndex;
if (Math.Abs(fraction) < 1e-5)
yield return inputs[baseIndex];
else
{
double delta = inputs[baseIndex + 1] - inputs[baseIndex];
yield return inputs[baseIndex] + fraction * delta;
}
}
}
它会产生您在问题中显示的两个输出集合,但除此之外,我还没有对其进行测试.很少执行错误检查,因此您应该添加必要的位.
It produces the two collections of outputs you showed in your question but beyond that, I have not tested it. Little error checking is performed so you should add the necessary bits.
这篇关于如何通过3个点/数以指定数量的样本进行插值? (在C#中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!