Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/ResampleNearestNeighbour.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 "ResampleNearestNeighbour.h"
00020 #include "../common_source.h"
00021 
00022 using namespace std;
00023 using namespace Marsyas;
00024 
00025 ResampleNearestNeighbour::ResampleNearestNeighbour(mrs_string name):MarSystem("ResampleNearestNeighbour", name)
00026 {
00027   addControls();
00028 }
00029 
00030 ResampleNearestNeighbour::ResampleNearestNeighbour(const ResampleNearestNeighbour& a) : MarSystem(a)
00031 {
00032   ctrl_stretch_ = getctrl("mrs_real/stretch");
00033   ctrl_samplingRateAdjustmentMode_ = getctrl("mrs_bool/samplingRateAdjustmentMode");
00034 }
00035 
00036 
00037 ResampleNearestNeighbour::~ResampleNearestNeighbour()
00038 {
00039 }
00040 
00041 MarSystem*
00042 ResampleNearestNeighbour::clone() const
00043 {
00044   return new ResampleNearestNeighbour(*this);
00045 }
00046 
00047 void
00048 ResampleNearestNeighbour::addControls()
00049 {
00050   addctrl("mrs_real/stretch", 1.0, ctrl_stretch_);
00051   addctrl("mrs_bool/samplingRateAdjustmentMode", (mrs_bool)true , ctrl_samplingRateAdjustmentMode_);
00052 
00053   setctrlState("mrs_real/stretch", true);
00054   setctrlState("mrs_bool/samplingRateAdjustmentMode",(mrs_bool)true);
00055 }
00056 
00057 void
00058 ResampleNearestNeighbour::myUpdate(MarControlPtr sender)
00059 {
00060   MarSystem::myUpdate(sender);
00061   MRSDIAG("ResampleNearestNeighbour.cpp - ResampleNearestNeighbour:myUpdate");
00062   mrs_real alpha = ctrl_stretch_->to<mrs_real>();
00063   ctrl_onSamples_->setValue((mrs_natural) (alpha * ctrl_inSamples_->to<mrs_natural>()), NOUPDATE);
00064   ctrl_onObservations_->setValue(ctrl_inObservations_->to<mrs_natural>());
00065 
00066   if (!(ctrl_samplingRateAdjustmentMode_->to<mrs_bool>()))
00067   {
00068     alpha=1.0;
00069   }
00070 
00071   ctrl_osrate_->setValue(ctrl_israte_->to<mrs_real>()*alpha);
00072 
00073   mrs_string inObsNames = ctrl_inObsNames_->to<mrs_string>();
00074   // Add prefix to the observation names.
00075   ctrl_onObsNames_->setValue(obsNamesAddPrefix(inObsNames, "ResampleNearestNeighbour_"), NOUPDATE);
00076 
00077 }
00078 
00079 void
00080 ResampleNearestNeighbour::myProcess(realvec& in, realvec& out)
00081 {
00082   mrs_natural o,t;
00083   mrs_real tp;
00084   mrs_natural tl, tr;
00085   mrs_real alpha = ctrl_stretch_->to<mrs_real>();
00086 
00087 
00088   for (o=0; o < onObservations_; o++)
00089   {
00090     for (t = 0; t < onSamples_; t++)
00091     {
00092       tp = t / alpha;
00093       tl= (mrs_natural)tp;
00094       tr = tl + 1;
00095       if (tl<inSamples_)
00096       {
00097 
00098         out(o,t) = (tr-tp) * in(o,tl) + (tp-tl) * in(o,tr);
00099         if((tp-tl)>(tr-tp)) //on equality default to the leftmost value
00100         {
00101           out(o,t) =in(o,tr);
00102         }
00103         else
00104         {
00105           out(o,t) =in(o,tl);
00106         }
00107       }
00108       else // "reflect" on boundary
00109       {
00110         out(o,t) = in(o,inSamples_-1); //  alternative possibilities would include:
00111 
00112         //periodic on boundary:
00113         //out(o,t) = in(o,0);
00114 
00115         //zero pad on boundary:
00116         //out(o,t) == 0;
00117       }
00118     }
00119   }
00120 }