CLAM-Development  1.4.0
TabFunct.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 #ifndef __TABFUNCT__
22 #define __TABFUNCT__
23 
24 #include "DataTypes.hxx"
25 #include "Assert.hxx"
26 #include <vector>
27 
28 using std::vector;
29 
30 namespace CLAM
31 {
43 template <class OriginalFunction> class TabFunct : public OriginalFunction
44 {
45 private:
46  TabFunct() {};
47 public:
48  TData operator() (const TData arg) {
49 
50  CLAM_DEBUG_ASSERT(arg>=mLowerBound && arg<=mUpperBound, "Tablulated functor argument out of bound");
51 
52  int index = int((arg-mLowerBound) / mIncr);
53 
54  CLAM_DEBUG_ASSERT(index<=mTableSize-2, "Bad index calculation");
55 
56  TData x1 = mLowerBound+mIncr*index;
57  TData yIncr = mTable[index+1]-mTable[index];
58 
59  return mTable[index] + ((arg-x1) * yIncr) * mInvIncr;
60  }
61 
62 // constructor
63  TabFunct(const unsigned tableSize, const TData lowerBound, const TData upperBound) :
64  mLowerBound(lowerBound),
65  mUpperBound(upperBound),
66  mTableSize(tableSize)
67 
68  {
69  CLAM_ASSERT(tableSize>=2, "Tabulating a function with less than 2 points.");
70  CLAM_ASSERT(lowerBound < upperBound, "No interval left to calculate values");
71 
72  CalculateTable();
73  }
74 
75 private:
76 
77  void CalculateTable()
78  {
79  mTable.reserve(mTableSize);
80 
81  mIncr = (mUpperBound - mLowerBound) / (mTableSize-1);
82  mInvIncr = 1/mIncr;
83 
84  TData arg = mLowerBound;
85 
86  for (int i=0; i<=mTableSize-1; arg+=mIncr, i++)
87  mTable[i] = OriginalFunction::operator() (arg);
88 
89  }
90  // internal table
91  vector<TData> mTable;
92  TData mLowerBound;
93  TData mUpperBound;
94  unsigned mTableSize;
95  TData mIncr, mInvIncr;
96 
97 };
98 
99 } //namespace
100 
101 #endif // TabFunct.hxx
102