Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/PeakInObservation.cpp
Go to the documentation of this file.
00001 #include "PeakInObservation.h"
00002 
00003 using std::ostringstream;
00004 using namespace Marsyas;
00005 
00006 PeakInObservation::PeakInObservation(mrs_string inName)
00007   :MarSystem("PeakInObservation",inName)
00008 {
00009   addControls();
00010 }
00011 
00012 PeakInObservation::PeakInObservation(const PeakInObservation& inToCopy)
00013   :MarSystem(inToCopy)
00014 {
00015   ctrl_HystLength_ = getctrl("mrs_natural/HystLength");
00016   ctrl_HystFactor_ = getctrl("mrs_real/HystFactor");
00017 
00018   HystLength_ = inToCopy.HystLength_;
00019   HystFactor_ = inToCopy.HystFactor_;
00020 }
00021 
00022 PeakInObservation::~PeakInObservation() {}
00023 
00024 MarSystem* PeakInObservation::clone() const
00025 {
00026   return new PeakInObservation(*this);
00027 }
00028 
00029 void PeakInObservation::addControls()
00030 {
00031   addctrl("mrs_natural/HystLength",10,ctrl_HystLength_);
00032   addctrl("mrs_real/HystFactor",2.f,ctrl_HystFactor_);
00033 
00034   ctrl_HystLength_->setState(true);
00035   ctrl_HystFactor_->setState(true);
00036 
00037   HystLength_ = 10;
00038   HystFactor_ = 2.f;
00039 }
00040 
00041 void PeakInObservation::myUpdate(MarControlPtr inSender)
00042 {
00043   MarSystem::myUpdate(inSender);
00044 
00045   if (ctrl_HystLength_->to<mrs_natural>() > 0 &&
00046       ctrl_HystFactor_->to<mrs_real>() > 1.f)
00047   {
00048     HystLength_ = ctrl_HystLength_->to<mrs_natural>();
00049     HystFactor_ = ctrl_HystFactor_->to<mrs_real>();
00050   } else
00051   {
00052     // Foutmelding
00053   }
00054 }
00055 
00056 void PeakInObservation::myProcess(realvec& inVec, realvec& outVec)
00057 {
00058   // (!!) Should be simplified
00059   outVec.setval(0.f);
00060 
00061   //int nmin = 0;
00062   mrs_real vmin = inVec(0);
00063   int nmax = 0;
00064   mrs_real vmax = inVec(0);
00065 
00066   int nthresh = 0;
00067   bool theValid = true;
00068   bool theMaxFlag = true;
00069 
00070   for (mrs_natural n = 1; n < inVec.getSize(); n++) {
00071     if (theMaxFlag)
00072       if (inVec(n) > vmax) {
00073         // Zone 1: [min hysteresis, max]
00074         vmax = inVec(n);
00075         nmax = n;
00076         nthresh = n;
00077         theValid = true;
00078 
00079         vmin = vmax;
00080         //nmin = nmax;
00081       } else {
00082         if (inVec(n)<vmax/HystFactor_ && nmax!=0) {
00083           // Zone 3: [max hysteresis, min]
00084 
00085           if ((mrs_natural)n > nthresh + HystLength_) {
00086             // Maximum was WIDE ENOUGH
00087             if (theValid) {
00088               outVec(nmax) = vmax;
00089               theMaxFlag = false;
00090             } else {
00091               //Search for new maximum
00092               vmax = inVec(n);
00093               nmax = n;
00094               nthresh = n;
00095               theValid = true;
00096 
00097               vmin = vmax;
00098               //nmin = nmax;
00099             }
00100 
00101           } else {
00102             // Maximum was TOO SMALL
00103             if (inVec(n) < vmin) {
00104               vmin = inVec(n);
00105               //nmin = n;
00106             }
00107           }
00108         } else {
00109           // Zone 2: [max, max hysteresis]
00110           if (nthresh != (mrs_natural)n-1) {
00111             theValid = false;
00112             if ((mrs_natural)n > nthresh + HystLength_) {
00113               // Search for new maximum
00114               vmax = inVec(n);
00115               nmax = n;
00116               nthresh = n;
00117               theValid = true;
00118 
00119               vmin = vmax;
00120               //nmin = nmax;
00121             }
00122           } else
00123             nthresh = n;
00124         }
00125       }
00126     else if (inVec(n) < vmin) {
00127       vmin = inVec(n);
00128       //nmin = n;
00129     } else if (inVec(n) > vmin*HystFactor_) {
00130       vmax = inVec(n);
00131       nmax = n;
00132       nthresh = 0;
00133 
00134       vmin = vmax;
00135       //nmin = nmax;
00136       theValid = true;
00137       theMaxFlag = true;
00138     }
00139   }
00140 }