SemitoneCenterFinder.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2006 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef SemitoneCenterFinder_hxx
00023 #define SemitoneCenterFinder_hxx
00024 #include <list>
00025 #include <vector>
00026 #include <cmath>
00027 
00028 namespace Simac
00029 {
00030 
00031 
00032 class SemitoneCenterFinder
00033 {
00034 public:
00035         typedef std::vector<std::pair<double, double> > PeakList;
00036 private:
00037         unsigned _histogramSize;
00038         unsigned * _histogram;
00039         unsigned _binsPerSemitone;
00040 public:
00041         SemitoneCenterFinder()
00042         {
00043                 _binsPerSemitone = 3;
00044                 _histogramSize = 30;
00045                 _histogram = new unsigned[_histogramSize];
00046                 for (unsigned int i=0; i<_histogramSize; i++)
00047                         _histogram[i]=0;
00048         }
00049         ~SemitoneCenterFinder()
00050         {
00051                 delete [] _histogram;
00052         }
00053         void doIt(unsigned int nPeaks, const double * peakPositions, const double * peakValues)
00054         {
00055                 for (unsigned int i=0; i<nPeaks; i++)
00056                 {
00057                         double semitonePosition = std::fmod(peakPositions[i],_binsPerSemitone);
00058                         unsigned histogramBin=semitonePosition*_histogramSize/_binsPerSemitone+0.5;
00059                         _histogram[histogramBin]++;
00060                 }
00061         }
00062         double output()
00063         {
00064                 unsigned maxPos=0;
00065                 unsigned maxOcurrences=0;
00066                 for (unsigned int i=0; i<_histogramSize; i++)
00067                 {
00068                         if (_histogram[i]<=maxOcurrences) continue;
00069                         maxOcurrences=_histogram[i];
00070                         maxPos=i;
00071                 }
00072                 return maxPos*_binsPerSemitone/float(_histogramSize);
00073         }
00074 };
00075 
00076 } // namespace Simac
00077 
00078 #endif// SemitoneCenterFinder_hxx
00079 
Generated by  doxygen 1.6.3