Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/SNR.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 "SNR.h"
00020 #include "../common_source.h"
00021 
00022 using namespace std;
00023 using namespace Marsyas;
00024 
00025 SNR::SNR(mrs_string name):MarSystem("SNR", name)
00026 {
00027   addControls();
00028   nsum_ = 0.0;
00029   nbsum_ = 0.0;
00030   psum_ = 0.0;
00031   dsum_ = 0.0;
00032 }
00033 
00034 SNR::SNR(const SNR& a) : MarSystem(a)
00035 {
00036   ctrl_mode_ = getctrl("mrs_string/mode");
00037   ctrl_done_ = getctrl("mrs_bool/done");
00038   nsum_ = 0.0;
00039   dsum_ = 0.0;
00040   nbsum_ = 0.0;
00041   psum_ = 0.0;
00042 }
00043 
00044 
00045 SNR::~SNR()
00046 {
00047 }
00048 
00049 MarSystem*
00050 SNR::clone() const
00051 {
00052   return new SNR(*this);
00053 }
00054 
00055 void
00056 SNR::addControls()
00057 {
00058   addctrl("mrs_string/mode", "standard", ctrl_mode_);
00059   addctrl("mrs_bool/done", false, ctrl_done_);
00060 
00061 }
00062 
00063 void
00064 SNR::myUpdate(MarControlPtr sender)
00065 {
00066   (void) sender;  //suppress warning of unused parameter(s)
00067   MRSDIAG("SNR.cpp - SNR:myUpdate");
00068   ctrl_onSamples_->setValue((mrs_natural)1, NOUPDATE);
00069   ctrl_onObservations_->setValue((mrs_natural)2, NOUPDATE);
00070   ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00071   ctrl_onObsNames_->setValue("SNR_" + ctrl_inObsNames_->to<mrs_string>() , NOUPDATE);
00072 
00073 
00074 }
00075 
00076 void
00077 SNR::myProcess(realvec& in, realvec& out)
00078 {
00079   const mrs_string& mode = ctrl_mode_->to<mrs_string>();
00080   mrs_bool accumulateNow = true;
00081 
00082   out.setval (0.);
00083 
00084   mrs_real  nsum    = 0,
00085              nbsum  = 0,
00086              psum   = 0,
00087               diff  = 0,
00088                dsum = 0;
00089 
00090   for (mrs_natural t = 0; t < inSamples_; t++)
00091   {
00092     nsum    += (in(0,t) * in(0,t));
00093     nbsum   += (in(1,t) * in(1,t));
00094     psum    += (in(0,t) * in(1,t));
00095     diff    = (in(0,t) - in(1,t));
00096     dsum    += (diff * diff);
00097   }
00098 
00099   if (mode == "checkRef4Silence")
00100   {
00101     if (nbsum/inSamples_ < 1e-6) //-60dB
00102       accumulateNow = false;
00103   }
00104 
00105   if (accumulateNow)
00106   {
00107     nsum_   += nsum;
00108     nbsum_  += nbsum;
00109     psum_   += psum;
00110     dsum_   += dsum;
00111   }
00112 
00113   if (nsum_ != 0 && dsum_ != 0)
00114     out(0,0) = 10. * log10(nsum_ / dsum_);
00115 
00116   if (nsum_ != 0 && nbsum_ != 0)
00117     r_ = (psum_ / sqrt(nsum_ * nbsum_));
00118   else
00119     r_ = 0;
00120 
00121   out(1,0) = 10. * log10(1 / (1 - (r_ * r_)));
00122 
00123   if (ctrl_done_->to<mrs_bool>() == true)
00124   {
00125     nsum_ = 0.0;
00126     nbsum_ = 0.0;
00127     dsum_ = 0.0;
00128     psum_ = 0.0;
00129   }
00130 
00131 
00132 
00133 }