Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/ParallelMatrixWeight.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 "../common_source.h"
00020 #include "ParallelMatrixWeight.h"
00021 
00022 using std::ostringstream;
00023 using namespace Marsyas;
00024 
00025 //#define MTLB_DBG_LOG
00026 
00027 ParallelMatrixWeight::ParallelMatrixWeight(mrs_string name) : MarSystem("ParallelMatrixWeight", name)
00028 {
00030   // Default controls that all MarSystems should have (like "inSamples"
00031   // and "onObservations"), are already added by MarSystem::addControl(),
00032   // which is already called by the constructor MarSystem::MarSystem(name).
00033   // If no specific controls are needed by a MarSystem there is no need to
00034   // implement and call this addControl() method (see for e.g. Rms.cpp)
00035   addControls();
00036 }
00037 
00038 ParallelMatrixWeight::ParallelMatrixWeight(const ParallelMatrixWeight& a) : MarSystem(a)
00039 {
00040   // IMPORTANT!
00043   // Otherwise this would result in trying to deallocate them twice!
00044   ctrl_weights_ = getctrl("mrs_realvec/weights");
00045 }
00046 
00047 
00048 ParallelMatrixWeight::~ParallelMatrixWeight()
00049 {
00050 }
00051 
00052 MarSystem*
00053 ParallelMatrixWeight::clone() const
00054 {
00055   // Every MarSystem should do this.
00056   return new ParallelMatrixWeight(*this);
00057 }
00058 
00059 void
00060 ParallelMatrixWeight::addControls()
00061 {
00062   mrs_realvec tmp(1);
00063   tmp(0)    = 1;
00064 
00065   addctrl("mrs_realvec/weights", tmp, ctrl_weights_);
00066 }
00067 
00068 void
00069 ParallelMatrixWeight::myUpdate(MarControlPtr sender)
00070 {
00071   MRSDIAG("ParallelMatrixWeight.cpp - ParallelMatrixWeight:myUpdate");
00072 
00074   MarSystem::myUpdate(sender);
00075 }
00076 
00077 void
00078 ParallelMatrixWeight::myProcess(realvec& in, realvec& out)
00079 {
00080   mrs_realvec weights       = ctrl_weights_->to<mrs_realvec> ();
00081   mrs_natural       k,i,j,
00082                 numRows     = weights.getRows (),
00083                  numCols        = weights.getCols (),
00084                   intRows,
00085                   intCols;
00086 
00087   if (numRows == 0)
00088   {
00089     out.setval(0);
00090     return;
00091   }
00092 
00093   if (in.getRows () % numRows)
00094   {
00095     MRSWARN("ParallelMatrixWeight: dimension mismatch");
00096     MRSASSERT(false);
00097     out.setval(0);
00098     return;
00099   }
00100 
00101   intRows       = in.getRows () / numRows,
00102    intCols      = in.getCols ();
00103 
00104   out = in;
00105 
00106   if (numCols == 1)
00107   {
00108     for (k = 0; k < numRows; k++)
00109     {
00110       mrs_real  weight  = weights(k);
00111       for (i = 0; i < intRows; i++)
00112       {
00113         for (j = 0; j < intCols; j++)
00114         {
00115           out(k*intRows+i,j)    *= weight;
00116         }
00117       }
00118     }
00119   }
00120   else
00121   {
00122     if (in.getCols () % numCols || in.getRows () != numRows)
00123     {
00124       MRSWARN("ParallelMatrixWeight: dimension mismatch");
00125       MRSASSERT(false);
00126       out.setval(0);
00127       return;
00128     }
00129 
00130     out     *= weights;
00131   }
00132 #ifdef MARSYAS_MATLAB
00133 #ifdef MTLB_DBG_LOG
00134   MATLAB_PUT(in, "in");
00135   MATLAB_PUT(out, "out");
00136   MATLAB_PUT(weights, "weights");
00137   MATLAB_EVAL("figure(2);subplot(221),imagesc(in),colorbar;subplot(222),imagesc(out),colorbar;subplot(212),imagesc(weights),colorbar;");
00138 #endif
00139 #endif
00140 }