Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/PeakEnhancer.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2000 George Tzanetakis <gtzan@cs.princeton.edu>
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 
00029 #include "PeakEnhancer.h"
00030 #include "HalfWaveRectifier.h"
00031 #include "Filter.h"
00032 #include <marsyas/system/MarSystem.h>
00033 
00034 using std::ostringstream;
00035 using namespace Marsyas;
00036 
00037 
00038 PeakEnhancer::PeakEnhancer(mrs_string name):MarSystem("PeakEnhancer", name)
00039 {
00040   //Add any specific controls needed by PeakEnhancer
00041   //(default controls all MarSystems should have
00042   //were already added by MarSystem::addControl(),
00043   //called by :MarSystem(name) constructor).
00044   //If no specific controls are needed by a MarSystem
00045   //there is no need to implement and call this addControl()
00046   //method (see for e.g. Rms.cpp)
00047   addControls();
00048   rect_=NULL;
00049   lowpass_=NULL;
00050 }
00051 
00052 PeakEnhancer::PeakEnhancer(const PeakEnhancer& orig) : MarSystem(orig)
00053 {
00054   // For any MarControlPtr in a MarSystem
00055   // it is necessary to perform this getctrl
00056   // in the copy constructor in order for cloning to work
00057 
00058   ctrl_itnum_ = getctrl("mrs_natural/itnum");
00059   if (orig.rect_!=NULL) rect_=orig.rect_->clone();
00060   else rect_=NULL;
00061   if (orig.lowpass_!=NULL) lowpass_=orig.lowpass_->clone();
00062   else lowpass_=NULL;
00063 }
00064 
00065 PeakEnhancer::~PeakEnhancer()
00066 {
00067   if (rect_!=NULL) delete rect_;
00068   if (lowpass_!=NULL) delete lowpass_;
00069 }
00070 
00071 MarSystem*
00072 PeakEnhancer::clone() const
00073 {
00074   return new PeakEnhancer(*this);
00075 }
00076 
00077 void
00078 PeakEnhancer::addControls()
00079 {
00080   //Add specific controls needed by this MarSystem.
00081   addctrl("mrs_natural/itnum", (mrs_natural)1, ctrl_itnum_);
00082 }
00083 
00084 void
00085 PeakEnhancer::myUpdate(MarControlPtr sender)
00086 {
00087   MarSystem::myUpdate(sender);
00088   mrs_string inObsNames = ctrl_inObsNames_->to<mrs_string>();
00089 
00090   setControl("mrs_natural/onSamples",  (mrs_natural)(ctrl_inSamples_->to<mrs_natural>()));
00091   setControl("mrs_natural/onObservations",  (mrs_natural)(ctrl_inObservations_->to<mrs_natural>()));
00092 
00093   // Add Names of The Observations to the observation names.
00094   //inObsNames+="";
00095 
00096   // Add prefix to the observation names.
00097   //ctrl_onObsNames_->setValue(obsNamesAddPrefix(inObsNames, "PeakEnhancer_"), NOUPDATE);
00098 
00099   mrs_realvec num_low;
00100   num_low.create(3);
00101   mrs_realvec denom_low;
00102   denom_low.create(3);
00103   num_low(0)=0.1207f; num_low(1)=0.2415f; num_low(2)=0.1207f;
00104   denom_low(0)=1.0f; denom_low(1)=-0.8058f; denom_low(2)=0.2888f;
00105 
00106   if (rect_==NULL ) rect_=new HalfWaveRectifier("hwr");
00107   if (lowpass_==NULL) lowpass_=new Filter("lpf");
00108 
00109   lowpass_->setctrl("mrs_realvec/ncoeffs", num_low);
00110   lowpass_->setctrl("mrs_realvec/dcoeffs", denom_low);
00111 
00112   rect_->setControl("mrs_natural/inSamples",ctrl_inSamples_);
00113   rect_->setControl("mrs_natural/inObservations",ctrl_inObservations_);
00114 
00115   rect_->setControl("mrs_natural/onSamples",ctrl_onSamples_);
00116   rect_->setControl("mrs_natural/onObservations",ctrl_onObservations_);
00117 
00118   lowpass_->setControl("mrs_natural/inSamples",ctrl_inSamples_);
00119   lowpass_->setControl("mrs_natural/inObservations",ctrl_inObservations_);
00120 
00121   lowpass_->setControl("mrs_natural/onSamples",ctrl_onSamples_);
00122   lowpass_->setControl("mrs_natural/onObservations",ctrl_onObservations_);
00123 
00124 
00125 }
00126 
00127 
00128 void
00129 PeakEnhancer::myProcess(realvec& in, realvec& out)
00130 {
00131   for (mrs_natural o=0; o<inObservations_; o++)
00132   {
00133 
00134     mrs_realvec tempVec;
00135     tempVec.create(inSamples_);
00136     mrs_realvec& temp=tempVec;
00137     mrs_realvec temp2Vec;
00138     temp2Vec.create(inSamples_);
00139     mrs_realvec& temp2=temp2Vec;
00140     mrs_realvec temp3Vec;
00141     temp3Vec.create(inSamples_);
00142     mrs_realvec& temp3=temp3Vec;
00143 
00144     //half wave rectify
00145     rect_->process(in, temp);
00146 
00147     temp2.setval(0.0);
00148 
00149     //timestretching via resampling using upsampling and lowpass filteing (discarding (ctrl_itnum_-1)/ctrl_itnum_ of the data )
00150     for (mrs_natural i=0; i<inSamples_/ctrl_itnum_->to<mrs_natural>(); ++i)
00151       temp2(ctrl_itnum_->to<mrs_natural>()*i)=temp(i);
00152 
00153     lowpass_->process(temp2, temp3);
00154 
00155     for(mrs_natural i=0; i<inSamples_; ++i)
00156     {
00157       out(i)=in(o,i)-temp3(i);
00158     }
00159   }
00160 
00161 
00162 }