Loris 1.5

Filter.h

00001 #ifndef INCLUDE_FILTER_H
00002 #define INCLUDE_FILTER_H
00003 /*
00004  * This is the Loris C++ Class Library, implementing analysis, 
00005  * manipulation, and synthesis of digitized sounds using the Reassigned 
00006  * Bandwidth-Enhanced Additive Sound Model.
00007  *
00008  * Loris is Copyright (c) 1999-2010 by Kelly Fitz and Lippold Haken
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY, without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  *
00025  * Filter.h
00026  *
00027  * Definition of class Loris::Filter, a generic digital filter of 
00028  * arbitrary order having both feed-forward and feedback coefficients. 
00029  *
00030  * Kelly Fitz, 1 Sept 1999
00031  * revised 9 Oct 2009
00032  * loris@cerlsoundgroup.org
00033  *
00034  * http://www.cerlsoundgroup.org/Loris/
00035  *
00036  */
00037 
00038 #include "LorisExceptions.h"
00039 #include "Notifier.h"
00040 
00041 #include <algorithm>
00042 #include <deque>
00043 #include <vector>
00044 
00045 //  begin namespace
00046 namespace Loris {
00047 
00048 // ---------------------------------------------------------------------------
00049 //  class Filter
00050 //
00074 //
00075 class Filter
00076 {
00077 public:
00078 
00079 //  --- lifecycle ---
00080 
00081     //  default construction
00083     Filter( void );
00084     
00085     //  initialized construction
00100     //
00101 #if !defined(NO_TEMPLATE_MEMBERS)
00102     template<typename IterT1, typename IterT2>
00103     Filter( IterT1 ffwdbegin, IterT1 ffwdend,   //  feed-forward coeffs
00104             IterT2 fbackbegin, IterT2 fbackend, //  feedback coeffs
00105             double gain = 1. );
00106 #else
00107     Filter( const double * ffwdbegin, const double * ffwdend, //    feed-forward coeffs
00108             const double * fbackbegin, const double * fbackend, //  feedback coeffs
00109             double gain = 1. );
00110 #endif
00111 
00112     
00113     // copy constructor 
00116     Filter( const Filter & other );
00117     
00118     //  assignment operator
00121     Filter & operator=( const Filter & rhs );
00122     
00126     ~Filter( void );
00127     
00128 
00129 //  --- filtering ---
00130 
00135     double apply( double input );
00136 
00140     double operator() ( double input ) { return apply(input); }    
00141 
00142 //  --- access/mutation ---
00143 
00147     
00148     std::vector< double > numerator( void );
00149 
00153     
00154     const std::vector< double > numerator( void ) const;
00155     
00159     
00160     std::vector< double > denominator( void );
00161 
00165     
00166     const std::vector< double > denominator( void ) const;
00167     
00168     
00170     void clear( void );
00171 
00172     
00173 private:    
00174     
00175 //  --- implementation ---
00176 
00178     std::deque< double > m_delayline;
00179         
00181     std::vector< double > m_ffwdcoefs;  
00182 
00184     std::vector< double > m_fbackcoefs; 
00185     
00187     double m_gain;      
00188 
00189 };  //  end of class Filter
00190 
00191 
00192 
00193 // ---------------------------------------------------------------------------
00194 //  constructor
00195 // ---------------------------------------------------------------------------
00210 //
00211 #if !defined(NO_TEMPLATE_MEMBERS)
00212 template<typename IterT1, typename IterT2>
00213 Filter::Filter( IterT1 ffwdbegin, IterT1 ffwdend,   //  feed-forward coeffs
00214                 IterT2 fbackbegin, IterT2 fbackend, //  feedback coeffs
00215                 double gain ) :
00216 #else
00217 inline 
00218 Filter::Filter( const double * ffwdbegin, const double * ffwdend, //    feed-forward coeffs
00219                 const double * fbackbegin, const double * fbackend, //  feedback coeffs
00220                 double gain ) :
00221 #endif
00222     m_ffwdcoefs( ffwdbegin, ffwdend ),
00223     m_fbackcoefs( fbackbegin, fbackend ),
00224     m_delayline( std::max( ffwdend-ffwdbegin, fbackend-fbackbegin ) - 1, 0. ),
00225     m_gain( gain )
00226 {
00227     if ( *fbackbegin == 0. )
00228     {
00229         Throw( InvalidObject, 
00230                "Tried to create a Filter with feeback coefficient at zero delay equal to 0.0" );
00231     }
00232 
00233     //  normalize the coefficients by 1/a[0], if a[0] is not equal to 1.0
00234     //  (already checked for a[0] == 0 above)
00235     if ( *fbackbegin != 1. )
00236     {
00237         //  scale all filter coefficients by a[0]:
00238         std::transform( m_ffwdcoefs.begin(), m_ffwdcoefs.end(), m_ffwdcoefs.begin(),
00239                         std::bind2nd( std::divides<double>(), *fbackbegin ) );
00240         std::transform( m_fbackcoefs.begin(), m_fbackcoefs.end(), m_fbackcoefs.begin(), 
00241                         std::bind2nd( std::divides<double>(), *fbackbegin ) );
00242         m_fbackcoefs[0] = 1.;
00243     }
00244 }
00245 
00246 
00247 }   //  end of namespace Loris
00248 
00249 #endif /* ndef INCLUDE_FILTER_H */
 All Classes Functions Variables Typedefs Enumerator Friends