Marsyas  0.2
/home/gperciva/src/marsyas/src/marsyas/SpectralCentroidBandNorm.cpp
00001 /*
00002 ** Copyright (C) 1998-2010 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "common.h" 
00020 #include "SpectralCentroidBandNorm.h"
00021 
00022 using std::ostringstream;
00023 using namespace Marsyas;
00024 
00025 SpectralCentroidBandNorm::SpectralCentroidBandNorm(mrs_string name) : MarSystem("SpectralCentroidBandNorm", name)
00026 {
00028     addControls();
00029 }
00030 
00031 SpectralCentroidBandNorm::SpectralCentroidBandNorm(const SpectralCentroidBandNorm& a) : MarSystem(a)
00032 {
00033 }
00034 
00035 
00036 SpectralCentroidBandNorm::~SpectralCentroidBandNorm()
00037 {
00038 }
00039 
00040 MarSystem*
00041 SpectralCentroidBandNorm::clone() const
00042 {
00043     return new SpectralCentroidBandNorm(*this);
00044 }
00045 
00046 void
00047 SpectralCentroidBandNorm::addControls()
00048 {
00050     addctrl("mrs_real/expected_peak", 100.0);
00051 }
00052 
00053 void
00054 SpectralCentroidBandNorm::myUpdate(MarControlPtr sender)
00055 {
00056     MRSDIAG("SpectralCentroidBandNorm.cpp - SpectralCentroidBandNorm:myUpdate");
00057 
00059     MarSystem::myUpdate(sender);
00060 
00061     //ctrl_onSamples_->setValue((mrs_natural)1, NOUPDATE);
00062     ctrl_onObservations_->setValue((mrs_natural)1, NOUPDATE);
00063     ctrl_onObsNames_->setValue("SCN_" + ctrl_inObsNames_->to<mrs_string>(),
00064         NOUPDATE);
00065 }
00066 
00067 void
00068 SpectralCentroidBandNorm::myProcess(realvec& in, realvec& out)
00069 {
00070     mrs_natural t,o;
00071     expected_peak_ = getctrl("mrs_real/expected_peak")->to<mrs_real>();
00072 
00074         for (t = 0; t < inSamples_; t++)
00075         {
00076             mrs_real m0 = 0.0;
00077             mrs_real m1 = 0.0;
00078             mrs_natural low_bin = 0.9*expected_peak_
00079                 / ((mrs_real) israte_);
00080             mrs_natural high_bin = 1.9*expected_peak_
00081                 / ((mrs_real) israte_);
00082             //printf("%li\t%li\n", low_bin, high_bin);
00083             for (o=low_bin; o < high_bin; o++)
00084             {
00085                     m1 += o * in(o,t);
00086                     m0 += in(o,t);
00087             }
00088             mrs_real sc = 0.0;
00089             if (m0 != 0.0) {
00090                 //sc = (m1 / m0) / (high_bin - low_bin);
00091                 sc = (m1 / m0) * israte_;
00092             }
00093             //printf("%li\t%li\t%.3f\n", low_bin, high_bin, sc);
00094             out(0, t) = fabs(expected_peak_ - sc) / expected_peak_;
00095             //printf("%.3f\t%.3f\t%.3f\n", expected_peak_, sc, out(0,t));
00096         }
00097 }