Library of Assembled Shared Sources
filters.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * Distributed under the terms of the GPL (GNU Public License)
6 *
7 * The LASS License:
8 *
9 * Copyright 2004-2006 Bram de Greve and Tom De Muer
10 *
11 * LASS is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26/** @defgroup Filters
27 * @brief one dimensional causal filters ...
28 */
29
30#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_FILTERS_H
31#define LASS_GUARDIAN_OF_INCLUSION_NUM_FILTERS_H
32
33#include "num_common.h"
34#include "num_traits.h"
35
36namespace lass
37{
38namespace num
39{
40
41/** Base class for all one dimensional causal filters
42 * @ingroup Filters
43 */
44template
45<
46 typename T,
47 typename InputIterator = const T*,
48 typename OutputIterator = T*
49>
50class Filter
51{
52public:
53 typedef typename util::CallTraits<T>::TValue TValue;
54 typedef typename util::CallTraits<T>::TParam TParam;
55 typedef typename util::CallTraits<T>::TReference TReference;
56 typedef typename util::CallTraits<T>::TConstReference TConstReference;
57 typedef InputIterator TInputIterator;
58 typedef OutputIterator TOutputIterator;
59 typedef NumTraits<T> TNumTraits;
60
61 virtual ~Filter() {}
62 TOutputIterator operator()(TInputIterator first, TInputIterator last, TOutputIterator output) { return doFilter(first, last, output); }
63 void reset() { doReset(); }
64
65private:
66 virtual TOutputIterator doFilter(TInputIterator first, TInputIterator last, TOutputIterator output) = 0;
67 virtual void doReset() {}
68};
69
70
71
72/** Finite Impulse Response filter
73 * @ingroup Filters
74 */
75template
76<
77 typename T,
78 typename InputIterator = const T*,
79 typename OutputIterator = T*
80>
81class FirFilter: public Filter<T, InputIterator, OutputIterator>
82{
83public:
84 typedef typename Filter<T, InputIterator, OutputIterator>::TValue TValue;
85 typedef typename Filter<T, InputIterator, OutputIterator>::TParam TParam;
86 typedef typename Filter<T, InputIterator, OutputIterator>::TReference TReference;
87 typedef typename Filter<T, InputIterator, OutputIterator>::TConstReference TConstReference;
88 typedef typename Filter<T, InputIterator, OutputIterator>::TInputIterator TInputIterator;
89 typedef typename Filter<T, InputIterator, OutputIterator>::TOutputIterator TOutputIterator;
90 typedef typename Filter<T, InputIterator, OutputIterator>::TNumTraits TNumTraits;
91
92 typedef std::vector<T> TValues;
93
94 FirFilter(const TValues& impulseResponse);
95private:
96 typedef std::vector<size_t> TIndexTable;
97
98 TOutputIterator doFilter(TInputIterator first, TInputIterator last, TOutputIterator output) override;
99 void doReset() override;
100
101 TValues taps_;
102 TValues buffer_;
103 TIndexTable nextIndex_;
104 size_t tapSize_;
105 size_t bufferIndex_;
106};
107
108
109
110/** Infinite Impulse Response filter
111 * @ingroup Filters
112 */
113template
114<
115 typename T,
116 typename InputIterator = const T*,
117 typename OutputIterator = T*
118>
119class IirFilter: public Filter<T, InputIterator, OutputIterator>
120{
121public:
122 typedef typename Filter<T, InputIterator, OutputIterator>::TValue TValue;
123 typedef typename Filter<T, InputIterator, OutputIterator>::TParam TParam;
124 typedef typename Filter<T, InputIterator, OutputIterator>::TReference TReference;
125 typedef typename Filter<T, InputIterator, OutputIterator>::TConstReference TConstReference;
126 typedef typename Filter<T, InputIterator, OutputIterator>::TInputIterator TInputIterator;
127 typedef typename Filter<T, InputIterator, OutputIterator>::TOutputIterator TOutputIterator;
128 typedef typename Filter<T, InputIterator, OutputIterator>::TNumTraits TNumTraits;
129
130 typedef std::vector<T> TValues;
131 typedef std::pair<TValues, TValues> TValuesPair;
132
133 IirFilter(const TValues& numerator, const TValues& denominator);
134 IirFilter(const TValuesPair& coefficients);
135
136 static IirFilter makeLaplace(const TValues& nominator, const TValues& denominator, TParam samplingFrequency);
137 static IirFilter makeButterworthLowPass(unsigned order, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency);
138 static IirFilter makeButterworthHighPass(unsigned order, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency);
139 static IirFilter makeRlcLowPass(TParam qFactor, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency);
140 static IirFilter makeRlcHighPass(TParam qFactor, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency);
141 static IirFilter makeRlcBandPass(TParam qFactor, TParam centerAngularFrequency, TParam gain, TParam samplingFrequency);
142 static IirFilter makeRlcNotch(TParam qFactor, TParam centerAngularFrequency, TParam gain, TParam samplingFrequency);
143 static IirFilter makeIntegrator(TParam gain, TParam samplingFrequency);
144 static IirFilter makeDifferentiator(TParam gain, TParam samplingFrequency);
145 static IirFilter makeAWeighting(TParam samplingFrequency);
146
147private:
148 typedef std::vector<size_t> TIndexTable;
149
150 TOutputIterator doFilter(TInputIterator first, TInputIterator last, TOutputIterator output) override;
151 void doReset() override;
152 void init(const TValues& numerator, const TValues& denominator);
153
154 template <typename FwdIt1, typename FwdIt2>
155 static IirFilter doMakeLaplace(FwdIt1 numFirst, FwdIt1 numLast, FwdIt2 denFirst, FwdIt2 denLast, TParam sampleFrequency);
156 static IirFilter doMakeLaplace(const TValuesPair& coefficients, TParam sampleFrequency);
157
158 TValues xTaps_;
159 TValues yTaps_;
160 TValues xBuffer_;
161 TValues yBuffer_;
162 TIndexTable xNextIndex_;
163 TIndexTable yNextIndex_;
164 size_t xTapSize_;
165 size_t yTapSize_;
166 size_t xBufferIndex_;
167 size_t yBufferIndex_;
168};
169
170}
171
172}
173
174#include "filters.inl"
175
176#endif
177
178// EOF
Base class for all one dimensional causal filters.
Definition filters.h:51
static IirFilter makeButterworthHighPass(unsigned order, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency)
make an IIR filter implementing a high-pass butterworth filter.
Definition filters.inl:262
IirFilter(const TValues &numerator, const TValues &denominator)
construct IIR filter
Definition filters.inl:196
static IirFilter makeRlcNotch(TParam qFactor, TParam centerAngularFrequency, TParam gain, TParam samplingFrequency)
make an IIR filter implementing an notch RLC circuit.
Definition filters.inl:366
static IirFilter makeRlcHighPass(TParam qFactor, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency)
make an IIR filter implementing an high-pass RLC circuit.
Definition filters.inl:312
static IirFilter makeDifferentiator(TParam gain, TParam samplingFrequency)
make an IIR filter implementing a perfect differentiator
Definition filters.inl:397
static IirFilter makeButterworthLowPass(unsigned order, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency)
make an IIR filter implementing a low-pass butterworth filter.
Definition filters.inl:246
static IirFilter makeRlcBandPass(TParam qFactor, TParam centerAngularFrequency, TParam gain, TParam samplingFrequency)
make an IIR filter implementing an band-pass RLC circuit.
Definition filters.inl:338
static IirFilter makeIntegrator(TParam gain, TParam samplingFrequency)
make an IIR filter implementing a perfect integrator
Definition filters.inl:382
static IirFilter makeRlcLowPass(TParam qFactor, TParam cutoffAngularFrequency, TParam gain, TParam samplingFrequency)
make an IIR filter implementing an low-pass RLC circuit.
Definition filters.inl:286
static IirFilter makeAWeighting(TParam samplingFrequency)
make an A weighting filter
Definition filters.inl:411
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53