CLAM-Development  1.4.0
InstantTunningEstimator.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2006 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef InstantTunningEstimator_hxx
23 #define InstantTunningEstimator_hxx
24 #include <list>
25 #include <vector>
26 #include <cmath>
27 
28 namespace Simac
29 {
30 
41 {
42 public:
43  typedef std::vector<std::pair<double, double> > PeakList;
44 private:
45  double _fasorX;
46  double _fasorY;
47  double _instantX;
48  double _instantY;
49  double _inertia;
50 public:
51  InstantTunningEstimator(double inertia=0.0)
52  : _inertia(inertia)
53  {
54  _fasorX=1.0;
55  _fasorY=0.0;
56  }
58  {
59  }
60  void setInertia(double inertia)
61  {
62  _inertia=inertia;
63  }
64  // TODO: This function is taken by S&R of the previous one, no test!!
65  void doIt(const std::vector<std::pair<double, double> >& peaks)
66  {
67  _fasorX*=_inertia;
68  _fasorY*=_inertia;
69  _instantX=0;
70  _instantY=0;
71  for (unsigned int peak=0; peak<peaks.size(); peak++)
72  {
73  double radiantTunning=peaks[peak].first*2*M_PI;
74  _instantX+=cos(radiantTunning)*peaks[peak].second;
75  _instantY+=sin(radiantTunning)*peaks[peak].second;
76  }
77  _fasorX += _instantX;
78  _fasorY += _instantY;
79  }
80  std::pair<double,double> output() const
81  {
82  double tunning=std::atan2(_fasorY,_fasorX)/2/M_PI;
83  double strength=std::sqrt(_fasorY*_fasorY+_fasorX*_fasorX);
84  return std::make_pair(tunning, strength);
85  }
86  std::pair<double,double> instantTunning() const
87  {
88  double tunning=std::atan2(_instantY,_instantX)/2/M_PI;
89  double strength=std::sqrt(_instantY*_instantY+_instantX*_instantX);
90  return std::make_pair(tunning, strength);
91  }
92 };
93 
94 } // namespace Simac
95 
96 #endif// InstantTunningEstimator_hxx
97