c@225: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@225: c@225: /* c@225: QM DSP Library c@225: c@225: Centre for Digital Music, Queen Mary, University of London. c@225: This file copyright 2006 Martin Gasser. c@309: c@309: This program is free software; you can redistribute it and/or c@309: modify it under the terms of the GNU General Public License as c@309: published by the Free Software Foundation; either version 2 of the c@309: License, or (at your option) any later version. See the file c@309: COPYING included with this distribution for more information. c@225: */ c@225: cannam@489: #ifndef QM_DSP_TONALESTIMATOR_H cannam@489: #define QM_DSP_TONALESTIMATOR_H c@225: c@225: #include c@225: #include c@225: #include c@225: #include c@225: c@225: class ChromaVector : public std::valarray c@225: { c@225: public: cannam@482: ChromaVector(size_t uSize = 12) : std::valarray() { cannam@482: resize(uSize, 0.0f); cannam@482: } cannam@482: cannam@482: virtual ~ChromaVector() {}; cannam@482: cannam@482: void printDebug() { cannam@482: for (int i = 0; i < int(size()); i++) { cannam@482: std::cout << (*this)[i] << ";"; cannam@482: } cannam@482: std::cout << std::endl; cannam@482: } cannam@482: cannam@482: void normalizeL1() { cannam@482: // normalize the chroma vector (L1 norm) cannam@482: double dSum = 0.0; cannam@482: cannam@482: for (size_t i = 0; i < 12; (dSum += std::abs((*this)[i++]))) ; cannam@482: for (size_t i = 0; i < 12; dSum > 0.0000001?((*this)[i] /= dSum):(*this)[i]=0.0, i++) ; cannam@482: } c@225: cannam@482: void clear() { c@299: for (size_t i = 0; i < 12; ++i) (*this)[i] = 0.0; c@299: } c@225: }; c@225: c@225: class TCSVector : public std::valarray c@225: { c@225: public: cannam@482: TCSVector() : std::valarray() { cannam@482: resize(6, 0.0f); cannam@482: } cannam@482: cannam@482: virtual ~TCSVector() {}; c@225: cannam@482: void printDebug() { cannam@482: for (int i = 0; i < int(size()); i++) { cannam@482: std::cout << (*this)[i] << ";"; cannam@482: } cannam@482: std::cout << std::endl; cannam@482: } cannam@482: cannam@482: double magnitude() const { cannam@482: double dMag = 0.0; cannam@482: cannam@482: for (size_t i = 0; i < 6; i++) { cannam@482: dMag += std::pow((*this)[i], 2.0); cannam@482: } cannam@482: cannam@482: return std::sqrt(dMag); cannam@482: } c@225: }; c@225: c@225: class TonalEstimator c@225: { c@225: public: cannam@482: TonalEstimator(); cannam@482: virtual ~TonalEstimator(); cannam@482: TCSVector transform2TCS(const ChromaVector& rVector); cannam@482: c@225: protected: cannam@482: std::valarray< std::valarray > m_Basis; c@225: }; c@225: c@225: #endif // _TONALESTIMATOR_