IT++ Logo
filter.h
Go to the documentation of this file.
1 
29 #ifndef FILTER_H
30 #define FILTER_H
31 
32 #include <itpp/base/vec.h>
33 #include <itpp/itexports.h>
34 
35 namespace itpp
36 {
37 
53 template <class T1, class T2, class T3>
54 class Filter
55 {
56 public:
58  Filter() {}
60  virtual T3 operator()(const T1 Sample) { return filter(Sample); }
62  virtual Vec<T3> operator()(const Vec<T1> &v);
64  virtual ~Filter() {}
65 protected:
70  virtual T3 filter(const T1 Sample) = 0;
71 };
72 
97 template <class T1, class T2, class T3>
98 class MA_Filter : public Filter<T1, T2, T3>
99 {
100 public:
102  explicit MA_Filter();
104  explicit MA_Filter(const Vec<T2> &b);
106  virtual ~MA_Filter() { }
108  Vec<T2> get_coeffs() const { return coeffs; }
110  void set_coeffs(const Vec<T2> &b);
112  void clear() { mem.clear(); }
114  Vec<T3> get_state() const;
116  void set_state(const Vec<T3> &state);
117 
118 private:
119  virtual T3 filter(const T1 Sample);
120 
121  Vec<T3> mem;
122  Vec<T2> coeffs;
123  int inptr;
124  bool init;
125 };
126 
151 template <class T1, class T2, class T3>
152 class AR_Filter : public Filter<T1, T2, T3>
153 {
154 public:
156  explicit AR_Filter();
158  explicit AR_Filter(const Vec<T2> &a);
160  virtual ~AR_Filter() { }
162  Vec<T2> get_coeffs() const { return coeffs; }
164  void set_coeffs(const Vec<T2> &a);
166  void clear() { mem.clear(); }
168  Vec<T3> get_state() const;
170  void set_state(const Vec<T3> &state);
171 
172 private:
173  virtual T3 filter(const T1 Sample);
174 
175  Vec<T3> mem;
176  Vec<T2> coeffs;
177  Vec<T2> a0;
178  int inptr;
179  bool init;
180 };
181 
182 
209 template <class T1, class T2, class T3>
210 class ARMA_Filter : public Filter<T1, T2, T3>
211 {
212 public:
214  explicit ARMA_Filter();
216  explicit ARMA_Filter(const Vec<T2> &b, const Vec<T2> &a);
218  virtual ~ARMA_Filter() { }
220  Vec<T2> get_coeffs_a() const { return acoeffs; }
222  Vec<T2> get_coeffs_b() const { return bcoeffs; }
224  void get_coeffs(Vec<T2> &b, Vec<T2> &a) const { b = bcoeffs; a = acoeffs; }
226  void set_coeffs(const Vec<T2> &b, const Vec<T2> &a);
228  void clear() { mem.clear(); }
230  Vec<T3> get_state() const;
232  void set_state(const Vec<T3> &state);
233 
234 private:
235  virtual T3 filter(const T1 Sample);
236 
237  Vec<T3> mem;
238  Vec<T2> acoeffs, bcoeffs;
239  int inptr;
240  bool init;
241 };
242 
243 
244 
268 ITPP_EXPORT vec filter(const vec &b, const vec &a, const vec &input);
269 ITPP_EXPORT cvec filter(const vec &b, const vec &a, const cvec &input);
270 ITPP_EXPORT cvec filter(const cvec &b, const cvec &a, const cvec &input);
271 ITPP_EXPORT cvec filter(const cvec &b, const cvec &a, const vec &input);
272 
273 ITPP_EXPORT vec filter(const vec &b, const int one, const vec &input);
274 ITPP_EXPORT cvec filter(const vec &b, const int one, const cvec &input);
275 ITPP_EXPORT cvec filter(const cvec &b, const int one, const cvec &input);
276 ITPP_EXPORT cvec filter(const cvec &b, const int one, const vec &input);
277 
278 ITPP_EXPORT vec filter(const int one, const vec &a, const vec &input);
279 ITPP_EXPORT cvec filter(const int one, const vec &a, const cvec &input);
280 ITPP_EXPORT cvec filter(const int one, const cvec &a, const cvec &input);
281 ITPP_EXPORT cvec filter(const int one, const cvec &a, const vec &input);
282 
283 
284 ITPP_EXPORT vec filter(const vec &b, const vec &a, const vec &input, const vec &state_in, vec &state_out);
285 ITPP_EXPORT cvec filter(const vec &b, const vec &a, const cvec &input, const cvec &state_in, cvec &state_out);
286 ITPP_EXPORT cvec filter(const cvec &b, const cvec &a, const cvec &input, const cvec &state_in, cvec &state_out);
287 ITPP_EXPORT cvec filter(const cvec &b, const cvec &a, const vec &input, const cvec &state_in, cvec &state_out);
288 
289 ITPP_EXPORT vec filter(const vec &b, const int one, const vec &input, const vec &state_in, vec &state_out);
290 ITPP_EXPORT cvec filter(const vec &b, const int one, const cvec &input, const cvec &state_in, cvec &state_out);
291 ITPP_EXPORT cvec filter(const cvec &b, const int one, const cvec &input, const cvec &state_in, cvec &state_out);
292 ITPP_EXPORT cvec filter(const cvec &b, const int one, const vec &input, const cvec &state_in, cvec &state_out);
293 
294 ITPP_EXPORT vec filter(const int one, const vec &a, const vec &input, const vec &state_in, vec &state_out);
295 ITPP_EXPORT cvec filter(const int one, const vec &a, const cvec &input, const cvec &state_in, cvec &state_out);
296 ITPP_EXPORT cvec filter(const int one, const cvec &a, const cvec &input, const cvec &state_in, cvec &state_out);
297 ITPP_EXPORT cvec filter(const int one, const cvec &a, const vec &input, const cvec &state_in, cvec &state_out);
306 ITPP_EXPORT vec fir1(int N, double cutoff);
307 
308 //----------------------------------------------------------------------------
309 // Implementation of templated functions starts here
310 //----------------------------------------------------------------------------
311 
312 //---------------------- class Filter ----------------------------
313 
314 template <class T1, class T2, class T3>
316 {
317  Vec<T3> y(x.length());
318 
319  for (int i = 0; i < x.length(); i++) {
320  y[i] = filter(x[i]);
321  }
322 
323  return y;
324 }
325 
326 //-------------------------- class MA_Filter ---------------------------------
327 
328 template <class T1, class T2, class T3>
330 {
331  inptr = 0;
332  init = false;
333 }
334 
335 template <class T1, class T2, class T3>
337 {
338  set_coeffs(b);
339 }
340 
341 
342 template <class T1, class T2, class T3>
344 {
345  it_assert(b.size() > 0, "MA_Filter: size of filter is 0!");
346 
347  coeffs = b;
348  mem.set_size(coeffs.size(), false);
349  mem.clear();
350  inptr = 0;
351  init = true;
352 }
353 
354 template <class T1, class T2, class T3>
356 {
357  it_assert(init == true, "MA_Filter: filter coefficients are not set!");
358 
359  int offset = inptr;
360  Vec<T3> state(mem.size());
361 
362  for (int n = 0; n < mem.size(); n++) {
363  state(n) = mem(offset);
364  offset = (offset + 1) % mem.size();
365  }
366 
367  return state;
368 }
369 
370 template <class T1, class T2, class T3>
372 {
373  it_assert(init == true, "MA_Filter: filter coefficients are not set!");
374  it_assert(state.size() == mem.size(), "MA_Filter: Invalid state vector!");
375 
376  mem = state;
377  inptr = 0;
378 }
379 
380 template <class T1, class T2, class T3>
381 T3 MA_Filter<T1, T2, T3>::filter(const T1 Sample)
382 {
383  it_assert(init == true, "MA_Filter: Filter coefficients are not set!");
384  T3 s = 0;
385 
386  mem(inptr) = Sample;
387  int L = mem.length() - inptr;
388 
389  for (int i = 0; i < L; i++) {
390  s += coeffs(i) * mem(inptr + i);
391  }
392  for (int i = 0; i < inptr; i++) {
393  s += coeffs(L + i) * mem(i);
394  }
395 
396  inptr--;
397  if (inptr < 0)
398  inptr += mem.length();
399 
400  return s;
401 }
402 
403 //---------------------- class AR_Filter ----------------------------------
404 
405 template <class T1, class T2, class T3>
407 {
408  inptr = 0;
409  init = false;
410 }
411 
412 template <class T1, class T2, class T3>
414 {
415  set_coeffs(a);
416 }
417 
418 template <class T1, class T2, class T3>
420 {
421  it_assert(a.size() > 0, "AR_Filter: size of filter is 0!");
422  it_assert(a(0) != T2(0), "AR_Filter: a(0) cannot be 0!");
423 
424  a0.set_size(1);//needed to keep the first coefficient for future reuse
425  a0(0) = a(0);
426  coeffs = a / a0(0);
427 
428  mem.set_size(coeffs.size() - 1, false);
429  mem.clear();
430  inptr = 0;
431  init = true;
432 }
433 
434 
435 template <class T1, class T2, class T3>
437 {
438  it_assert(init == true, "AR_Filter: filter coefficients are not set!");
439 
440  int offset = inptr;
441  Vec<T3> state(mem.size());
442 
443  for (int n = 0; n < mem.size(); n++) {
444  state(n) = mem(offset);
445  offset = (offset + 1) % mem.size();
446  }
447 
448  return state;
449 }
450 
451 template <class T1, class T2, class T3>
453 {
454  it_assert(init == true, "AR_Filter: filter coefficients are not set!");
455  it_assert(state.size() == mem.size(), "AR_Filter: Invalid state vector!");
456 
457  mem = state;
458  inptr = 0;
459 }
460 
461 template <class T1, class T2, class T3>
462 T3 AR_Filter<T1, T2, T3>::filter(const T1 Sample)
463 {
464  it_assert(init == true, "AR_Filter: Filter coefficients are not set!");
465  T3 s = Sample;
466 
467  if (mem.size() == 0)
468  return (s / a0(0));
469 
470  int L = mem.size() - inptr;
471  for (int i = 0; i < L; i++) {
472  s -= mem(i + inptr) * coeffs(i + 1); // All coeffs except a(0)
473  }
474  for (int i = 0; i < inptr; i++) {
475  s -= mem(i) * coeffs(L + i + 1); // All coeffs except a(0)
476  }
477 
478  inptr--;
479  if (inptr < 0)
480  inptr += mem.size();
481  mem(inptr) = s;
482 
483  return (s / a0(0));
484 }
485 
486 
487 //---------------------- class ARMA_Filter ----------------------------------
488 template <class T1, class T2, class T3>
490 {
491  inptr = 0;
492  init = false;
493 }
494 
495 template <class T1, class T2, class T3>
496 ARMA_Filter<T1, T2, T3>::ARMA_Filter(const Vec<T2> &b, const Vec<T2> &a) : Filter<T1, T2, T3>()
497 {
498  set_coeffs(b, a);
499 }
500 
501 template <class T1, class T2, class T3>
503 {
504  it_assert(a.size() > 0 && b.size() > 0, "ARMA_Filter: size of filter is 0!");
505  it_assert(a(0) != T2(0), "ARMA_Filter: a(0) cannot be 0!");
506 
507  acoeffs = a / a(0);
508  bcoeffs = b / a(0);
509 
510  mem.set_size(std::max(a.size(), b.size()) - 1, false);
511  mem.clear();
512  inptr = 0;
513  init = true;
514 }
515 
516 template <class T1, class T2, class T3>
518 {
519  it_assert(init == true, "ARMA_Filter: filter coefficients are not set!");
520 
521  int offset = inptr;
522  Vec<T3> state(mem.size());
523 
524  for (int n = 0; n < mem.size(); n++) {
525  state(n) = mem(offset);
526  offset = (offset + 1) % mem.size();
527  }
528 
529  return state;
530 }
531 
532 template <class T1, class T2, class T3>
534 {
535  it_assert(init == true, "ARMA_Filter: filter coefficients are not set!");
536  it_assert(state.size() == mem.size(), "ARMA_Filter: Invalid state vector!");
537 
538  mem = state;
539  inptr = 0;
540 }
541 
542 template <class T1, class T2, class T3>
543 T3 ARMA_Filter<T1, T2, T3>::filter(const T1 Sample)
544 {
545  it_assert(init == true, "ARMA_Filter: Filter coefficients are not set!");
546  T3 z = Sample;
547  T3 s;
548 
549  for (int i = 0; i < acoeffs.size() - 1; i++) { // All AR-coeff except a(0).
550  z -= mem((i + inptr) % mem.size()) * acoeffs(i + 1);
551  }
552  s = z * bcoeffs(0);
553 
554  for (int i = 0; i < bcoeffs.size() - 1; i++) { // All MA-coeff except b(0).
555  s += mem((i + inptr) % mem.size()) * bcoeffs(i + 1);
556  }
557 
558  inptr--;
559  if (inptr < 0)
560  inptr += mem.size();
561  mem(inptr) = z;
562 
563  mem(inptr) = z; // Store in the internal state.
564 
565  return s;
566 }
567 
569 
570 // ----------------------------------------------------------------------
571 // Instantiations
572 // ----------------------------------------------------------------------
573 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT MA_Filter<double, double, double>;
574 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT MA_Filter< double, std::complex<double>,
575  std::complex<double> >;
576 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT MA_Filter< std::complex<double>, double,
577  std::complex<double> >;
578 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT MA_Filter< std::complex<double>, std::complex<double>,
579  std::complex<double> >;
580 
581 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT AR_Filter<double, double, double>;
582 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT AR_Filter< double, std::complex<double>,
583  std::complex<double> >;
584 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT AR_Filter< std::complex<double>,
585  double, std::complex<double> >;
586 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT AR_Filter< std::complex<double>, std::complex<double>,
587  std::complex<double> >;
588 
589 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT ARMA_Filter<double, double, double>;
590 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT ARMA_Filter< double, std::complex<double>,
591  std::complex<double> >;
592 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT ARMA_Filter< std::complex<double>,
593  double, std::complex<double> >;
594 ITPP_EXPORT_TEMPLATE template class ITPP_EXPORT ARMA_Filter< std::complex<double>, std::complex<double>,
595  std::complex<double> >;
596 
598 
599 } // namespace itpp
600 
601 #endif // #ifndef FILTER_H
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:25 for IT++ by Doxygen 1.8.2