C语言中带范围的查找表

C语言中带范围的查找表,c,lookup-tables,adc,C,Lookup Tables,Adc,我有一个表,可以根据我需要用C实现的ADC(模拟-数字转换器)计数范围来计算温度。我不确定如何进行计算,因为它不是一个精确的值,而是在一个重叠的范围内 代码将接收从传感器读取的ADC整数值(第2-4列分别表示ADC低、ADC中心和ADC高),并输出温度(第1列)。这些值来自一份文件,我将其从电阻到电压转换为ADC。ADC值将是一个整数,需要在该范围内进行最佳拟合,我认为这意味着最接近中心值。它不需要非常精确,因为它是一个非常稳定的值(通常在370-350或25-26摄氏度之间),它将用于确定过热

我有一个表,可以根据我需要用C实现的ADC(模拟-数字转换器)计数范围来计算温度。我不确定如何进行计算,因为它不是一个精确的值,而是在一个重叠的范围内

代码将接收从传感器读取的ADC整数值(第2-4列分别表示ADC低、ADC中心和ADC高),并输出温度(第1列)。这些值来自一份文件,我将其从电阻到电压转换为ADC。ADC值将是一个整数,需要在该范围内进行最佳拟合,我认为这意味着最接近中心值。它不需要非常精确,因为它是一个非常稳定的值(通常在370-350或25-26摄氏度之间),它将用于确定过热(41摄氏度)

以下是表格中几个单元格的示例:

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
    }
}
struct范围{
低漂;
浮心;
高飘浮;
内部温度;
};
结构范围表{
结构范围*列表;
尺寸与长度;
}
//TODO:在这里读取文件
结构RangeTable*表=。。。
//注意:确保table.list在文件中以升序'center'值排序,或在加载后对数组使用排序函数。
/////////////
///在提供的表中查找值,并返回指向最佳匹配范围结构值的指针。
结构范围*查找(结构范围表格*表格,浮点adcReading){
//对'center'值进行二进制搜索,这将返回具有最接近'center'值的范围对象。
结构范围*closestCenter=binarySearch(表->列表,表->长度,adcReading);
if(closestCenter==NULL){
//错误条件。
出口(1);
}
如果(AdCredinglow | | AdCreding>closestCenter->high){
//超出范围
}
否则{
//在射程之内
}
}

当然,实现排序和二进制搜索功能取决于您。

这里有一些可行的代码。它假设映射表是内置的;如果您需要动态地阅读它,这需要一些修改过的代码,但在其他方面并不是一个基本问题

如a中所述,代码确定每个温度的整数ADC值,这样,如果该值大于极限,则使用原始ADC数据表中ADC中心值项之间的线性插值,温度为给定值。对相关温度的搜索是简单的线性搜索。如果需要,您可以使用二进制搜索,或者为合理的ADC值创建一个表,并将每个ADC值映射到一个温度(使用更多空间,但提供最快的查找)。但对于25-41ºC的范围,线性搜索的性能几乎不值得担心,除非你能证明这是一个巨大的瓶颈,特别是当正常搜索只需要查看列表开头的2或3个条目时(因此正常线性搜索可能比执行二进制搜索更好!)


你真的是说你必须有一个查找表吗?没有其他替代搜索算法?您是否想知道使用什么数据结构来存储查找表,或者如何实现一种查找方法,该方法接收电流读数(以安培为单位)并给出温度?因此,如果超精度不是问题,您可以计算25到26之间ADC中心值的中点,在26和27之间,…并将其四舍五入到最接近的整数。当读取输入整数时,你可以找到匹配的温度,因为你有一个边界值,其中较高的数字是25ºC,然后是26ºC,然后是27ºC,等等。我想你可以想象一下,玩模糊逻辑,将3个数字视为一个三角形,看看输入温度是25℃,26℃和27℃,然后得出一个复合温度,但这不太值得。建议/首选查找表。是的,我想知道数据结构(不是在文件中读取,而是在代码中将其作为结构),然后使用该值确定温度如果表从-40变为125,您会推荐一种不同的搜索方法来提高速度吗?或者您认为这也足够了?这取决于您是否面临CPU使用压力。在最坏的情况下,你会看到少于170个比较,温度为125或更高的线性搜索;对于二进制搜索,这将是9个比较(但是对于25-41范围内的线性和二进制,比较16和4)。是的,我可能会在更大的温度范围内使用二进制搜索,但这会带来一些额外的复杂性。不过,我怀疑这会给任何系统带来压力。话虽如此,另一个问题是访问内存-页面错误等。这取决于上下文!我可能不会将“原始ADC数据”表保存在内存中——我会预先计算它。即使您需要能够重新校准ADC,我也可能会让操作程序在启动和请求时从文件中读取ADC映射表,并让程序预计算(重新预计算)ADC映射。据推测,这样的重新校准很少见。另外,请注意,您需要一个自定义的二进制搜索函数。标准配置()查找精确匹配,但当ADC读数未显示在表中时,您需要能够返回一个值。(我还要提到原始ADC表中6个小数位数中的最后4个不太可能相关-因此选择了打印格式。我对任何小数位数都有保留,但肯定是最后4个-特别是因为ADC只报告整数值。)
#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;
}