Marsyas  0.2
/home/gperciva/src/marsyas/src/marsyas/PeakRatio.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 "PeakRatio.h"
00020 
00021 using std::cout;
00022 using std::endl;
00023 
00024 using std::ostringstream;
00025 
00026 using namespace Marsyas;
00027 
00036 PeakRatio::PeakRatio(mrs_string name):MarSystem("PeakRatio", name)
00037 {
00038     //Add any specific controls needed by PeakRatio
00039     //(default controls all MarSystems should have
00040     //were already added by MarSystem::addControl(), 
00041     //called by :MarSystem(name) constructor).
00042     //If no specific controls are needed by a MarSystem
00043     //there is no need to implement and call this addControl()
00044     //method (see for e.g. Rms.cpp)
00045     //addControls();
00046 
00047 }
00048 
00049 PeakRatio::PeakRatio(const PeakRatio& a) : MarSystem(a)
00050 {
00051     // For any MarControlPtr in a MarSystem 
00052     // it is necessary to perform this getctrl 
00053     // in the copy constructor in order for cloning to work 
00054 
00055 }
00056 
00057 PeakRatio::~PeakRatio()
00058 {
00059 }
00060 
00061 MarSystem* 
00062 PeakRatio::clone() const 
00063 {
00064     return new PeakRatio(*this);
00065 }
00066 
00067 void 
00068 PeakRatio::addControls()
00069 {
00070     //Add specific controls needed by this MarSystem.
00071     //none used for PeakRatio
00072 }
00073 
00074 void
00075 PeakRatio::myUpdate(MarControlPtr sender)
00076 {
00077     MarSystem::myUpdate(sender);    
00078     mrs_string inObsNames = ctrl_inObsNames_->to<mrs_string>();
00079     
00080     setControl("mrs_natural/onSamples",  (mrs_natural)1);  
00081     setControl("mrs_natural/onObservations",  (mrs_natural)(ctrl_inObservations_->to<mrs_natural>()+2));  
00082 
00083     // Add Names of The Observations to the observation names.
00084     mrs_string inObsName = stringSplit(ctrl_inObsNames_->to<mrs_string>(), ",")[0];
00085     inObsNames+="Average_" + inObsName + ",Minimum_" + inObsName + ",";
00086 
00087     // Add prefix to the observation names.
00088     ctrl_onObsNames_->setValue(obsNamesAddPrefix(inObsNames, "PeakRatio_"), NOUPDATE);
00089     
00090     maxima_.stretch(inSamples_);
00091     minima_.stretch(inSamples_);
00092 
00093 }
00094 
00095 
00096 void 
00097 PeakRatio::myProcess(realvec& in, realvec& out)
00098 {   
00099     mrs_natural t,o;
00100 
00101 
00102 
00103     mrs_real max_ = -1.0 * DBL_MAX;
00104     mrs_real min_ = DBL_MAX;
00105     mrs_real avg_ = 0.0;
00106 
00107 
00108     for (t=0; t < inSamples_; t++)
00109     {
00110         for (o=0; o < inObservations_; o++)
00111         {
00112             out(o,t)=in(o,t); // copy everything from input to output
00113 
00114             if (in(o,t) > max_)
00115             {
00116                 max_ = in(o,t);
00117             }
00118             if (in(o,t) < min_)
00119             {
00120                 min_ = in(o,t);
00121             }
00122             avg_=avg_+in(o,t);
00123             
00124         }
00125         avg_=avg_/(inSamples_*inObservations_);
00126         maxima_(t)=max_;
00127         minima_(t)=min_;
00128     }
00129     mrs_real res1=0.0;
00130     mrs_real res2=0.0;
00131     
00132     for (t=0; t < inSamples_; t++)
00133     {
00134         //compute ratios
00135         if(minima_(t)!=0.0) res1=maxima_(t)/minima_(t);
00136         if(minima_(t)!=0.0) res2=maxima_(t)/avg_;
00137 
00138         //add the ratios to the output
00139         out(onObservations_-1,t)=res1;
00140         out(onObservations_-2,t)=res2;
00141     }
00142 }