我遇到了内插数据文件的问题,例如,我已经将.csv转换为X数组和Y数组,其中X [0]对应于点Y [0]。C#线性插值
我需要在这些值之间进行插值以在最后给我一个平滑的文件。 我正在使用一个Picoscope输出这个函数,这意味着每一行的时间间隔都是相等的,所以只能使用Y值,这就是为什么当我看到我的代码时试图以一种奇怪的方式做到这一点。
的一种价值观它具有处理是:
X Y
0 0
2.5 0
2.5 12000
7.5 12000
7.5 3000
10 3000
10 6000
11 6625.254
12 7095.154
那么,2个Y值彼此相邻的是相同的他们,但之间的一条直线时,他们改变从X = 10像在这个例子中它将成为正弦波。
我一直在看插值等方程和其他帖子在这里,但我还没有做过这样的数学多年,可悲的是我无法想象出来,因为有2个未知数,我可以我不认为如何将它编程到C#中。
我有的是:
int a = 0, d = 0, q = 0;
bool up = false;
double times = 0, change = 0, points = 0, pointchange = 0;
double[] newy = new double[8192];
while (a < sizeoffile-1 && d < 8192)
{
Console.Write("...");
if (Y[a] == Y[a+1])//if the 2 values are the same add correct number of lines in to make it last the correct time
{
times = (X[a] - X[a + 1]);//number of repetitions
if ((X[a + 1] - X[a]) > times)//if that was a negative number try the other way around
times = (X[a + 1] - X[a]);//number of repetitions
do
{
newy[d] = Y[a];//add the values to the newy array which replaces y later on in the program
d++;//increment newy position
q++;//reduce number of reps in this loop
}
while (q < times + 1 && d < 8192);
q = 0;//reset reps
}
else if (Y[a] != Y[a + 1])//if the 2 values are not the same interpolate between them
{
change = (Y[a] - Y[a + 1]);//work out difference between the values
up = true;//the waveform is increasing
if ((Y[a + 1] - Y[a]) > change)//if that number was a negative try the other way around
{
change = (Y[a + 1] - Y[a]);//work out difference bwteen values
up = false;//the waveform is decreasing
}
points = (X[a] - X[a + 1]);//work out amount of time between given points
if ((X[a + 1] - X[a]) > points)//if that was a negative try other way around
points = (X[a + 1] - X[a]);///work out amount of time between given points
pointchange = (change/points);//calculate the amount per point in time the value changes by
if (points > 1)//any lower and the values cause errors
{
newy[d] = Y[a];//load first point
d++;
do
{
if (up == true)//
newy[d] = ((newy[d - 1]) + pointchange);
else if (up == false)
newy[d] = ((newy[d - 1]) - pointchange);
d++;
q++;
}
while (q < points + 1 && d < 8192);
q = 0;
}
else if (points != 0 && points > 0)
{
newy[d] = Y[a];//load first point
d++;
}
}
a++;
}
,这将创建一个紧密的波形,但它仍然是非常steppy。
所以任何人都可以看到为什么这不是很准确? 如何提高其准确性? 或使用数组做这个不同的方式?
为寻找谢谢:)
可能重复http://stackoverflow.com/questions/ 350852 /最小二乘法c-sharp-library) –