我有一个串行连接,可以从连接到J热电偶的微型计算机获取数据。微处理器发送一个从0到1023的数值,该数值与测量的毫伏成比例。来自串行的数据存储在一个不断更新的变量"Thm1“中。我的目标是计算温度,重新转换收到的数据,并在文本框中显示相同的数据。热电偶的输出是非线性的,因此我不能使用方程,我应该从一个以5度为步长给出毫伏/温度的表中读取数据,并在两个最接近的值之间积分接收到的值。
让我们假设1023对应于16,881 mV。因此,我有1023点,每个点都是0.01650 mV。我从serial 800接收到对应于0,016550 x 800 = 13,2012 mV的数据。看看这个表pyromation.com/downloads/data/emfj_c.pdf,左边的第一列,这个值在摄氏240度到250度之间。我可以在这两个点之间做一个线性积分。但是,我怎样才能找到这两点呢?有没有比使用一长串if和if else更好的方法呢?
请举例说明。
发布于 2013-05-03 04:53:52
你可以像这样做一个线性外推:
public static decimal ExtrapolateFrom(int f, int s, decimal f1, decimal s2, int value)
{
return (s2-f1)/((s-(decimal)f)/(value-(decimal)f))+f1;
}
public static decimal ExtrapolateFrom(List<Tuple<int, decimal>> table, int value)
{
if(table.Count < 2) throw new Exception("Not enough values to extrapolate from");
var result = table.Select((x, i) => new { x, i }).Where(x => x.x.Item1 >= value).Select(x => x.i).ToList();
var index = result.Any()? result.First() : table.Count-1;
if (index < 1) index = 1;
return ExtrapolateFrom(table[index - 1].Item1, table[index].Item1, table[index - 1].Item2,table[index].Item2, value);
}
private static void Main(string[] args)
{
var table = new List<Tuple<int, decimal>> ()
{
new Tuple<int, decimal>(0, 0.0M),
new Tuple<int, decimal>(100, 5.0M),
new Tuple<int, decimal>(200, 6.0M),
new Tuple<int, decimal>(300, 9.0M),
new Tuple<int, decimal>(500, 11.0M),
};
Console.WriteLine(ExtrapolateFrom(table, 50));
Console.WriteLine(ExtrapolateFrom(table, 400));
Console.WriteLine(ExtrapolateFrom(table, 600));
}获取表的ExtrapolateFrom执行以下操作:
https://stackoverflow.com/questions/16346661
复制相似问题