X 为对数的图(半对数图)上的插值
Interpolation on a graph where X is logarithmic (semi-log graph)
我正在尝试在数据由线段近似的图表上进行插值,当然,由于图表是对数的,所以这些不是线段,而是 exponentials/logarithms.
的片段
所以我需要找到一个函数,在给定线段坐标和 X 值的情况下插入新的 Y 值。
同类型示例图:
这是我目前失败的尝试:
public static float LogLinear(float x0, float x1, float y0, float y1, float x)
{
return LinearInterpolation(Math.Log(x0), Math.Log(x1), y0, y1, Math.Log(x));
}
public static float LinearInterpolation(float x0, float x1, float y0, float y1, float x)
{
float m = (x - x0) / (x1 - x0);
return y0 * (1 - m) + y1 * (m);
}
鉴于图表上的线已经足够插值,我需要沿着这条线(不是真正的线)进行插值。
我的第一次尝试是基于 Y 值是线性的想法,但是 X 值应该映射到不同的 X 以获得正确的 Y,当然它不起作用,因为如果我在点之间画一条线段
Point p1 = new Point(0.1, 20);
Point p2 = new Point(10,60);
然后对于 X = 1 我希望找到 Y = 40,但事实并非如此。
我也希望通过
反转斜率来得到结果
Point p1 = new Point(10, 20);
Point p2 = new Point(0.1,60);
我试过了,结果如你所料返回了 40。你确定你传递的参数顺序正确吗?
void Main()
{
Math.Log(.1f).Dump();
LogLinear(0.1f,10,20,60,1).Dump();
LogLinear(10,0.1f,20,60,1).Dump();
}
public static float LogLinear(float x0, float x1, float y0, float y1, float x)
{
return LinearInterpolation((float) Math.Log(x0), (float) Math.Log(x1), y0, y1, (float) Math.Log(x));
}
public static float LinearInterpolation(float x0, float x1, float y0, float y1, float x)
{
float m = (x - x0) / (x1 - x0);
return y0 * (1 - m) + y1 * (m);
}
我正在尝试在数据由线段近似的图表上进行插值,当然,由于图表是对数的,所以这些不是线段,而是 exponentials/logarithms.
的片段所以我需要找到一个函数,在给定线段坐标和 X 值的情况下插入新的 Y 值。
同类型示例图:
这是我目前失败的尝试:
public static float LogLinear(float x0, float x1, float y0, float y1, float x)
{
return LinearInterpolation(Math.Log(x0), Math.Log(x1), y0, y1, Math.Log(x));
}
public static float LinearInterpolation(float x0, float x1, float y0, float y1, float x)
{
float m = (x - x0) / (x1 - x0);
return y0 * (1 - m) + y1 * (m);
}
鉴于图表上的线已经足够插值,我需要沿着这条线(不是真正的线)进行插值。
我的第一次尝试是基于 Y 值是线性的想法,但是 X 值应该映射到不同的 X 以获得正确的 Y,当然它不起作用,因为如果我在点之间画一条线段
Point p1 = new Point(0.1, 20);
Point p2 = new Point(10,60);
然后对于 X = 1 我希望找到 Y = 40,但事实并非如此。
我也希望通过
反转斜率来得到结果Point p1 = new Point(10, 20);
Point p2 = new Point(0.1,60);
我试过了,结果如你所料返回了 40。你确定你传递的参数顺序正确吗?
void Main()
{
Math.Log(.1f).Dump();
LogLinear(0.1f,10,20,60,1).Dump();
LogLinear(10,0.1f,20,60,1).Dump();
}
public static float LogLinear(float x0, float x1, float y0, float y1, float x)
{
return LinearInterpolation((float) Math.Log(x0), (float) Math.Log(x1), y0, y1, (float) Math.Log(x));
}
public static float LinearInterpolation(float x0, float x1, float y0, float y1, float x)
{
float m = (x - x0) / (x1 - x0);
return y0 * (1 - m) + y1 * (m);
}