在 C 中使用范围查找 Table
Lookup Table in C with Ranges
我有一个 table 可以根据我需要在 C 中实现的 ADC(模拟数字转换器)计数范围来计算温度。我不确定如何去做,因为它不会是一个精确的值,它会在一个重叠的范围内。
代码将接收从传感器读取的 ADC 整数值(第 2-4 列分别表示 ADC 低、ADC 中心和 ADC 高)并吐出温度(第 1 列)。这些值来自我从电阻转换为电压到 ADC 的文档。 ADC 值将是一个整数,它需要最适合该范围,我认为这意味着最接近中心值。它不需要非常精确,因为它是一个漂亮的 stable 值(通常在 370-350 又名 25-26C 之间),它将用于确定过热 (41C)。
下面是 table 的几个单元格的示例:
Temperature | ADC Low | ADC Center | ADC High
-------------+-------------+-------------+------------
25 | 362.1804923 | 372.3636364 | 382.4913871
26 | 349.9452011 | 359.9395371 | 369.8908548
27 | 338.1432261 | 347.9502029 | 357.7197732
28 | 326.7557813 | 336.3737597 | 345.9668118
29 | 315.7666703 | 325.2012277 | 334.6164426
30 | 305.1694416 | 314.4195099 | 323.6592884
31 | 294.9747625 | 304.0429113 | 313.1063265
如有任何帮助,我们将不胜感激。
我在想:使用二进制搜索遍历声明范围的结构数组:
struct Range {
float low;
float center;
float high;
int temperature;
};
struct RangeTable {
struct Range* list;
size_t length;
}
// TODO: Read the file here
struct RangeTable* table = ...
// NOTE: Ensure table.list is sorted in ascending `center` value, either in the file, or use a sorting function on the array after loading.
/////////////
/// <summary>Looks up the <paramref="adcReading" /> value in the provided table and returns a pointer to the best-matching Range struct value.</summary>
struct Range* lookup( struct RangeTable * table, float adcReading ) {
// do a binary-search on the `center` value, which will return the Range object with the closest `center` value.
struct Range* closestCenter = binarySearch( table->list, table->length, adcReading );
if( closestCenter == NULL ) {
// error condition.
exit( 1 );
}
if( adcReading < closestCenter->low || adcReading > closestCenter->high ) {
// out of range
}
else {
// inside the range
}
}
当然,排序和二进制搜索功能的实现取决于您。
这是一些可行的代码。它假定映射 table 是内置的;如果您需要动态阅读它,则需要修改一些代码,但不是根本问题。
如 中所述,代码为每个温度确定一个整数 ADC 值,如果该值大于限制,则温度为给定值,使用 ADC 中心之间的线性插值原始 ADC 数据中的值条目 table。搜索相关温度是一个简单的线性搜索。如果需要,您可以使用二进制搜索,或者为合理的 ADC 值创建一个 table,并将每个 ADC 值映射到一个温度(使用更多 space,但提供最快的查找)。但是对于 25-41ºC 的范围,几乎不值得担心线性搜索的性能,除非你能证明它是一个巨大的瓶颈,特别是因为正常搜索只需要在开始时查看 2 或 3 个条目列表的(所以正常的线性搜索可能会胜过二进制搜索!)。
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct raw_adc
{
int temp;
double adc_lo;
double adc_mid;
double adc_hi;
};
/* Assumed sorted in order of increasing temperature */
/* Assumed monotonic in order of adc_mid values in sorted order */
/* Assumed lower temperature has higher adc_mid value */
/* Assumed ADC readings always positive (0 used as dummy) */
/* Assume contiguous series of temperatures */
static struct raw_adc raw_adc_data[] =
{
{ 25, 362.1804923, 372.3636364, 382.4913871 },
{ 26, 349.9452011, 359.9395371, 369.8908548 },
{ 27, 338.1432261, 347.9502029, 357.7197732 },
{ 28, 326.7557813, 336.3737597, 345.9668118 },
{ 29, 315.7666703, 325.2012277, 334.6164426 },
{ 30, 305.1694416, 314.4195099, 323.6592884 },
{ 31, 294.9747625, 304.0429113, 313.1063265 },
};
enum { NUM_RAW_ADC = sizeof(raw_adc_data) / sizeof(raw_adc_data[0]) };
struct map_adc
{
int temp;
int adc_value;
};
static struct map_adc adc_map[NUM_RAW_ADC];
static void map_raw_adc_values(void)
{
int i;
for (i = 0; i < NUM_RAW_ADC - 1; i++)
{
adc_map[i].temp = raw_adc_data[i].temp;
/* Optionally add 0.5 before assigning */
adc_map[i].adc_value = (raw_adc_data[i].adc_mid + raw_adc_data[i+1].adc_mid) / 2;
}
/* Last value is deemed to be hotter than the last recorded value */
adc_map[i].temp = adc_map[i-1].temp + 1;
adc_map[i].adc_value = 0;
}
static int temp_from_adc(int adc_value)
{
int i;
for (i = 0; i < NUM_RAW_ADC; i++)
{
/* Use of > determined by inspection of data - colder temps have higher ADC value */
if (adc_value > adc_map[i].adc_value)
return adc_map[i].temp;
}
assert(0); /* Can't happen! */
return 300; /* If it gets here, the machine is too hot! */
}
static void dump_adc_map(void)
{
for (int i = 0; i < NUM_RAW_ADC; i++)
{
assert(raw_adc_data[i].temp == adc_map[i].temp);
printf("T = %.3dºC ADC = (%6.2f:%6.2f:%6.2f) Limit = %d\n",
raw_adc_data[i].temp, raw_adc_data[i].adc_lo,
raw_adc_data[i].adc_mid, raw_adc_data[i].adc_hi,
adc_map[i].adc_value);
}
}
int main(void)
{
map_raw_adc_values();
dump_adc_map();
srand(time(0));
for (int i = 0; i < 20; i++)
{
/* Range of ADC values in table is 294-382 */
/* Generate random value in that range */
int adc_rdg = rand() % (382 - 294) + 294;
printf("ADC: %d = %d ºC\n", adc_rdg, temp_from_adc(adc_rdg));
}
return 0;
}
示例运行:
T = 025ºC ADC = (362.18:372.36:382.49) Limit = 366
T = 026ºC ADC = (349.95:359.94:369.89) Limit = 353
T = 027ºC ADC = (338.14:347.95:357.72) Limit = 342
T = 028ºC ADC = (326.76:336.37:345.97) Limit = 330
T = 029ºC ADC = (315.77:325.20:334.62) Limit = 319
T = 030ºC ADC = (305.17:314.42:323.66) Limit = 309
T = 031ºC ADC = (294.97:304.04:313.11) Limit = 0
ADC: 298 = 31 ºC
ADC: 358 = 26 ºC
ADC: 343 = 27 ºC
ADC: 315 = 30 ºC
ADC: 358 = 26 ºC
ADC: 352 = 27 ºC
ADC: 374 = 25 ºC
ADC: 322 = 29 ºC
ADC: 372 = 25 ºC
ADC: 376 = 25 ºC
ADC: 333 = 28 ºC
ADC: 334 = 28 ºC
ADC: 356 = 26 ºC
ADC: 307 = 31 ºC
ADC: 304 = 31 ºC
ADC: 305 = 31 ºC
ADC: 324 = 29 ºC
ADC: 358 = 26 ºC
ADC: 324 = 29 ºC
ADC: 322 = 29 ºC
我有一个 table 可以根据我需要在 C 中实现的 ADC(模拟数字转换器)计数范围来计算温度。我不确定如何去做,因为它不会是一个精确的值,它会在一个重叠的范围内。
代码将接收从传感器读取的 ADC 整数值(第 2-4 列分别表示 ADC 低、ADC 中心和 ADC 高)并吐出温度(第 1 列)。这些值来自我从电阻转换为电压到 ADC 的文档。 ADC 值将是一个整数,它需要最适合该范围,我认为这意味着最接近中心值。它不需要非常精确,因为它是一个漂亮的 stable 值(通常在 370-350 又名 25-26C 之间),它将用于确定过热 (41C)。
下面是 table 的几个单元格的示例:
Temperature | ADC Low | ADC Center | ADC High
-------------+-------------+-------------+------------
25 | 362.1804923 | 372.3636364 | 382.4913871
26 | 349.9452011 | 359.9395371 | 369.8908548
27 | 338.1432261 | 347.9502029 | 357.7197732
28 | 326.7557813 | 336.3737597 | 345.9668118
29 | 315.7666703 | 325.2012277 | 334.6164426
30 | 305.1694416 | 314.4195099 | 323.6592884
31 | 294.9747625 | 304.0429113 | 313.1063265
如有任何帮助,我们将不胜感激。
我在想:使用二进制搜索遍历声明范围的结构数组:
struct Range {
float low;
float center;
float high;
int temperature;
};
struct RangeTable {
struct Range* list;
size_t length;
}
// TODO: Read the file here
struct RangeTable* table = ...
// NOTE: Ensure table.list is sorted in ascending `center` value, either in the file, or use a sorting function on the array after loading.
/////////////
/// <summary>Looks up the <paramref="adcReading" /> value in the provided table and returns a pointer to the best-matching Range struct value.</summary>
struct Range* lookup( struct RangeTable * table, float adcReading ) {
// do a binary-search on the `center` value, which will return the Range object with the closest `center` value.
struct Range* closestCenter = binarySearch( table->list, table->length, adcReading );
if( closestCenter == NULL ) {
// error condition.
exit( 1 );
}
if( adcReading < closestCenter->low || adcReading > closestCenter->high ) {
// out of range
}
else {
// inside the range
}
}
当然,排序和二进制搜索功能的实现取决于您。
这是一些可行的代码。它假定映射 table 是内置的;如果您需要动态阅读它,则需要修改一些代码,但不是根本问题。
如
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct raw_adc
{
int temp;
double adc_lo;
double adc_mid;
double adc_hi;
};
/* Assumed sorted in order of increasing temperature */
/* Assumed monotonic in order of adc_mid values in sorted order */
/* Assumed lower temperature has higher adc_mid value */
/* Assumed ADC readings always positive (0 used as dummy) */
/* Assume contiguous series of temperatures */
static struct raw_adc raw_adc_data[] =
{
{ 25, 362.1804923, 372.3636364, 382.4913871 },
{ 26, 349.9452011, 359.9395371, 369.8908548 },
{ 27, 338.1432261, 347.9502029, 357.7197732 },
{ 28, 326.7557813, 336.3737597, 345.9668118 },
{ 29, 315.7666703, 325.2012277, 334.6164426 },
{ 30, 305.1694416, 314.4195099, 323.6592884 },
{ 31, 294.9747625, 304.0429113, 313.1063265 },
};
enum { NUM_RAW_ADC = sizeof(raw_adc_data) / sizeof(raw_adc_data[0]) };
struct map_adc
{
int temp;
int adc_value;
};
static struct map_adc adc_map[NUM_RAW_ADC];
static void map_raw_adc_values(void)
{
int i;
for (i = 0; i < NUM_RAW_ADC - 1; i++)
{
adc_map[i].temp = raw_adc_data[i].temp;
/* Optionally add 0.5 before assigning */
adc_map[i].adc_value = (raw_adc_data[i].adc_mid + raw_adc_data[i+1].adc_mid) / 2;
}
/* Last value is deemed to be hotter than the last recorded value */
adc_map[i].temp = adc_map[i-1].temp + 1;
adc_map[i].adc_value = 0;
}
static int temp_from_adc(int adc_value)
{
int i;
for (i = 0; i < NUM_RAW_ADC; i++)
{
/* Use of > determined by inspection of data - colder temps have higher ADC value */
if (adc_value > adc_map[i].adc_value)
return adc_map[i].temp;
}
assert(0); /* Can't happen! */
return 300; /* If it gets here, the machine is too hot! */
}
static void dump_adc_map(void)
{
for (int i = 0; i < NUM_RAW_ADC; i++)
{
assert(raw_adc_data[i].temp == adc_map[i].temp);
printf("T = %.3dºC ADC = (%6.2f:%6.2f:%6.2f) Limit = %d\n",
raw_adc_data[i].temp, raw_adc_data[i].adc_lo,
raw_adc_data[i].adc_mid, raw_adc_data[i].adc_hi,
adc_map[i].adc_value);
}
}
int main(void)
{
map_raw_adc_values();
dump_adc_map();
srand(time(0));
for (int i = 0; i < 20; i++)
{
/* Range of ADC values in table is 294-382 */
/* Generate random value in that range */
int adc_rdg = rand() % (382 - 294) + 294;
printf("ADC: %d = %d ºC\n", adc_rdg, temp_from_adc(adc_rdg));
}
return 0;
}
示例运行:
T = 025ºC ADC = (362.18:372.36:382.49) Limit = 366
T = 026ºC ADC = (349.95:359.94:369.89) Limit = 353
T = 027ºC ADC = (338.14:347.95:357.72) Limit = 342
T = 028ºC ADC = (326.76:336.37:345.97) Limit = 330
T = 029ºC ADC = (315.77:325.20:334.62) Limit = 319
T = 030ºC ADC = (305.17:314.42:323.66) Limit = 309
T = 031ºC ADC = (294.97:304.04:313.11) Limit = 0
ADC: 298 = 31 ºC
ADC: 358 = 26 ºC
ADC: 343 = 27 ºC
ADC: 315 = 30 ºC
ADC: 358 = 26 ºC
ADC: 352 = 27 ºC
ADC: 374 = 25 ºC
ADC: 322 = 29 ºC
ADC: 372 = 25 ºC
ADC: 376 = 25 ºC
ADC: 333 = 28 ºC
ADC: 334 = 28 ºC
ADC: 356 = 26 ºC
ADC: 307 = 31 ºC
ADC: 304 = 31 ºC
ADC: 305 = 31 ºC
ADC: 324 = 29 ºC
ADC: 358 = 26 ºC
ADC: 324 = 29 ºC
ADC: 322 = 29 ºC