This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Lookup or array

Hi,

I have a non-linear (so I can't form a simple expression) transducer, I am reading a voltage from the ADC and now wish to do a lookup or search an array to retrieve its corresponding value.  It's not large but I'm unsure as to what if a lookup table is best nor how to implement it.  I've had a look around and can't find anything simple.

ADC = 1.45 (Vin)......Find its greater than 1.4000 so return 32

#DATA

TC,   Vin

25       1.9000

26        1.7700

28        1.6500

30        1.5100

32        1.4000....

  • Search algorithms is a rather broad field in computer science. The two most common are linear and binary. Wiki has good articles on both

    http://en.wikipedia.org/wiki/Linear_search
    http://en.wikipedia.org/wiki/Binary_search_algorithm

    For small amounts of data, linear is faster. For large amounts of data, binary is faster. I would guess that less than 50 would be considered small. Your platform may have library functions for search an array of values. I don't know what your level of programming experience. If you looking for some example code, here's a possible implementation of a linear search:

    typedef struct
    {
      int   tc;
      float vin;
    } TCVIN;

    /*Ascending by tc, descending by vin */
    const TCVIN tcvin[] =
    {
      {25,1.9000},
      {26,1.7700},
      {28,1.6500},
      {30,1.5100},
      {32,1.4000}
    };

    int lookup_tc(float key)
    {
      int i;
      int imax = sizeof(tcvin)/sizeof(TCVIN);

      for(i=0; i<imax; i++)
        if(tcvin[i].vin < key)
          return(tcvin[i].tc); /*Found*/

      return(tcvin[imax-1].tc);/*Not found use last entry? */
    }

    For speed, you should probably avoid using floating point operations and use the original ADC fixed point integer values.

     

  • Hi Norman,

    Many thanks, very clear answer. I will indeed program and search with ADC fixed point.  Thanks for the advise on size

    Regards.