Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/HarmonicEnhancer.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 "HarmonicEnhancer.h"
00020 #include "../common_source.h"
00021 
00022 using std::ostringstream;
00023 using namespace Marsyas;
00024 
00025 
00026 HarmonicEnhancer::HarmonicEnhancer(mrs_string name):MarSystem("HarmonicEnhancer",name)
00027 {
00028   //type_ = "HarmonicEnhancer";
00029   //name_ = name;
00030 }
00031 
00032 HarmonicEnhancer::~HarmonicEnhancer()
00033 {
00034 }
00035 
00036 
00037 MarSystem*
00038 HarmonicEnhancer::clone() const
00039 {
00040   return new HarmonicEnhancer(*this);
00041 }
00042 
00043 
00044 void
00045 HarmonicEnhancer::myUpdate(MarControlPtr sender)
00046 {
00047   (void) sender;  //suppress warning of unused parameter(s)
00048   MRSDIAG("HarmonicEnhancer.cpp - HarmonicEnhancer:myUpdate");
00049 
00050   setctrl("mrs_natural/onSamples", (mrs_natural)1);
00051   setctrl("mrs_natural/onObservations", (mrs_natural)4);
00052   setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00053 
00054   flag_.create(getctrl("mrs_natural/inSamples")->to<mrs_natural>());
00055 
00056   setctrl("mrs_string/onObsNames", getctrl("mrs_string/inObsNames"));
00057 }
00058 
00059 void
00060 HarmonicEnhancer::harm_prob(mrs_real& pmax, mrs_real factor,
00061                             mrs_real& s1, mrs_natural& t1,
00062                             mrs_real& s2, mrs_natural& t2,
00063                             mrs_natural tmx,
00064                             mrs_natural size,
00065                             realvec& in)
00066 {
00067 
00068   mrs_natural index = (mrs_natural) floor(factor * tmx + 0.5);
00069 
00070 
00071 
00072   mrs_real prob;
00073   mrs_real c;
00074 
00075   if (index > 100.0)
00076     c = 1.0;
00077   else
00078     c = 0.75;
00079 
00080   mrs_real a;
00081 
00082   if ((tmx > 50)&&(tmx < 100))
00083     a = 1.5;
00084   else
00085     a = 0.75;
00086 
00087 
00088 
00089 
00090   if (index < size)
00091   {
00092     prob = a * in(0,tmx) + c *(in(0, index));
00093 
00094 
00095     if ((index-1 > 0) && (index-1 < size))
00096       prob += (c * in(0, index-1));
00097     if ((index+1 > 0) && (index+1 < size))
00098       prob += (c * in(0, index+1));
00099 
00100 
00101     if ((index-2 > 0) && (index-2 < size))
00102       prob += (c * in(0, index-2));
00103     if ((index+2 > 0) && (index+2 < size))
00104       prob += (c * in(0, index+2));
00105 
00106     if ((index-3 > 0) && (index-3 < size))
00107       prob += (c * in(0, index-3));
00108     if ((index+3 > 0) && (index+3 < size))
00109       prob += (c * in(0, index+3));
00110 
00111     if (index > 150.0)
00112     {
00113 
00114       if ((index-4 > 0) && (index-4 < size))
00115         prob += (c * in(0, index-4));
00116       if ((index+4 > 0) && (index+4 < size))
00117         prob += (c * in(0, index+4));
00118 
00119       if ((index-5 > 0) && (index-5 < size))
00120         prob += (c * in(0, index-5));
00121       if ((index+5 > 0) && (index+5 < size))
00122         prob += (c * in(0, index+5));
00123 
00124 
00125       if ((index-6 > 0) && (index-6 < size))
00126         prob += (c * in(0, index-6));
00127       if ((index+6 > 0) && (index+6 < size))
00128         prob += (c * in(0, index+6));
00129     }
00130 
00131 
00132     /* if (index > 150.0)
00133     {
00134 
00135     if ((index-7 > 0) && (index-7 < size))
00136     prob += (c * in(0, index-7));
00137     if ((index+7 > 0) && (index+7 < size))
00138     prob += (c * in(0, index+7));
00139 
00140     if ((index-8 > 0) && (index-8 < size))
00141     prob += (c * in(0, index-8));
00142     if ((index+8 > 0) && (index+8 < size))
00143     prob += (c * in(0, index+8));
00144 
00145 
00146     if ((index-9 > 0) && (index-9 < size))
00147     prob += (c * in(0, index-9));
00148     if ((index+9 > 0) && (index+9 < size))
00149     prob += (c * in(0, index+9));
00150     }
00151     */
00152 
00153 
00154   }
00155   else
00156     prob = 0;
00157 
00158 
00159 
00160   if (prob > pmax)
00161   {
00162     if (tmx < index)
00163     {
00164       s1 = in(0,tmx);
00165       s2 = in(0,index);
00166       if ((index-1 > 0) && (index-1 < size))
00167         s2 += in(0,index-1);
00168       if ((index+1 > 0) && (index+1 < size))
00169         s2 += in(0,index+1);
00170 
00171       if ((index-2 > 0) && (index-2 < size))
00172         s2 += in(0,index-2);
00173       if ((index+2 > 0) && (index+2 < size))
00174         s2 += in(0,index+2);
00175 
00176       if ((index-3 > 0) && (index-3 < size))
00177         s2 += in(0,index-3);
00178       if ((index+3 > 0) && (index+3 < size))
00179         s2 += in(0,index+3);
00180 
00181       t1 = tmx+1;
00182       t2 = (mrs_natural)(factor * t1);
00183       pmax = prob;
00184     }
00185     else
00186     {
00187       s1 = in(0,index);
00188       if ((index-1 > 0) && (index-1 < size))
00189         s1 += in(0,index-1);
00190       if ((index+1 > 0) && (index+1 < size))
00191         s1 += in(0,index+1);
00192 
00193       if ((index-2 > 0) && (index-2 < size))
00194         s1 += in(0,index-2);
00195       if ((index+2 > 0) && (index+2 < size))
00196         s1 += in(0,index+2);
00197 
00198       if ((index-3 > 0) && (index-3 < size))
00199         s1 += in(0,index-3);
00200       if ((index+3 > 0) && (index+3 < size))
00201         s1 += in(0,index+3);
00202 
00203 
00204 
00205       s2 = in(0,tmx);
00206       t1 = index+1;
00207       t2 = (mrs_natural)(factor * t1);
00208       pmax = prob;
00209     }
00210   }
00211 
00212 
00213 
00214 
00215 }
00216 
00217 
00218 
00219 void
00220 HarmonicEnhancer::myProcess(realvec& in, realvec& out)
00221 {
00222   //checkFlow(in,out);
00223 
00224   mrs_real mx = DBL_MIN;
00225   mrs_natural tmx  = 0;
00226   mrs_natural o,t,c;
00227 
00228 
00229   mrs_real pmax = DBL_MIN;
00230   mrs_natural t1;
00231   mrs_natural t2;
00232   mrs_real s1;
00233   mrs_real s2;
00234 
00235 
00236   flag_.setval(0.0);
00237 
00238   for (c=0; c < 3; ++c)
00239   {
00240 
00241     for (o=0; o < inObservations_; o++)
00242       for (t = 0; t < inSamples_; t++)
00243       {
00244         if ((in(o,t) > mx)&&(flag_(t) == 0.0)&&(t > 40) && (t < 120))
00245         {
00246           mx = in(o,t);
00247           tmx = t;
00248         }
00249       }
00250 
00251 
00252     flag_(tmx) = 1.0;
00253     mx = DBL_MIN;
00254 
00255 
00256 
00257     if (tmx < 120.0)
00258     {
00259       harm_prob(pmax, 2, s1, t1, s2, t2, tmx, inSamples_, in);
00260       harm_prob(pmax, 3.0, s1, t1, s2, t2, tmx, inSamples_, in);
00261     }
00262     else
00263     {
00264       harm_prob(pmax, 0.5, s1, t1, s2, t2, tmx, inSamples_, in);
00265       harm_prob(pmax, 0.33333, s1, t1, s2, t2, tmx, inSamples_, in);
00266     }
00267 
00268   }
00269 
00270   flag_.setval(0.0);
00271 
00272 
00273 
00274 
00275   out(0,0) = s1;
00276   out(1,0) = t1;
00277   out(2,0) = s2;
00278   out(3,0) = t2;
00279 
00280 
00281 
00282 
00283 
00284 
00285 }
00286 
00287 
00288 
00289 
00290 
00291 
00292 
00293 
00294 
00295