Marsyas  0.5.0-beta1
/Users/jleben/code/marsyas/src/marsyas/marsystems/PeakResidual.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 #include <iostream>
00019 
00020 #include "../common_source.h"
00021 #include "PeakResidual.h"
00022 
00023 //#define MTLB_DBG_LOG
00024 
00025 using std::ostringstream;
00026 using namespace Marsyas;
00027 
00028 PeakResidual::PeakResidual(mrs_string name):MarSystem("PeakResidual", name)
00029 {
00030 
00031   addControls();
00032 }
00033 
00034 PeakResidual::PeakResidual(const PeakResidual& a) : MarSystem(a)
00035 {
00036   ctrl_SNR_ = getctrl("mrs_real/SNR");
00037 }
00038 
00039 PeakResidual::~PeakResidual()
00040 {
00041   outFile_.close ();
00042 }
00043 
00044 MarSystem*
00045 PeakResidual::clone() const
00046 {
00047   return new PeakResidual(*this);
00048 }
00049 
00050 void
00051 PeakResidual::addControls()
00052 {
00053   addctrl("mrs_real/SNR", 0.0, ctrl_SNR_);
00054   addctrl("mrs_bool/snrInDb", true);
00055   addctrl("mrs_string/outFilePath", EMPTYSTRING);
00056 }
00057 
00058 void
00059 PeakResidual::myUpdate(MarControlPtr sender)
00060 {
00061   (void) sender;  //suppress warning of unused parameter(s)
00062   mrs_natural o;
00063   ctrl_onSamples_->setValue(ctrl_inSamples_, NOUPDATE);
00064   ctrl_onObservations_->setValue(ctrl_inObservations_->to<mrs_natural>()/2, NOUPDATE);
00065   ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00066 
00067   ostringstream oss;
00068   mrs_string inObsNames = ctrl_inObsNames_->to<mrs_string>();
00069   mrs_string inObsName;
00070   mrs_string temp;
00071   for(o=0; o < ctrl_onObservations_->to<mrs_natural>(); o++)
00072   {
00073     inObsName = inObsNames.substr(0, inObsNames.find(","));
00074     temp = inObsNames.substr(inObsNames.find(",")+1, inObsNames.length());
00075     inObsNames = temp;
00076     oss << inObsName << "_residual,";
00077   }
00078   ctrl_onObsNames_->setValue(oss.str(), NOUPDATE);
00079 
00080   outFile_.close ();
00081   outFile_.clear ();
00082 
00083   mrs_string fileName = getControl ("mrs_string/outFilePath")->to<mrs_string> ();
00084   if (fileName != EMPTYSTRING)
00085     outFile_.open(fileName.c_str ());
00086 
00087 }
00088 
00089 void
00090 PeakResidual::myProcess(realvec& in, realvec& out)
00091 {
00092   mrs_bool inDb = getControl ("mrs_bool/snrInDb")->to<mrs_bool>();
00093   mrs_natural t,o;
00094   mrs_real snr = (inDb)? -80.0 : 0.;
00095   mrs_real originalPower;
00096   mrs_real synthPower;
00097   mrs_real diffPower;
00098 
00099 #ifdef MARSYAS_MATLAB
00100 #ifdef MTLB_DBG_LOG
00101   MATLAB_PUT(in, "in");
00102   MATLAB_EVAL("figure(31),subplot(211),plot(in(1,:)),axis([1 512 -1 1]),grid on,subplot(212),plot(in(2,:)),axis([1 512 -1 1]),grid on");
00103 #endif
00104 #endif
00105 
00106   for (o=0; o < inObservations_/2; o++)
00107   {
00108     originalPower=0;
00109     synthPower=0;
00110     diffPower=0;
00111     for (t = 0; t < inSamples_; t++)
00112     {
00113       out(o,t) =  in(o,t)-in(o+1, t);
00114       synthPower += in(o, t)*in(o, t);
00115       diffPower += out(o, t)*out(o, t);
00116       originalPower += in(o+1, t)*in(o+1, t);
00117     }
00118 
00119     //originalPower/=inSamples_;
00120     //synthPower/=inSamples_;
00121     //diffPower/=inSamples_;
00122 
00123     if(synthPower > .001 && originalPower > .01) //[?]
00124     {
00125       snr = (originalPower)/(diffPower+MINREAL);
00126       if (inDb)
00127         snr = 10*log10(snr); // +synthPower
00128     }
00129   }
00130 
00131   ctrl_SNR_->setValue(snr);
00132 
00133   if (outFile_.good ())
00134   {
00135     outFile_ << snr << std::endl;
00136     //outFile_.flush ();
00137   }
00138 }
00139