Marsyas  0.5.0-beta1
/Users/jleben/code/marsyas/src/marsyas/marsystems/Flux.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2006 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 "Flux.h"
00020 #include "../common_source.h"
00021 
00022 using namespace std;
00023 using std::abs;
00024 
00025 using namespace Marsyas;
00026 
00027 Flux::Flux(mrs_string name):MarSystem("Flux",name)
00028 {
00029   addControls();
00030 
00031   diff_ = 0.0;
00032   flux_ = 0.0;
00033   max_ = 0.0;
00034   addToStabilizingDelay_ = 1;
00035 }
00036 
00037 Flux::Flux(const Flux& a) : MarSystem(a)
00038 {
00039   ctrl_reset_ = getctrl("mrs_bool/reset");
00040   ctrl_mode_ = getctrl("mrs_string/mode");
00041   addToStabilizingDelay_ = 1;
00042 }
00043 
00044 Flux::~Flux()
00045 {
00046 }
00047 
00048 MarSystem*
00049 Flux::clone() const
00050 {
00051   return new Flux(*this);
00052 }
00053 
00054 void
00055 Flux::addControls()
00056 {
00057   addctrl("mrs_string/mode", "marsyas", ctrl_mode_);
00058   // set this true initially to clear prevWindow_
00059   addctrl("mrs_bool/reset", true, ctrl_reset_);
00060   setctrlState("mrs_bool/reset", true);
00061 }
00062 
00063 void
00064 Flux::myUpdate(MarControlPtr sender)
00065 {
00066   (void) sender;  //suppress warning of unused parameter(s)
00067   MRSDIAG("Flux.cpp - Flux:myUpdate");
00068   ctrl_onSamples_->setValue(ctrl_inSamples_, NOUPDATE);
00069   if (ctrl_mode_->to<mrs_string>() == "multichannel") {
00070     ctrl_onObservations_->setValue(inObservations_, NOUPDATE);
00071     // need this to match
00072     prevWindow_.create(ctrl_onObservations_->to<mrs_natural>(),
00073                        ctrl_onSamples_->to<mrs_natural>());
00074   } else {
00075     ctrl_onObservations_->setValue((mrs_natural)1, NOUPDATE);
00076     prevWindow_.create(ctrl_inObservations_->to<mrs_natural>(),
00077                        ctrl_inSamples_->to<mrs_natural>());
00078   }
00079   ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00080   ctrl_onObsNames_->setValue("Flux_" + ctrl_inObsNames_->to<mrs_string>() , NOUPDATE);
00081 
00082   reset_ = ctrl_reset_->to<mrs_bool>();
00083 
00084 }
00085 
00086 void
00087 Flux::myProcess(realvec& in, realvec& out)
00088 {
00089   mrs_natural o,t;
00090   mrs_string mode = ctrl_mode_->to<mrs_string>();
00091 
00092   if (reset_) {
00093     prevWindow_.setval(0.0);
00094     // don't set this control to be false!
00095     // this is almost always linked to by another
00096     // control, so let that one do the false'ing!
00097     reset_ = false;
00098   }
00099 
00100   for (t = 0; t < inSamples_; t++)
00101   {
00102     if(mode == "marsyas")
00103     {
00104       flux_ = 0.0;
00105       diff_ = 0.0;
00106       max_ = 0.0;
00107       for(o = 1; o < inObservations_; ++o)
00108       {
00109         logtmp_ = log(in(o,t) + MINREAL);
00110         diff_ = pow((mrs_real)logtmp_  - prevWindow_(o,t), (mrs_real)2.0);
00111         if(diff_ > max_)
00112           max_ = diff_;
00113         flux_ += diff_;
00114 
00115         prevWindow_(o,t) = logtmp_;
00116       }
00117 
00118       if(max_ != 0.0)
00119         flux_ /= (max_ * inObservations_);
00120       else
00121         flux_ = 0.0;
00122 
00123       out(0,t) = flux_;
00124     }
00125     else if (mode == "Laroche2003")
00126     {
00127       flux_ = 0.0;
00128 
00129       for (o=1; o < inObservations_; o++)
00130       {
00131         mrs_real tmp;
00132         tmp = in(o,t) - prevWindow_(o,t);
00133         if (tmp >= 0)
00134           // flux_ += in(o,t);
00135           flux_ += tmp;
00136         prevWindow_(o,t) = in(o,t);
00137       }
00138 
00139       out(0,t) = flux_;
00140     }
00141     else if (mode == "multichannel")
00142     {
00143 #if 0
00144       cout<<"in"<<endl;
00145       for(o = 0 ; o < inObservations_; o++) {
00146         cout<<in(o,t)<<" ";
00147       }
00148       cout<<endl;
00149 #endif
00150       for(o = 0 ; o < inObservations_; o++) {
00151         mrs_real tmp = in(o,t) - prevWindow_(o,t);
00152         prevWindow_(o,t) = in(o,t);
00153         if (tmp < 0) {
00154           tmp = 0;
00155         }
00156         out(o, t) = tmp;
00157       }
00158 #if 0
00159       cout<<"out"<<endl;
00160       for(o = 0 ; o < inObservations_; o++) {
00161         cout<<out(o,t)<<" ";
00162       }
00163       cout<<endl;
00164 #endif
00165     }
00166 
00167     else if(mode=="DixonDAFX06")
00168     {
00169       flux_ = 0.0;
00170       //diff_ = 0.0;
00171       //max_ = 0.0;
00172 
00173       for(o = 1 ; o < inObservations_; o++)
00174       {
00175 
00176         //Simon's version
00177         mrs_real tmp = in(o,t)  - prevWindow_(o,t);
00178 
00179         diff_ = (tmp+abs(tmp))/2;
00180 
00181         //lmartins version
00182         //diff_ = in(o,t)*in(o,t) - prevWindow_(o,t)*prevWindow_(o,t);
00183         //diff_ = (diff_+abs(diff_))/2.0;
00184 
00185         //if(diff_ > max_)
00186         //  max_ = diff_;
00187 
00188         flux_ += diff_;
00189         prevWindow_(o,t) = in(o,t);
00190 
00191         //from Juan's Matlab
00192         //          mrs_real tmp = pow(in(o,t), 2.0)  - pow(prevWindow_(o,t), 2.0);
00193         //          mrs_real tmp2 = (tmp+abs(tmp))/2;
00194         //          diff_ = sqrt(tmp2);
00195         //          if(diff_ > max_)
00196         //              max_ = diff_;
00197         //          flux_ += diff_;
00198         //          prevWindow_(o,t) = in(o,t);
00199       }
00200 
00201       //Normalizing with max_ was a bad idea
00202       //        if(max_ != 0.0)
00203       //            flux_ /= (max_ * inObservations_);
00204       //        else
00205       //            flux_ = 0.0;
00206       //
00207       out(0,t) = flux_;
00208 
00209     }
00210   }
00211 
00212   //used for toy_with_onsets.m (DO NOT DELETE! - COMMENT INSTEAD)
00213   //MATLAB_PUT(out, "Flux_out");
00214   //MATLAB_EVAL("FluxTS = [FluxTS, Flux_out];");
00215   //MATLAB_EVAL("plot(FluxTS)");
00216 }