Mercurial > hg > qm-dsp
annotate dsp/tonal/TonalEstimator.h @ 255:9edaa3ce62e8
* Make MFCC able to accept already-FFT'd input, and simplify API a bit
* Add log power value to MFCC, restore windowing, and avoid some heap allocs
* In HMM, bail out of iteration if loglik hits NaN
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Fri, 18 Jan 2008 13:24:12 +0000 |
parents | 49844bc8a895 |
children | ba4076cb1be1 |
rev | line source |
---|---|
c@225 | 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ |
c@225 | 2 |
c@225 | 3 /* |
c@225 | 4 QM DSP Library |
c@225 | 5 |
c@225 | 6 Centre for Digital Music, Queen Mary, University of London. |
c@225 | 7 This file copyright 2006 Martin Gasser. |
c@225 | 8 All rights reserved. |
c@225 | 9 */ |
c@225 | 10 |
c@225 | 11 #ifndef _TONALESTIMATOR_ |
c@225 | 12 #define _TONALESTIMATOR_ |
c@225 | 13 |
c@225 | 14 |
c@225 | 15 #include <valarray> |
c@225 | 16 #include <numeric> |
c@225 | 17 #include <algorithm> |
c@225 | 18 #include <iostream> |
c@225 | 19 |
c@225 | 20 class ChromaVector : public std::valarray<double> |
c@225 | 21 { |
c@225 | 22 public: |
c@225 | 23 ChromaVector(size_t uSize = 12) : std::valarray<double>() |
c@225 | 24 { resize(uSize, 0.0f); } |
c@225 | 25 |
c@225 | 26 virtual ~ChromaVector() {}; |
c@225 | 27 |
c@225 | 28 void printDebug() |
c@225 | 29 { |
c@225 | 30 for (int i = 0; i < size(); i++) |
c@225 | 31 { |
c@225 | 32 std::cout << (*this)[i] << ";"; |
c@225 | 33 } |
c@225 | 34 |
c@225 | 35 std::cout << std::endl; |
c@225 | 36 } |
c@225 | 37 |
c@225 | 38 void normalizeL1() |
c@225 | 39 { |
c@225 | 40 // normalize the chroma vector (L1 norm) |
c@225 | 41 double dSum = 0.0; |
c@225 | 42 |
c@225 | 43 for (size_t i = 0; i < 12; (dSum += std::abs((*this)[i++]))); |
c@225 | 44 for (size_t i = 0; i < 12; dSum > 0.0000001?((*this)[i] /= dSum):(*this)[i]=0.0, i++); |
c@225 | 45 |
c@225 | 46 } |
c@225 | 47 |
c@225 | 48 }; |
c@225 | 49 |
c@225 | 50 class TCSVector : public std::valarray<double> |
c@225 | 51 { |
c@225 | 52 public: |
c@225 | 53 TCSVector() : std::valarray<double>() |
c@225 | 54 { resize(6, 0.0f); } |
c@225 | 55 |
c@225 | 56 virtual ~TCSVector() {}; |
c@225 | 57 |
c@225 | 58 void printDebug() |
c@225 | 59 { |
c@225 | 60 for (int i = 0; i < size(); i++) |
c@225 | 61 { |
c@225 | 62 std::cout << (*this)[i] << ";"; |
c@225 | 63 } |
c@225 | 64 |
c@225 | 65 std::cout << std::endl; |
c@225 | 66 } |
c@225 | 67 |
c@225 | 68 double magnitude() const |
c@225 | 69 { |
c@225 | 70 double dMag = 0.0; |
c@225 | 71 |
c@225 | 72 for (size_t i = 0; i < 6; i++) |
c@225 | 73 { |
c@225 | 74 dMag += std::pow((*this)[i], 2.0); |
c@225 | 75 } |
c@225 | 76 |
c@225 | 77 return std::sqrt(dMag); |
c@225 | 78 } |
c@225 | 79 |
c@225 | 80 }; |
c@225 | 81 |
c@225 | 82 |
c@225 | 83 |
c@225 | 84 class TonalEstimator |
c@225 | 85 { |
c@225 | 86 public: |
c@225 | 87 TonalEstimator(); |
c@225 | 88 virtual ~TonalEstimator(); |
c@225 | 89 TCSVector transform2TCS(const ChromaVector& rVector); |
c@225 | 90 protected: |
c@225 | 91 std::valarray< std::valarray<double> > m_Basis; |
c@225 | 92 }; |
c@225 | 93 |
c@225 | 94 #endif // _TONALESTIMATOR_ |