Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/OnePole.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 "OnePole.h"
00020 
00021 using std::ostringstream;
00022 using namespace Marsyas;
00023 
00024 
00025 OnePole::OnePole(mrs_string name) : MarSystem("OnePole", name)
00026 {
00027   addControls();
00028 }
00029 
00030 
00031 OnePole::~OnePole()
00032 {
00033 }
00034 
00035 
00036 MarSystem*
00037 OnePole::clone() const
00038 {
00039   return new OnePole(*this);
00040 }
00041 
00042 void
00043 OnePole::addControls()
00044 {
00045   addControl("mrs_real/alpha", 0.9);
00046   setControlState("mrs_real/alpha", true);
00047 }
00048 
00049 
00050 void
00051 OnePole::myUpdate(MarControlPtr sender)
00052 {
00053   // Use the default MarSystem setup with equal input/output stream format.
00054   MarSystem::myUpdate(sender);
00055 
00056   // Cache the alpha and gain value.
00057   alpha_ = getControl("mrs_real/alpha")->to<mrs_real>();
00058   gain_ = 1.0 - alpha_;
00059 
00060   // Allocate and initialize the buffer for previous output samples.
00061   mrs_natural rows = ctrl_inObservations_->to<mrs_natural>();
00062   previousOutputSamples_.stretch(rows, 1);
00063   previousOutputSamples_.setval(0.0);
00064 }
00065 
00066 void
00067 OnePole::myProcess(realvec& in, realvec& out)
00068 {
00069   mrs_natural t,o;
00070   for (o = 0; o < inObservations_; o++)
00071   {
00072     // Use the last sample from the previous slice for the first sample of
00073     // this slice.
00074     t = 0;
00075     out(o, t) = gain_ * in(o, t) + alpha_ * previousOutputSamples_(o, 0);
00076 
00077     // Do the remaining samples.
00078     for (t = 1; t < inSamples_; t++)
00079     {
00080       out(o, t) = gain_ * in(o, t) + alpha_ * out(o, t - 1);
00081     }
00082 
00083     // Store the last sample for usage in next process() call.
00084     previousOutputSamples_(o, 0) = out(o, inSamples_ - 1);
00085   }
00086 }