diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/cosinetables.cpp | 36 | ||||
-rw-r--r-- | common/cosinetables.h | 22 | ||||
-rw-r--r-- | common/sinetables.cpp | 40 | ||||
-rw-r--r-- | common/sinetables.h | 16 |
4 files changed, 87 insertions, 27 deletions
diff --git a/common/cosinetables.cpp b/common/cosinetables.cpp index 4bee1f24ea..42c6e4e05a 100644 --- a/common/cosinetables.cpp +++ b/common/cosinetables.cpp @@ -32,17 +32,35 @@ CosineTable::CosineTable(int bitPrecision) { _bitPrecision = bitPrecision; - int m = 1 << _bitPrecision; - double freq = 2 * M_PI / m; - _table = new float[m / 2]; + _nPoints = 1 << _bitPrecision; + _radResolution = 2.0 * M_PI / _nPoints; + _refSize = _nPoints / 4; + _table = new float[_nPoints / 2]; - // Table contains cos(2*pi*i/m) for 0<=i<m/4, - // followed by 3m/4<=i<m - for (int i = 0; i <= m / 4; i++) - _table[i] = cos(i * freq); + // Table contains cos(2*pi*i/_nPoints) for 0<=i<=_nPoints/4, + // followed by 3_nPoints/4<=i<_nPoints + for (int i = 0; i <= _nPoints / 4; i++) + _table[i] = cos(i * _radResolution); - for (int i = 1; i < m / 4; i++) - _table[m / 2 - i] = _table[i]; + for (int i = 1; i < _nPoints / 4; i++) + _table[_nPoints / 2 - i] = _table[i]; +} + +float CosineTable::at(int index) const { + assert((index >= 0) && (index < _nPoints)); + if (index < _refSize) + // [0,pi/2) + return _table[index]; + if ((index > _refSize) && (index < 2 * _refSize)) + // (pi/2,pi) + return -_table[2 * _refSize - index]; + if ((index >= 2 * _refSize) && (index < 3 * _refSize)) + // [pi,3/2pi) + return -_table[index - 2 * _refSize]; + if ((index > 3 * _refSize) && (index < _nPoints)) + // (3/2pi,2pi) + return _table[_nPoints - index]; + return 0.0f; // cos(pi/2) and cos(3pi/2) = 0 } CosineTable::~CosineTable() { diff --git a/common/cosinetables.h b/common/cosinetables.h index 0ff01e40d6..c08bb4cddd 100644 --- a/common/cosinetables.h +++ b/common/cosinetables.h @@ -39,22 +39,32 @@ public: * Get pointer to table. * * This table contains 2^bitPrecision/2 entries. + * Prefer to use at() * The layout of this table is as follows: - * - Entries 0 up to (excluding) 2^bitPrecision/4: - * cos(0) till (excluding) cos(1/2*pi) - * - Entries 2^bitPrecision/4 up to (excluding) 2^bitPrecision/2: - * cos(3/2*pi) till (excluding) cos(2*pi) + * - Entries 0 up to (including) 2^bitPrecision/4: + * cos(0) till (including) cos(1/2*pi) + * - Entries (excluding) 2^bitPrecision/4 up to 2^bitPrecision/2: + * (excluding) cos(3/2*pi) till (excluding) cos(2*pi) */ const float *getTable() { return _table; } /** - * Get pointer to table + * Returns cos(2*pi * index / 2^bitPrecision ) + * Index must be in range [0,2^bitPrecision-1] */ - int getPrecision() { return _bitPrecision; } + float at(int index) const; + + /** + * Get bit precision + */ + int getBitPrecision() { return _bitPrecision; } private: float *_table; int _bitPrecision; + double _radResolution; // Smallest radian increment + int _refSize; // _nPoints / 4 + int _nPoints; // range of operator[] }; } // End of namespace Common diff --git a/common/sinetables.cpp b/common/sinetables.cpp index c01705e3dc..c85a7bf7cf 100644 --- a/common/sinetables.cpp +++ b/common/sinetables.cpp @@ -32,17 +32,39 @@ SineTable::SineTable(int bitPrecision) { _bitPrecision = bitPrecision; - int m = 1 << _bitPrecision; - double freq = 2 * M_PI / m; - _table = new float[m / 2]; + _nPoints = 1 << _bitPrecision; + _radResolution = 2.0 * M_PI / _nPoints; + _refSize = _nPoints / 4; + _table = new float[_nPoints / 2]; - // Table contains sin(2*pi*i/m) for 0<=i<m/4, - // followed by m/2<=i<3m/4 - for (int i = 0; i < m / 4; i++) - _table[i] = sin(i * freq); + // Table contains sin(2*pi*i/_nPoints) for 0<=i<_nPoints/4, + // followed by _nPoints/2<=i<3_nPoints/4 + for (int i = 0; i < _nPoints / 4; i++) + _table[i] = sin(i * _radResolution); - for (int i = 0; i < m / 4; i++) - _table[m / 4 + i] = -_table[i]; + for (int i = 0; i < _nPoints / 4; i++) + _table[_nPoints / 4 + i] = -_table[i]; + +} + +float SineTable::at(int index) const { + assert((index >= 0) && (index < _nPoints)); + if (index < _refSize) + // [0,pi/2) + return _table[index]; + if (index == _refSize) + // pi/2 + return 1.0f; // sin(pi/2) = 1.0 + if ((index > _refSize) && (index < 2 * _refSize)) + // (pi/2,pi) + return _table[2 * _refSize - index]; + if ((index >= 2 * _refSize) && (index < 3 * _refSize)) + // [pi,3/2pi) + return -_table[index - 2 * _refSize]; + if ((index > 3 * _refSize) && (index < _nPoints)) + // (3/2pi,2pi) + return -_table[_nPoints - index]; + return -1.0f; // sin(3pi/2) = -1.0 } SineTable::~SineTable() { diff --git a/common/sinetables.h b/common/sinetables.h index 67225c0bcb..f91592cf38 100644 --- a/common/sinetables.h +++ b/common/sinetables.h @@ -39,22 +39,32 @@ public: * Get pointer to table * * This table contains 2^bitPrecision/2 entries. + * Prefer to use at() * The layout of this table is as follows: * - Entries 0 up to (excluding) 2^bitPrecision/4: * sin(0) till (excluding) sin(1/2*pi) - * - Entries 2^bitPrecision/4 up to (excluding) 2^bitPrecision/2: + * - Entries 2^bitPrecision/4 up to 2^bitPrecision/2: * sin(pi) till (excluding) sin(3/2*pi) */ const float *getTable() { return _table; } /** - * Get pointer to table + * Returns sin(2*pi * index / 2^bitPrecision ) + * Index must be in range [0,2^bitPrecision-1] + */ + float at(int index) const; + + /** + * Get bit precision */ - int getPrecision() { return _bitPrecision; } + int getBitPrecision() { return _bitPrecision; } private: float *_table; int _bitPrecision; + double _radResolution; // Smallest radian increment + int _refSize; // _nPoints / 4 + int _nPoints; // range of operator[] }; } // End of namespace Common |