Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/SpectralSNR.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2007 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 "SpectralSNR.h"
00020 #include "../common_source.h"
00021 
00022 using namespace std;
00023 using namespace Marsyas;
00024 
00025 SpectralSNR::SpectralSNR(mrs_string name):MarSystem("SpectralSNR",name)
00026 {
00027   addControls();
00028 }
00029 
00030 SpectralSNR::SpectralSNR(const SpectralSNR& a):MarSystem(a)
00031 {
00032   N2_ = a.N2_;
00033 }
00034 
00035 SpectralSNR::~SpectralSNR()
00036 {
00037 }
00038 
00039 void
00040 SpectralSNR::addControls()
00041 {
00042 }
00043 
00044 MarSystem*
00045 SpectralSNR::clone() const
00046 {
00047   return new SpectralSNR(*this);
00048 }
00049 
00050 void
00051 SpectralSNR::myUpdate(MarControlPtr sender)
00052 {
00053   (void) sender;  //suppress warning of unused parameter(s)
00054   ctrl_onSamples_->setValue((mrs_natural)1, NOUPDATE);
00055   ctrl_onObservations_->setValue((mrs_natural)1, NOUPDATE);
00056   ctrl_osrate_->setValue(ctrl_israte_->to<mrs_real>(), NOUPDATE);
00057   ctrl_onObsNames_->setValue(ctrl_inObsNames_, NOUPDATE);
00058 
00059   N2_ = inObservations_/2;
00060 }
00061 
00062 void
00063 SpectralSNR::myProcess(realvec& in, realvec& out)
00064 {
00065   mrs_natural t,o;
00066   for (t = 0; t < inSamples_; t++)
00067   {
00068     sum_ = 0.0;
00069 
00070     for (o=0; o < N2_; o++)
00071     {
00072       orig_ = in(o,t);
00073       extr_ = in(N2_+o, t);
00074       if (orig_ != 0.0)
00075         sum_ += (orig_ * orig_) / ((orig_-extr_) * (orig_-extr_));
00076     }
00077 
00078     if (sum_ != 0.0)
00079       sum_ /= N2_;
00080     out(0,t) = 10.0 * log10(sqrt(sum_));
00081 
00082     MRSMSG("sum("<<t<<") = " << sum_ << endl);
00083     MRSMSG("SpectralSNR (for frame "<<t<<") = " << out(0,t) << endl);
00084   }
00085 }