Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/WHaSp.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 "WHaSp.h"
00020 #include <marsyas/peakView.h>
00021 #include "PeakFeatureSelect.h"
00022 #include "SelfSimilarityMatrix.h"
00023 #include "HWPS.h"
00024 
00025 using namespace std;
00026 using namespace Marsyas;
00027 
00028 WHaSp::WHaSp(mrs_string name):MarSystem("WHaSp", name)
00029 {
00030   HWPSnet_ = NULL;
00031 
00032   addControls();
00033 }
00034 
00035 WHaSp::WHaSp(const WHaSp& a) : MarSystem(a)
00036 {
00037   ctrl_histSize_ = getctrl("mrs_natural/histSize");
00038   ctrl_totalNumPeaks_ = getctrl("mrs_natural/totalNumPeaks");
00039   ctrl_frameMaxNumPeaks_ = getctrl("mrs_natural/frameMaxNumPeaks");
00040 
00041   HWPSnet_ = NULL;
00042 }
00043 
00044 WHaSp::~WHaSp()
00045 {
00046   delete HWPSnet_;
00047 }
00048 
00049 MarSystem*
00050 WHaSp::clone() const
00051 {
00052   return new WHaSp(*this);
00053 }
00054 
00055 void
00056 WHaSp::addControls()
00057 {
00058   addctrl("mrs_natural/histSize", 20, ctrl_histSize_);
00059   addctrl("mrs_natural/totalNumPeaks",0, ctrl_totalNumPeaks_);
00060   addctrl("mrs_natural/frameMaxNumPeaks", 0, ctrl_frameMaxNumPeaks_);
00061 }
00062 
00063 void
00064 WHaSp::createSimMatrixNet()
00065 {
00066   if(HWPSnet_)
00067     return;
00068 
00069   HWPSnet_ = new Series("HWPSnet");
00070 
00071   //add a feat selector and
00072   //set the features needed for HWPS
00073   MarSystem* peFeatSelect = new PeakFeatureSelect("peFeatSelect");
00074   peFeatSelect->updControl("mrs_natural/selectedFeatures",
00075                            PeakFeatureSelect::pkFrequency | PeakFeatureSelect::pkSetFrequencies| PeakFeatureSelect::pkSetAmplitudes);
00076   HWPSnet_->addMarSystem(peFeatSelect);
00077 
00078   //create a similarityMatrix MarSystem that uses the HPWS metric
00079   SelfSimilarityMatrix* simMat = new SelfSimilarityMatrix("simMat");
00080   simMat->addMarSystem(new HWPS("hwps"));
00081 
00082   HWPSnet_->addMarSystem(simMat);
00083 
00084   //link totalNumPeaks control to PeakFeatureSelect
00085   HWPSnet_->getctrl("PeakFeatureSelect/peFeatSelect/mrs_natural/totalNumPeaks")->linkTo(ctrl_totalNumPeaks_, NOUPDATE);
00086   HWPSnet_->update(); //only call update to HWPSnet_ since this is being called from WHaSp::update()! -> avoid potential infinite recursion!
00087 
00088   //link frameMaxNumPeaks control to PeakFeatureSelect
00089   HWPSnet_->getctrl("PeakFeatureSelect/peFeatSelect/mrs_natural/frameMaxNumPeaks")->linkTo(ctrl_frameMaxNumPeaks_, NOUPDATE);
00090   HWPSnet_->update(); //only call update to HWPSnet_ since this is being called from WHaSp::update()! -> avoid potential infinite recursion!
00091 
00092   //link histSize control to HWPS metric
00093   HWPSnet_->getctrl("SelfSimilarityMatrix/simMat/HWPS/hwps/mrs_natural/histSize")->linkTo(ctrl_histSize_, NOUPDATE);
00094   HWPSnet_->update(); //only call update to HWPSnet_ since this is being called from WHaSp::update()! -> avoid potential infinite recursion!
00095 
00096   HWPSnet_->setctrl("SelfSimilarityMatrix/simMat/HWPS/hwps/mrs_natural/histSize", 20);
00097   HWPSnet_->update(); //only call update to HWPSnet_ since this is being called from WHaSp::update()! -> avoid potential infinite recursion!
00098 
00099   HWPSnet_->updControl("SelfSimilarityMatrix/simMat/HWPS/hwps/mrs_bool/calcDistance", true);
00100 
00101   //HWPSnet_->setctrl("SelfSimilarityMatrix/simMat/HWPS/hwps/mrs_natural/histSize", 100);
00102   HWPSnet_->update(); //only call update to HWPSnet_ since this is being called from WHaSp::update()! -> avoid potential infinite recursion!
00103 
00104 }
00105 
00106 void
00107 WHaSp::myUpdate(MarControlPtr sender)
00108 {
00109   //output has the same flow format as input (see myProcess())
00110   MarSystem::myUpdate(sender);
00111 
00112   if(!HWPSnet_)
00113     createSimMatrixNet();
00114 
00115   //pass input flow configuration to internal MarSystem
00116   HWPSnet_->setctrl("mrs_natural/inSamples",ctrl_inSamples_);
00117   HWPSnet_->setctrl("mrs_natural/inObservations", ctrl_inObservations_);
00118   HWPSnet_->setctrl("mrs_real/israte", ctrl_israte_);
00119   HWPSnet_->updControl("mrs_string/inObsNames", ctrl_inObsNames_); //this calls update()
00120 }
00121 
00122 void
00123 WHaSp::myProcess(realvec& in, realvec& out)
00124 {
00125   peakView inPkView(in);
00126   peakView outPkView(out);
00127 
00128   //copy input to output, so we can fill in
00129   //the WHASP values calculated bellow
00130   out = in;
00131 
00132   mrs_natural numPeaks = inPkView.getFrameNumPeaks();
00133 
00134   if(numPeaks > 0)
00135   {
00136     simMatrix_.create(numPeaks, numPeaks);
00137 
00138     HWPSnet_->process(in, simMatrix_);
00139 
00140     mrs_real maxDist = MINREAL;
00141     realvec volumes(numPeaks);
00142 
00143     for(mrs_natural r=0; r<numPeaks; ++r)
00144     {
00145       outPkView(r, peakView::pkVolume) = 0;
00146       for(mrs_natural c=0; c < numPeaks; ++c)
00147       {
00148         if(r != c)
00149           outPkView(r, peakView::pkVolume) += simMatrix_(r, c)*outPkView(c, peakView::pkAmplitude);
00150       }
00151       if(outPkView(r, peakView::pkVolume) > maxDist)
00152         maxDist = outPkView(r, peakView::pkVolume);
00153       volumes(r) = outPkView(r, peakView::pkVolume);
00154     }
00155 
00156     volumes.sort();
00157     //  cout << "Number of Peaks considered" << numPeaks << endl;
00158     // cout << volumes ;
00159     mrs_natural nbSelected = 10;
00160     //mrs_real dist;
00161 
00162     for(mrs_natural i=0; i < numPeaks; ++i)
00163     {
00164 
00165       mrs_bool found=false;
00166       for (mrs_natural j=0 ; j< nbSelected ; j++)
00167         if(volumes(nbSelected-j-1) == outPkView(i, peakView::pkVolume))
00168           found=true;
00169 
00170       if(!found)
00171         outPkView(i, peakView::pkAmplitude) = 0;
00172 
00173       //"enhance" all spectral peaks that have a high harmonicity
00174       //similarity with other peaks (i.e. high HWPS)
00175       //             outPkView(i, peakView::pkAmplitude) *= outPkView(i, peakView::pkVolume)/maxDist;
00176 
00177       //filter peaks with Volumes below a defined threshold
00178       //
00179       //dist = outPkView(i, peakView::pkVolume)/maxDist;
00180       //if(dist < 0.75)
00181       //    outPkView(i, peakView::pkAmplitude) = 0;
00182     }
00183   }
00184 
00185 //      realvec table;
00186 //  outPkView.toTable(table);
00187 //  MATLAB_PUT(table, "P");
00188 //  MATLAB_EVAL("stemPeaks(P)");
00189 }
00190 
00191 
00192 
00193 
00194 
00195 
00196 
00197