SemitoneCenterFinder.hxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 }
00077
00078 #endif// SemitoneCenterFinder_hxx
00079