AudiClean
Fir1.h
Go to the documentation of this file.
1 /*
2 License: MIT License (http://www.opensource.org/licenses/mit-license.php)
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 /* (C) 2013-2021 Graeme Hattan & Bernd Porr */
24 
25 #ifndef FIR1_H
26 #define FIR1_H
27 
28 #include <stdio.h>
29 #include <vector>
30 
36 class Fir1 {
37 public:
43  template <unsigned nTaps> Fir1(const double (&_coefficients)[nTaps]) :
44  coefficients(new double[nTaps]),
45  buffer(new double[nTaps]()),
46  taps(nTaps) {
47  for(unsigned i=0;i<nTaps;i++) {
48  coefficients[i] = _coefficients[i];
49  buffer[i] = 0;
50  }
51  }
52 
57  Fir1(std::vector<double> _coefficients) {
58  initWithVector(_coefficients);
59  }
60 
66  Fir1(double *coefficients, unsigned number_of_taps);
67 
74  Fir1(const char* coeffFile, unsigned number_of_taps = 0);
75 
81  Fir1(unsigned number_of_taps);
82 
86  ~Fir1();
87 
88 
94  inline double filter(double input) {
95  const double *coeff = coefficients;
96  const double *const coeff_end = coefficients + taps;
97 
98  double *buf_val = buffer + offset;
99 
100  *buf_val = input;
101  double output_ = 0;
102 
103  while(buf_val >= buffer)
104  output_ += *buf_val-- * *coeff++;
105 
106  buf_val = buffer + taps-1;
107 
108  while(coeff < coeff_end)
109  output_ += *buf_val-- * *coeff++;
110 
111  if(++offset >= taps)
112  offset = 0;
113 
114  return output_;
115  }
116 
117 
124  inline void lms_update(double error) {
125  double *coeff = coefficients;
126  const double *coeff_end = coefficients + taps;
127 
128  double *buf_val = buffer + offset;
129 
130  while(buf_val >= buffer) {
131  *coeff++ += *buf_val-- * error * mu;
132  }
133 
134  buf_val = buffer + taps-1;
135 
136  while(coeff < coeff_end) {
137  *coeff++ += *buf_val-- * error * mu;
138  }
139  }
140 
145  void setLearningRate(double _mu) {mu = _mu;};
146 
150  double getLearningRate() {return mu;};
151 
155  void reset();
156 
160  void zeroCoeff();
161 
170  void getCoeff(double* coeff_data, unsigned number_of_taps) const;
171 
175  std::vector<double> getCoeffVector() const {
176  return std::vector<double>(coefficients,coefficients+taps);
177  }
178 
182  unsigned getTaps() {return taps;};
183 
189  inline double getTapInputPower() {
190  double *buf_val = buffer;
191 
192  double p = 0;
193 
194  for(unsigned i = 0; i < taps; i++) {
195  p += (*buf_val) * (*buf_val);
196  buf_val++;
197  }
198 
199  return p;
200  }
201 
202 private:
203  void initWithVector(std::vector<double> _coefficients);
204 
205  double *coefficients;
206  double *buffer;
207  unsigned taps;
208  unsigned offset = 0;
209  double mu = 0;
210 };
211 
212 #endif
Fir1::lms_update
void lms_update(double error)
Definition: Fir1.h:124
Fir1::getTaps
unsigned getTaps()
Definition: Fir1.h:182
Fir1::getLearningRate
double getLearningRate()
Definition: Fir1.h:150
Fir1::setLearningRate
void setLearningRate(double _mu)
Definition: Fir1.h:145
Fir1::zeroCoeff
void zeroCoeff()
Definition: Fir1.cpp:120
Fir1::getCoeff
void getCoeff(double *coeff_data, unsigned number_of_taps) const
Definition: Fir1.cpp:130
Fir1::~Fir1
~Fir1()
Definition: Fir1.cpp:101
Fir1::getCoeffVector
std::vector< double > getCoeffVector() const
Definition: Fir1.h:175
Fir1::Fir1
Fir1(const double(&_coefficients)[nTaps])
Definition: Fir1.h:43
Fir1::Fir1
Fir1(std::vector< double > _coefficients)
Definition: Fir1.h:57
Fir1::getTapInputPower
double getTapInputPower()
Definition: Fir1.h:189
Fir1::reset
void reset()
Definition: Fir1.cpp:111
Fir1
Definition: Fir1.h:36
Fir1::filter
double filter(double input)
Definition: Fir1.h:94