Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

tacLUTableBlock.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * E.S.O. - VLT project
00003 *
00004 * "@(#) $Id: tacLUTableBlock.h,v 1.34 2003/11/27 21:41:52 vltsccm Exp $"
00005 *
00006 * who       when      what
00007 * --------  --------  ----------------------------------------------
00008 * bbauvir   21/03/01  created
00009 */
00010 
00011 #ifndef TAC_LUTABLEBLOCK_H
00012 #define TAC_LUTABLEBLOCK_H
00013 
00014 /************************************************************************
00015  *  tacLUTableBlock.h - This file provides the interface to the Look-Up 
00016  *                      Table Block abstract class. Any specification of 
00017  *                      this class must include this header file.
00018  *
00019  *----------------------------------------------------------------------
00020  */
00021 
00022 /* 
00023  * System Headers
00024  */
00025 
00026 /* 
00027  * Local Headers
00028  */
00029 
00030 #include "tacStdBlock.h"
00031 
00032 /*
00033  * Constants
00034  */
00035 
00036 /*
00037  * The constructor expects the size of the function table (unsigned short)
00038  */
00039 
00040 #define tacLUTABLE_EXPECTED_PARAM 1    
00041 
00042 /*
00043  * Child function block is allowed to store parameters starting from this index
00044  */
00045 
00046 #define tacLUTABLE_FREE_PARAM_INDEX 1    
00047 
00048 /* 
00049  * Types
00050  */
00051 
00052 typedef struct {
00053   double x;
00054   double y;
00055 }tacLUTABLE_DOUBLET;
00056 
00057 typedef struct {
00058   unsigned short size;
00059   unsigned short index;
00060   tacLUTABLE_DOUBLET* function;
00061 }tacLUTABLE;
00062 
00063 /*
00064  * Macros
00065  */
00066 
00067 #define tacLUTableGetTable(pSelf) ((tacLUTABLE*) ((pSelf)->parameter))
00068 
00069 #define tacLUTableSetPoint(pTable, index, abs, ord) \
00070   {                                                 \
00071     pTable->function[index].x = abs;                \
00072     pTable->function[index].y = ord;                \
00073   }
00074 
00075 #define tacLUTableGetPoint(pTable, index, abs, ord) \
00076   {                                                 \
00077     abs = pTable->function[index].x;                \
00078     ord = pTable->function[index].y;                \
00079   }
00080 
00081 /*
00082  * Global variables
00083  */
00084 
00085 /*
00086  * Functions
00087  */
00088 
00089 /*
00090  * Note: Routines called from within the algorithm must be declared with
00091  *       static __inline__ <prototype>
00092  */
00093 
00094 STATUS tacLUTableBlockConstructor(tacSTDBLOCK* pSelf, tacSTDBLOCK_PARAM* parameter, tacERROR* error);
00095 STATUS tacLUTableBlockGetParameter(tacSTDBLOCK* pSelf, tacSTDBLOCK_PARAM* parameter, tacERROR* error);
00096 void tacLUTableBlockAlgorithm(tacSTDBLOCK* pSelf);
00097 void tacLUTableBlockDestructor(tacSTDBLOCK* pSelf);
00098 
00099 STATUS tacLUTableCreateTable(tacLUTABLE* pTable, unsigned short size);
00100 void tacLUTableRemoveTable(tacLUTABLE* pTable);
00101 
00102 /*
00103  * Note: This routine returns the index of the highest input of the 
00104  *       tabulated function inferior to the passed input value 
00105  *       See Numerical recipes in C (3.4 How to search an ordered table)
00106  */
00107 
00108 static __inline__ unsigned short tacLUTableHunt (tacLUTABLE* pTable, double input)
00109 {
00110   unsigned short increment;
00111   unsigned short lower, higher, middle;
00112 
00113   /*
00114    * Notes: - We make the assumption that the input points are sorted in 
00115    *          monotonically increasing order
00116    *        - Due to the slow dynamics of input signals, we use the last 
00117    *          index found as input guess
00118    *        - Bissection does not support input values out of the table
00119    *          boundaries
00120    */
00121 
00122   /* Set the hunting increment */
00123   increment = 1;
00124 
00125   /* Set the previous known position */
00126   lower = pTable->index;
00127 
00128   if (input >= pTable->function[lower].x)    /* Hunt upwards */
00129     {
00130       higher = lower + 1;
00131 
00132       while (input >= pTable->function[higher].x)
00133   {
00134     lower = higher;
00135     increment <<= 1;
00136     higher = lower + increment;
00137 
00138     if (higher >= pTable->size)    /* End of table */
00139       {
00140         higher = pTable->size;
00141         break;
00142       }
00143   }
00144     }
00145   else    /* Hunt downwards */
00146     {
00147       higher = lower;
00148       lower -= 1;
00149 
00150       while (input <= pTable->function[lower].x)
00151   {
00152     higher = lower;
00153     increment <<= 1;
00154     lower = higher - increment;
00155 
00156 #if 0    /* Cannot perform this test - lower is unsigned */
00157     if (lower <= 0)    /* End of table */
00158       {
00159         lower = 0;
00160         break;
00161       }
00162 #else
00163     if (increment >= higher)    /* End of table */
00164       {
00165         lower = 0;
00166         break;
00167       }
00168 #endif
00169   }
00170     }
00171 
00172   /*
00173    * Hunt is done ...
00174    * ... start bissection 
00175    */
00176 
00177   while ((higher - lower) != 1)
00178     {
00179       middle = (higher + lower) >> 1;
00180 
00181       if (input >= pTable->function[middle].x)
00182   {
00183     lower = middle;
00184   }
00185       else
00186   {
00187     higher = middle;
00188   }
00189     }
00190 
00191   /* Store found index for further use */
00192   pTable->index = lower;
00193 
00194   return lower;
00195 }
00196 
00197 static __inline__ double tacLUTableInterpolate (tacLUTABLE* pTable, double input)
00198 {
00199   unsigned short index = 0;
00200 
00201   double y0, y1;
00202   double x0, x1;
00203 
00204   if (input <= pTable->function[0].x)
00205     {
00206       return (pTable->function[0].y);
00207     }
00208   else if (input >= pTable->function[(pTable->size - 1)].x)
00209     {
00210       return (pTable->function[(pTable->size - 1)].y);
00211     }
00212   else
00213     {
00214       index = tacLUTableHunt(pTable, input);
00215 
00216       x0 = pTable->function[index].x;
00217       y0 = pTable->function[index].y;
00218       x1 = pTable->function[index+1].x;
00219       y1 = pTable->function[index+1].y;
00220 
00221       /* Linear interpolation */
00222       return (y0 + ((input - x0) / (x1 - x0) * (y1 - y0)));
00223     }
00224 }
00225 
00226 #endif 

Generated on Wed Dec 3 14:52:20 2003 for ATCS API by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001