Go to the documentation of this file.00001
00002
00011 #ifndef IIR_FILTER_H
00012 #define IIR_FILTER_H
00013
00014 #include <vector>
00015 #include <deque>
00016 #include <string>
00017 #include <cmath>
00018 #include <iostream>
00019
00024 class IIRFilter
00025 {
00026 public:
00030 IIRFilter(const std::string& error_prefix = "");
00037 IIRFilter(unsigned int dim, std::vector<double>& fb_coeffs, std::vector<double>& ff_coeffs, const std::string& error_prefix = "");
00041 ~IIRFilter() {};
00042
00052 bool setParameter(int dim, std::vector<double>& A, std::vector<double>& B);
00053
00060 bool setParameterAsBiquad(const double f_cutoff, const double Q, const double hz);
00061
00064 void getParameter(int &dim, std::vector<double>&A, std::vector<double>& B);
00065
00068 void reset(double initial_input = 0.0);
00069
00073 double executeFilter(double input) {
00074
00075 return passFilter(input);
00076 };
00080 double passFilter(double input);
00081
00082 private:
00083
00084
00085 int m_dimension;
00086 std::vector<double> m_fb_coefficients;
00087 std::vector<double> m_ff_coefficients;
00088 std::deque<double> m_previous_values;
00089
00090 bool m_initialized;
00091 std::string m_error_prefix;
00092 };
00093
00097 template <class T> class FirstOrderLowPassFilter
00098 {
00099 private:
00100 T prev_value;
00101 double cutoff_freq, dt, const_param;
00102 public:
00103 FirstOrderLowPassFilter (const double _cutoff_freq, const double _dt, const T init_value) : prev_value(init_value), dt(_dt)
00104 {
00105 setCutOffFreq(_cutoff_freq);
00106 };
00107 ~FirstOrderLowPassFilter()
00108 {
00109 };
00110 T passFilter (const T& value)
00111 {
00112 prev_value = 1.0/(1+const_param) * prev_value + const_param/(1+const_param) * value;
00113 return prev_value;
00114 };
00115 void reset (const T& value) { prev_value = value; };
00116 void setCutOffFreq (const double f)
00117 {
00118 cutoff_freq = f;
00119 const_param = 2 * M_PI * cutoff_freq * dt;
00120 };
00121 double getCutOffFreq () const { return cutoff_freq; };
00122 T getCurrentValue () const { return prev_value; };
00123 };
00124
00125 #endif // IIRFilter_H