00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00042
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
00073
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
00111
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