Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/StereoSpectrum.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 "StereoSpectrum.h"
00020 
00021 using namespace std;
00022 using namespace Marsyas;
00023 
00024 StereoSpectrum::StereoSpectrum(mrs_string name):MarSystem("StereoSpectrum",name)
00025 {
00026   addControls();
00027 }
00028 
00029 StereoSpectrum::StereoSpectrum(const StereoSpectrum& a):MarSystem(a)
00030 {
00031 
00032 }
00033 
00034 StereoSpectrum::~StereoSpectrum()
00035 {
00036 }
00037 
00038 void
00039 StereoSpectrum::addControls()
00040 {
00041 }
00042 
00043 MarSystem*
00044 StereoSpectrum::clone() const
00045 {
00046   return new StereoSpectrum(*this);
00047 }
00048 
00049 void
00050 StereoSpectrum::myUpdate(MarControlPtr sender)
00051 {
00052   (void) sender;  //suppress warning of unused parameter(s)
00053 
00054   N2_ = ctrl_inObservations_->to<mrs_natural>()/2;
00055   N4_ = N2_/2+1; //i.e. N/2+1
00056 
00057   ctrl_onSamples_->setValue((mrs_natural)1, NOUPDATE);
00058   ctrl_onObservations_->setValue(N4_, NOUPDATE);// N/2+1
00059   ctrl_osrate_->setValue(ctrl_israte_->to<mrs_real>(), NOUPDATE);
00060 
00061   ostringstream oss;
00062   for (mrs_natural n=0; n < N4_; n++)
00063     oss << "stereobin_" << n << ",";
00064   ctrl_onObsNames_->setValue(oss.str(), NOUPDATE);
00065 }
00066 
00067 void
00068 StereoSpectrum::myProcess(realvec& in, realvec& out)
00069 {
00070   for (mrs_natural t=0; t < N4_; t++)
00071   {
00072     //left channel
00073     if (t==0) //DC bin (i.e. 0)
00074     {
00075       rel_ = in(0,0);
00076       iml_ = 0.0;
00077     }
00078     else if (t == N4_-1) //Nyquist bin (i.e. N/2)
00079     {
00080       rel_ = in(1, 0);
00081       iml_ = 0.0;
00082     }
00083     else //all other bins
00084     {
00085       rel_ = in(2*t, 0);
00086       iml_ = in(2*t+1, 0);
00087     }
00088     //right channel
00089     if (t==0) //DC bin (i.e. 0)
00090     {
00091       rer_ = in(N2_,0);
00092       imr_ = 0.0;
00093     }
00094     else if (t == N4_-1) //Nyquist bin (i.e. N/2)
00095     {
00096       rer_ = in(N2_+1, 0);
00097       imr_ = 0.0;
00098     }
00099     else //all other bins
00100     {
00101       rer_ = in(N2_ + 2*t, 0);
00102       imr_ = in(N2_ + 2*t+1, 0);
00103     }
00104 
00105     mrs_real f1 = (rel_ * rer_)*(rel_ * rer_);
00106     mrs_real f2 = (iml_ * imr_)*(iml_ * imr_);
00107     mrs_real f3 = (iml_ * rer_)*(iml_ * rer_);
00108     mrs_real f4 = (imr_ * rel_)*(imr_ * rel_);
00109     mrs_real nf = sqrt(f1 + f2 + f3 + f4);
00110     mrs_real dfl = rel_*rel_ + iml_*iml_;
00111     mrs_real dfr = rer_ * rer_ + imr_*imr_;
00112     mrs_real df = dfl + dfr;
00113 
00114     mrs_real sign;
00115     if (nf / dfl - nf/dfr > 0)
00116       sign = 1.0;
00117     else
00118       sign = -1.0;
00119 
00120     if (df != 0.0)
00121       out(t,0) = (1.0 - 2 * (nf / df)) * sign;
00122     else
00123       out(t,0) = 0.0;
00124   }
00125 
00126   //MATLAB_PUT(out, "stereoSpectrum");
00127   //MATLAB_EVAL("plot(stereoSpectrum)");
00128 }