library of assembled shared sources

http://lass.cocamware.com

filters.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  Distributed under the terms of the GPL (GNU Public License)
00006  *
00007  *  The LASS License:
00008  *
00009  *  Copyright 2004-2006 Bram de Greve and Tom De Muer
00010  *
00011  *  LASS is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version.
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU General Public License for more details.
00020  *
00021  *  You should have received a copy of the GNU General Public License
00022  *  along with this program; if not, write to the Free Software
00023  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  */
00025 
00026 /** @defgroup Filters
00027  *  @brief one dimensional causal filters ...
00028  */
00029 
00030 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_FILTERS_H
00031 #define LASS_GUARDIAN_OF_INCLUSION_NUM_FILTERS_H
00032 
00033 #include "num_common.h"
00034 #include "num_traits.h"
00035 
00036 namespace lass
00037 {
00038 namespace num
00039 {
00040 
00041 /** Base class for all one dimensional causal filters
00042  *  @ingroup Filters
00043  */
00044 template 
00045 <   
00046     typename T,
00047     typename InputIterator = const T*,
00048     typename OutputIterator = T*
00049 >
00050 class Filter
00051 {
00052 public:
00053     typedef typename util::CallTraits<T>::TValue TValue;
00054     typedef typename util::CallTraits<T>::TParam TParam;
00055     typedef typename util::CallTraits<T>::TReference TReference;
00056     typedef typename util::CallTraits<T>::TConstReference TConstReference;
00057     typedef InputIterator TInputIterator;
00058     typedef OutputIterator TOutputIterator;
00059     typedef NumTraits<T> TNumTraits;
00060 
00061     virtual ~Filter() {}
00062     TOutputIterator operator()(TInputIterator first, TInputIterator last, TOutputIterator output) { return doFilter(first, last, output); }
00063     void reset() { doReset(); }
00064 
00065 private:
00066     virtual TOutputIterator doFilter(TInputIterator first, TInputIterator last, TOutputIterator output) = 0;
00067     virtual void doReset() {}
00068 };
00069 
00070 
00071 
00072 /** Finite Impulse Response filter
00073  *  @ingroup Filters
00074  */
00075 template 
00076 <   
00077     typename T,
00078     typename InputIterator = const T*,
00079     typename OutputIterator = T*
00080 >
00081 class FirFilter: public Filter<T, InputIterator, OutputIterator>
00082 {
00083 public:
00084     typedef typename Filter<T, InputIterator, OutputIterator>::TValue TValue;
00085     typedef typename Filter<T, InputIterator, OutputIterator>::TParam TParam;
00086     typedef typename Filter<T, InputIterator, OutputIterator>::TReference TReference;
00087     typedef typename Filter<T, InputIterator, OutputIterator>::TConstReference TConstReference;
00088     typedef typename Filter<T, InputIterator, OutputIterator>::TInputIterator TInputIterator;
00089     typedef typename Filter<T, InputIterator, OutputIterator>::TOutputIterator TOutputIterator;
00090     typedef typename Filter<T, InputIterator, OutputIterator>::TNumTraits TNumTraits;
00091 
00092     typedef std::vector<T> TValues;
00093 
00094     FirFilter(const TValues& impulseResponse);
00095 private:
00096     typedef std::vector<size_t> TIndexTable;
00097 
00098     TOutputIterator doFilter(TInputIterator first, TInputIterator last, TOutputIterator output);
00099     void doReset();
00100 
00101     TValues taps_;
00102     TValues buffer_;
00103     TIndexTable nextIndex_;
00104     size_t tapSize_;
00105     size_t bufferIndex_;
00106 };
00107 
00108 
00109 
00110 /** Infinite Impulse Response filter
00111  *  @ingroup Filters
00112  */
00113 template 
00114 <   
00115     typename T,
00116     typename InputIterator = const T*,
00117     typename OutputIterator = T*
00118 >
00119 class IirFilter: public Filter<T, InputIterator, OutputIterator>
00120 {
00121 public:
00122     typedef typename Filter<T, InputIterator, OutputIterator>::TValue TValue;
00123     typedef typename Filter<T, InputIterator, OutputIterator>::TParam TParam;
00124     typedef typename Filter<T, InputIterator, OutputIterator>::TReference TReference;
00125     typedef typename Filter<T, InputIterator, OutputIterator>::TConstReference TConstReference;
00126     typedef typename Filter<T, InputIterator, OutputIterator>::TInputIterator TInputIterator;
00127     typedef typename Filter<T, InputIterator, OutputIterator>::TOutputIterator TOutputIterator;
00128     typedef typename Filter<T, InputIterator, OutputIterator>::TNumTraits TNumTraits;
00129 
00130     typedef std::vector<T> TValues;
00131     typedef std::pair<TValues, TValues> TValuesPair;
00132 
00133     IirFilter(const TValues& numerator, const TValues& denominator);
00134     IirFilter(const TValuesPair& coefficients);
00135 
00136     static IirFilter makeLaplace(const TValues& nominator, const TValues& denominator, TParam samplingFrequency);
00137     static IirFilter makeButterworthLowPass(unsigned order, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency);
00138     static IirFilter makeButterworthHighPass(unsigned order, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency);
00139     static IirFilter makeRlcLowPass(TParam qFactor, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency);
00140     static IirFilter makeRlcHighPass(TParam qFactor, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency);
00141     static IirFilter makeRlcBandPass(TParam qFactor, TParam centerAngularFrequency, TParam gain, TParam samplingFrequency);
00142     static IirFilter makeRlcNotch(TParam qFactor, TParam centerAngularFrequency, TParam gain, TParam samplingFrequency);
00143     static IirFilter makeIntegrator(TParam gain, TParam samplingFrequency);
00144     static IirFilter makeDifferentiator(TParam gain, TParam samplingFrequency);
00145     static IirFilter makeAWeighting(TParam samplingFrequency);
00146 
00147 private:
00148     typedef std::vector<size_t> TIndexTable;
00149 
00150     TOutputIterator doFilter(TInputIterator first, TInputIterator last, TOutputIterator output);
00151     void doReset();
00152     void init(const TValues& numerator, const TValues& denominator);
00153     
00154     template <typename FwdIt1, typename FwdIt2>
00155     static IirFilter doMakeLaplace(FwdIt1 numFirst, FwdIt1 numLast, FwdIt2 denFirst, FwdIt2 denLast, TParam sampleFrequency);
00156     static IirFilter doMakeLaplace(const TValuesPair& coefficients, TParam sampleFrequency);
00157 
00158     TValues xTaps_;
00159     TValues yTaps_;
00160     TValues xBuffer_;
00161     TValues yBuffer_;
00162     TIndexTable xNextIndex_;
00163     TIndexTable yNextIndex_;
00164     size_t xTapSize_;
00165     size_t yTapSize_;
00166     size_t xBufferIndex_;
00167     size_t yBufferIndex_;
00168 };
00169 
00170 }
00171 
00172 }
00173 
00174 #include "filters.inl"
00175 
00176 #endif
00177 
00178 // EOF

Generated on Mon Nov 10 14:20:04 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo