add queen mary DSP library
[ardour2.git] / libs / qm-dsp / dsp / tonal / TonalEstimator.h
blobf555ef9e3b7c6ec51ab4522d680793022ba29363
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
3 /*
4 QM DSP Library
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Martin Gasser.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
16 #ifndef _TONALESTIMATOR_
17 #define _TONALESTIMATOR_
20 #include <valarray>
21 #include <numeric>
22 #include <algorithm>
23 #include <iostream>
25 class ChromaVector : public std::valarray<double>
27 public:
28 ChromaVector(size_t uSize = 12) : std::valarray<double>()
29 { resize(uSize, 0.0f); }
31 virtual ~ChromaVector() {};
33 void printDebug()
35 for (int i = 0; i < size(); i++)
37 std::cout << (*this)[i] << ";";
40 std::cout << std::endl;
43 void normalizeL1()
45 // normalize the chroma vector (L1 norm)
46 double dSum = 0.0;
48 for (size_t i = 0; i < 12; (dSum += std::abs((*this)[i++]))) ;
49 for (size_t i = 0; i < 12; dSum > 0.0000001?((*this)[i] /= dSum):(*this)[i]=0.0, i++) ;
53 void clear()
55 for (size_t i = 0; i < 12; ++i) (*this)[i] = 0.0;
61 class TCSVector : public std::valarray<double>
63 public:
64 TCSVector() : std::valarray<double>()
65 { resize(6, 0.0f); }
67 virtual ~TCSVector() {};
69 void printDebug()
71 for (int i = 0; i < size(); i++)
73 std::cout << (*this)[i] << ";";
76 std::cout << std::endl;
79 double magnitude() const
81 double dMag = 0.0;
83 for (size_t i = 0; i < 6; i++)
85 dMag += std::pow((*this)[i], 2.0);
88 return std::sqrt(dMag);
95 class TonalEstimator
97 public:
98 TonalEstimator();
99 virtual ~TonalEstimator();
100 TCSVector transform2TCS(const ChromaVector& rVector);
101 protected:
102 std::valarray< std::valarray<double> > m_Basis;
105 #endif // _TONALESTIMATOR_