MCL
A C++ library mirroring some of the most common Matlab functions.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
iirfilter.h
Go to the documentation of this file.
1 /*
2  MCL
3  Copyright (c) 2012-18, Enzo De Sena
4  All rights reserved.
5 
6  Authors: Enzo De Sena, enzodesena@gmail.com
7  */
8 
9 #ifndef MCL_IIRFILTER_H
10 #define MCL_IIRFILTER_H
11 
12 #include "digitalfilter.h"
13 #include "vectorop.h"
14 #include "constants.h"
15 
16 namespace mcl {
17 
18 
20 class IirFilter : public DigitalFilter {
21 public:
23  IirFilter();
24 
29  IirFilter(std::vector<Real> B,std::vector<Real> A);
30 
31 
39  virtual Real Filter(const Real input) noexcept;
40 
41  virtual void Filter(const Real* input_data, const Int num_samples,
42  Real* output_data) noexcept;
43 
45 
47  Int order() const noexcept;
48 
53  void SetCoefficients(const std::vector<Real>& B,
54  const std::vector<Real>& A) noexcept;
55 
57  void SetCoefficients(const IirFilter& filter) noexcept;
58 
59  Real GetNumeratorCoefficient(const Int coeff_id) const noexcept;
60 
61  Real GetDenominatorCoefficient(const Int coeff_id) const noexcept;
62 
63  void SetNumeratorCoefficient(const Int coeff_id,
64  const Real value) noexcept;
65 
66  inline void SetDenominatorCoefficient(const Int coeff_id,
67  const Real value) noexcept;
68 
70  std::vector<Real> B() const;
71 
73  std::vector<Real> A() const;
74 
75  void Reset();
76 
78  static bool Test();
79 
81  IirFilter & operator= (const IirFilter &);
82 
84  IirFilter (const IirFilter&);
85 
86  virtual ~IirFilter();
87 
88 private:
89  std::vector<Real> B_;
90  std::vector<Real> A_;
91 
92  // By storing A[0] before normalisation we can output B() and A() before
93  // normalisation while keeping the internal representation normalised
94  Real A0_;
95 
96  Real* state_;
97 };
98 
100 class IirFilterBank : public FilterBank {
101 private:
102  std::vector<IirFilter> filters_;
103 
104 public:
105  IirFilterBank(const std::vector<IirFilter>& filters) noexcept :
106  filters_(filters) {}
107 
108  virtual Int num_filters() noexcept { return filters_.size(); }
109 
111  virtual std::vector<Real> Filter(const Real input);
112 
114  virtual std::vector<std::vector<Real> >
115  Filter(const std::vector<Real>& input);
116 
118  virtual void Reset();
119 };
120 
121 //
122 // /** Implements a first-order IIR low-pass filter with a given decay constant. */
123 // class RampSmoothingFilter : public DigitalFilter {
124 // public:
125 //
126 // /**
127 // @param[in] ramp_samples number of samples after which the value is
128 // to 1/e away from target value. */
129 // RampSmoothingFilter(const Real ramp_samples) noexcept {
130 // ASSERT_WITH_MESSAGE(std::isgreaterequal(ramp_samples, 0),
131 // "Decay constant cannot be negative ");
132 //
133 //
134 // }
135 //
136 // virtual Real Filter(const Real input) noexcept {
137 // return filter_.Filter(input);
138 // }
139 //
140 // using DigitalFilter::Filter;
141 //
142 // virtual void Reset() noexcept { filter_.Reset(); }
143 //
144 //
145 // private:
146 // };
147 //
149 //class LowPassSmoothingFilter : public DigitalFilter {
150 //public:
151 //
152 // /**
153 // @param[in] ramp_samples number of samples after which the value is
154 // to 1/e away from target value. */
155 // LowPassSmoothingFilter(const Real ramp_samples) noexcept {
156 // ASSERT_WITH_MESSAGE(std::isgreaterequal(ramp_samples, 0),
157 // "Decay constant cannot be negative ");
158 //
159 // Real a1 = exp(-1.0/ramp_samples);
160 // Real b0 = 1.0 - a1;
161 // filter_ = IirFilter(mcl::BinaryVector(b0, 0.0),
162 // mcl::BinaryVector(1.0, -a1));
163 // }
164 //
165 // virtual Real Filter(const Real input) noexcept {
166 // return filter_.Filter(input);
167 // }
168 //
169 // using DigitalFilter::Filter;
170 //
171 // virtual void Reset() noexcept { filter_.Reset(); }
172 //
173 //
174 //private:
175 // IirFilter filter_;
176 //};
177 
179 IirFilter IdenticalFilter();
180 
182 IirFilter GainFilter(const Real gain);
183 
187 IirFilter WallFilter(WallType wall_type, Real sampling_frequency);
188 
190 IirFilter PinkifierFilter();
191 
192 } // namespace mcl
193 
194 #endif