123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /******************************************************************************
- from url: http://www.mochima.com/articles/LUT/LUT.html
- File: LUT.H
- Author: Carlos Moreno
- Date: March 1999
- Description:
- Template class definitions for Look-Up Table objects (LUT_number to
- encapsulate LUT'd floating-point multiplications, and LUT_function
- to encapsulate LUT'd functions evaluation)
- ******************************************************************************/
- #ifndef __LUT_H__
- #define __LUT_H__
- /*****************************************************************************
- LUT_number definition
- Template parameters:
- TResult:
- type parameter indicating the data type of the result of the multiplication
- LBound:
- Lower bound for the integer operand (multiplier)
- UBound:
- Upper bound for the integer operand (multiplier)
- ScaleFactor:
- The result of the multiplication is scaled by this factor (in case you want
- to provide the result as an integer, you can reduce the effect of the
- rounding error by specifying a high scale factor)
- ******************************************************************************/
- template <int LBound = -128, int UBound = 127, class TResult = double,
- int ScaleFactor = 1>
- class LUT_number
- {
- public:
- explicit LUT_number (double val = 0)
- {
- lut = lut_array - LBound;
- for (int i = LBound; i <= UBound; i++)
- {
- lut[i] = i * ScaleFactor * val;
- }
- }
- const TResult & operator* (int val) const
- {
- return lut[val];
- }
- private:
- TResult lut_array[UBound - LBound + 1];
- TResult * lut;
- };
- template <int LBound, int UBound, class TResult, int ScaleFactor>
- inline const TResult & operator* (int val, const LUT_number<LBound,
- UBound, TResult, ScaleFactor> & coeff)
- {
- return coeff * val;
- }
- /*******************************************************************************
- LUT_function definition
- Template parameters:
- TResult:
- Type parameter indicating the data type of the return value of the function
- LBound:
- Lower bound for the integer argument
- UBound:
- Upper bound for the integer argument
- TArg:
- Type parameter indicating the data type of the parameter
- (argument) of the function used to "initialize" the LUT
- ******************************************************************************/
- template <int LBound = -128, int UBound = 127, class TResult = double,
- class TArg = double>
- class LUT_function
- {
- public:
- explicit LUT_function (TResult (*f) (TArg), double coeff = 1)
- {
- lut = lut_array - LBound;
- for (int i = LBound; i <= UBound; i++)
- {
- lut[i] = f(coeff * i);
- }
- }
-
- const TResult & operator()(int i) const
- {
- if (i<UBound) {
- if (i>LBound) {
- return lut[i];
- } else return lut[LBound];
- } else return lut[UBound];
- }
-
- private:
- TResult lut_array[UBound - LBound + 1];
- TResult * lut;
- };
- #endif
|