Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/MaxArgMax.cpp
Go to the documentation of this file.
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 "MaxArgMax.h"
00020 #include <algorithm>
00021 
00022 using std::cout;
00023 using std::endl;
00024 
00025 using std::ostringstream;
00026 using std::max;
00027 using std::min;
00028 
00029 using namespace Marsyas;
00030 
00031 MaxArgMax::MaxArgMax(mrs_string name):MarSystem("MaxArgMax",name)
00032 {
00033   addControls();
00034 }
00035 
00036 MaxArgMax::~MaxArgMax()
00037 {
00038 }
00039 
00040 MarSystem*
00041 MaxArgMax::clone() const
00042 {
00043   return new MaxArgMax(*this);
00044 }
00045 
00046 void
00047 MaxArgMax::addControls()
00048 {
00049   addctrl("mrs_natural/nMaximums", (mrs_natural)1);
00050   setctrlState("mrs_natural/nMaximums", true);
00051 
00052   //Added to avoid Fanout Crash (if used within Fanout) - requires equal number of columns in each line!
00053   addctrl("mrs_natural/fanoutLength", (mrs_natural)1);
00054   setctrlState("mrs_natural/fanoutLength", true);
00055 
00056   addctrl("mrs_natural/interpolation", (mrs_natural)0);
00057 }
00058 
00059 void
00060 MaxArgMax::myUpdate(MarControlPtr sender)
00061 {
00062   (void) sender;  //suppress warning of unused parameter(s)
00063 
00064   mrs_natural k = getctrl("mrs_natural/nMaximums")->to<mrs_natural>();
00065   mrs_natural fanoutLength_ = getctrl("mrs_natural/fanoutLength")->to<mrs_natural>();
00066 
00067   //Added to avoid Fanout Crash (if used within Fanout) - requires equal number of columns in each line!
00068   mrs_natural size = 2 * max(k, fanoutLength_);
00069   setctrl("mrs_natural/onSamples",  size);
00070   setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations"));
00071   setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00072 
00073 
00074 }
00075 
00076 void quadraticInterpolation(mrs_real *ix, mrs_real *iy, realvec& data)
00077 {
00078 
00079   mrs_natural index = (mrs_natural) *ix;
00080   mrs_real d = (data(index-1)-data(index+1))/(2*(-2*data(index)+data(index-1)+data(index+1)));
00081   *ix += d;
00082   *iy -= d*(data(index-1)-data(index+1))/4;
00083 
00084 }
00085 
00086 
00087 void
00088 MaxArgMax::myProcess(realvec& in, realvec& out)
00089 {
00090   mrs_natural t,o;
00091   out.setval(MINREAL);
00092   mrs_natural k = getctrl("mrs_natural/nMaximums")->to<mrs_natural>();
00093 
00094   mrs_natural interpolationMode = getctrl("mrs_natural/interpolation")->to<mrs_natural>();
00095 
00096   for (o=0; o < inObservations_; o++)
00097   {
00098     for (t=0; t < inSamples_; t++)
00099     {
00100       mrs_real newmax = in(o,t);
00101       mrs_real newmax_i = t;
00102 
00103       //pair indexes = peak amplitude
00104       //odd indexes = peak argument
00105 
00106       for (ki=0; ki < k; ++ki)
00107       {
00108         if (newmax > out(o, 2*ki))
00109         {
00110           mrs_real oldmax = out(o, 2*ki);
00111           mrs_real oldmax_i = out(o,2*ki+1);
00112           out(o,2*ki) = newmax;
00113           out(o,2*ki+1) = newmax_i;
00114           newmax = oldmax;
00115           newmax_i = oldmax_i;
00116         }
00117       }
00118     }
00119     if(interpolationMode)
00120       for (ki=0; ki < k; ++ki)
00121       {
00122         mrs_real ix =  out(o,2*ki+1), iy =  out(o,2*ki);
00123 
00124 
00125         if ((ix != 0.0) &&  (iy != 0.0))
00126           quadraticInterpolation(&ix, &iy, in);
00127         out(o,2*ki) = iy;
00128         out(o,2*ki+1) = ix;
00129       }
00130   }
00131 
00132 
00133 
00134   //MATLAB_PUT(in, "Peaker_output");
00135   //MATLAB_PUT(out, "MaxPeaks");
00136 }