qm-dsp
1.8
|
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 00002 00003 /* 00004 QM DSP Library 00005 00006 Centre for Digital Music, Queen Mary, University of London. 00007 This file copyright 2006 Martin Gasser. 00008 00009 This program is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU General Public License as 00011 published by the Free Software Foundation; either version 2 of the 00012 License, or (at your option) any later version. See the file 00013 COPYING included with this distribution for more information. 00014 */ 00015 00016 #ifndef _TONALESTIMATOR_ 00017 #define _TONALESTIMATOR_ 00018 00019 00020 #include <valarray> 00021 #include <numeric> 00022 #include <algorithm> 00023 #include <iostream> 00024 00025 class ChromaVector : public std::valarray<double> 00026 { 00027 public: 00028 ChromaVector(size_t uSize = 12) : std::valarray<double>() 00029 { resize(uSize, 0.0f); } 00030 00031 virtual ~ChromaVector() {}; 00032 00033 void printDebug() 00034 { 00035 for (int i = 0; i < size(); i++) 00036 { 00037 std::cout << (*this)[i] << ";"; 00038 } 00039 00040 std::cout << std::endl; 00041 } 00042 00043 void normalizeL1() 00044 { 00045 // normalize the chroma vector (L1 norm) 00046 double dSum = 0.0; 00047 00048 for (size_t i = 0; i < 12; (dSum += std::abs((*this)[i++]))) ; 00049 for (size_t i = 0; i < 12; dSum > 0.0000001?((*this)[i] /= dSum):(*this)[i]=0.0, i++) ; 00050 00051 } 00052 00053 void clear() 00054 { 00055 for (size_t i = 0; i < 12; ++i) (*this)[i] = 0.0; 00056 } 00057 00058 00059 }; 00060 00061 class TCSVector : public std::valarray<double> 00062 { 00063 public: 00064 TCSVector() : std::valarray<double>() 00065 { resize(6, 0.0f); } 00066 00067 virtual ~TCSVector() {}; 00068 00069 void printDebug() 00070 { 00071 for (int i = 0; i < size(); i++) 00072 { 00073 std::cout << (*this)[i] << ";"; 00074 } 00075 00076 std::cout << std::endl; 00077 } 00078 00079 double magnitude() const 00080 { 00081 double dMag = 0.0; 00082 00083 for (size_t i = 0; i < 6; i++) 00084 { 00085 dMag += std::pow((*this)[i], 2.0); 00086 } 00087 00088 return std::sqrt(dMag); 00089 } 00090 00091 }; 00092 00093 00094 00095 class TonalEstimator 00096 { 00097 public: 00098 TonalEstimator(); 00099 virtual ~TonalEstimator(); 00100 TCSVector transform2TCS(const ChromaVector& rVector); 00101 protected: 00102 std::valarray< std::valarray<double> > m_Basis; 00103 }; 00104 00105 #endif // _TONALESTIMATOR_