Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/PhiSEMFilter.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2010 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 
00030 #include "../common_source.h"
00031 #include "PhiSEMFilter.h"
00032 
00033 
00034 using std::ostringstream;
00035 using std::cout;
00036 using std::endl;
00037 
00038 
00039 using namespace Marsyas;
00040 
00041 PhiSEMFilter::PhiSEMFilter(mrs_string name)
00042   : MarSystem("PhiSEMFilter", name) {
00043   output_ = NULL;
00044   coeffs_ = NULL;
00045   resVec_.create(10);
00046   freqVec_.create(10);
00047   addControls();
00048 }
00049 
00050 PhiSEMFilter::~PhiSEMFilter() {
00051   //delete previously allocated coeffs and outputs
00052   if ( coeffs_ )
00053     delete[] coeffs_;
00054   if ( output_ )
00055     delete[] output_;
00056 }
00057 
00058 MarSystem*
00059 PhiSEMFilter::clone() const {
00060   return new PhiSEMFilter(*this);
00061 }
00062 
00063 void
00064 PhiSEMFilter::addControls() {
00065   //Add specific controls needed by this MarSystem.
00066   addctrl("mrs_natural/numFilters", 1, numFilters_);
00067   setctrlState("mrs_natural/numFilters", true);
00068 
00069   addctrl("mrs_realvec/frequencies", freqVec_);
00070   setctrlState("mrs_realvec/frequencies", true);
00071 
00072   addctrl("mrs_realvec/resonances", resVec_);
00073   setctrlState("mrs_realvec/resonances", true);
00074 }
00075 
00076 void
00077 PhiSEMFilter::myUpdate(MarControlPtr sender) {
00078   MRSDIAG("PhiSEMFilter.cpp - PhiSEMFilter::myUpdate()");
00079 
00080   setctrl("mrs_natural/numFilters", getctrl("mrs_natural/numFilters"));
00081   freqVec_ = getctrl("mrs_realvec/frequencies")->to<mrs_realvec>();
00082   resVec_ = getctrl("mrs_realvec/resonances")->to<mrs_realvec>();
00083 
00084   mrs_natural numFilters = getctrl("mrs_natural/numFilters")->to<mrs_natural>();
00085   //mrs_natural numFilters = numFilters_->to<mrs_natural>();
00086   if ( numFilters > 10 ) {
00087     setctrl("mrs_natural/numFilters",   10);
00088     numFilters = 10;
00089   }
00090 
00091   //delete previously allocated coeffs and outputs
00092   if ( coeffs_ )
00093     delete[] coeffs_;
00094   if ( output_ )
00095     delete[] output_;
00096 
00097   coeffs_ = new mrs_realpair[numFilters * 2];
00098   output_ = new mrs_realpair[numFilters * 2];
00099 
00100   cout << "Num Filters: " << numFilters << endl;
00101   for(int i=0; i < numFilters; ++i) {
00102     coeffs_[i].zero = 1.0 - resVec_(i) * 2.0 * cos(freqVec_(i) * TWOPI / israte_);
00103     coeffs_[i].one = resVec_(i) * resVec_(i);
00104     output_[i].zero = output_[i].one = 0.0;
00105 
00106     cout << "->Filter " << i << ": freq=" << freqVec_(i)
00107          << " res=" << resVec_(i)
00108          << " coeff0=" << coeffs_[i].zero << " coeff1=" << coeffs_[i].one << endl;
00109   }
00110   MarSystem::myUpdate(sender);
00111 }
00112 
00113 void
00114 PhiSEMFilter::myProcess(realvec& in, realvec& out) {
00115   mrs_real temp = 0.0;
00116   mrs_real output = 0.0;
00117   mrs_natural t,o;
00118   for (o=0; o < inObservations_; o++ ) {
00119     for (t=0; t < inSamples_; t++ ) {
00120       //apply each filter
00121       for(int i=0; i < numFilters_->to<mrs_natural>(); ++i) {
00122         temp = in(o,t);
00123         temp -= output_[i].zero * coeffs_[i].zero;
00124         temp -= output_[i].one * coeffs_[i].one;
00125         output_[i].one = output_[i].zero;
00126         output_[i].zero = temp;
00127         output += output_[i].zero - output_[i].one;
00128       }
00129       out(o,t) = output;
00130     }
00131   }
00132 }