开发者

Convert an unbound value to a bound value using an algorithm?

开发者 https://www.devze.com 2023-03-13 04:10 出处:网络
I have a device driver that uses the following algorithm to convert a value received from a sensor (no bound range) to a different value (bound range).

I have a device driver that uses the following algorithm to convert a value received from a sensor (no bound range) to a different value (bound range).

The sensor's values are usually within the 0-200 range but can exceed it, the maximum being about 4000 (this only happens when you use the sensor in an extreme way). I basically need a function that can do the following but without the giant if so it is more flexible.

It needs to take in the value, the step (in this case 20.0f) and the max output (in this case 10).

/* disregard the floating point numbers, I can cast them to int */
if (value <= 20.0f)
    return 0;
else if (value <= 40.0f)
    return 1;
else if (value <= 60.0f)
    return 2;
else if开发者_StackOverflow中文版 (value <= 80.0f)
    return 3;
else if (value <= 100.0f)
    return 4;
else if (value <= 120.0f)
    return 5;
else if (value <= 140.0f)
    return 6;
else if (value <= 160.0f)
    return 7;
else if (value <= 180.0f)
    return 8;
else if (value <= 190.0f)
    return 9;
else if (value >= 200.0f)
    return 10;

return 0;


int step(double value, int step, int maximum) { 
    return min(int(value / step), maximum);
}

Edit: As @DSM noted, this has a fencepost error, and should be something like:

int step(double value, int step, int maximum) { 
    return min(int((value-1) / step), maximum);
}


Supposing you have min, it looks like you want

min(0, (int)((min(220, value) - 20) / 20))

(Edited, sorry for the earlier min/max confusion!)


I would do something like this:

struct temp_thresholds {                                                                                                                                                                   
        float upper_bound;                                                                                                                                                                 
        int   ret_val;                                                                                                                                                                     
} thresholds = {                                                                                                                                                                           
        {  20.0f,     0},                                                                                                                                                                  
        {  40.0f,     1},                                                                                                                                                                  
        {  80.0f,     2},                                                                                                                                                                  
        // ..                                                                                                                                                                              
        { 190.0f,     9},                                                                                                                                                                  
        { 200.0f,     0}, // strange, and doesn't quite match your code                                                                                                                   
        { MAX_FLOAT, 10}, // find a constant like this                                                                                                                                     
};                                                                                                                                                                                         

int foo(float value)                                                                                                                                                                      
{                                                                                                                                                                                          
        // if the table were big enough, you could do binary search                                                                                                                        
        for (int i = 0; i < sizeof thresholds / sizeof thresholds[0]; ++i)                                                                                                                 
                if (value <= thresholds[i].upper_bound)                                                                                                                                    
                        return thresholds[i].ret_value;                                                                                                                                    
        assert(0);                                                                                                                                                                         
        return 0;                                                                                                                                                                          
}          
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号