UniSet  2.6.0
DigitalFilter.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 //--------------------------------------------------------------------------
00017 // Реализация различных цифровых фильтров
00018 //--------------------------------------------------------------------------
00019 #ifndef DigitalFilter_H_
00020 #define DigitalFilter_H_
00021 //--------------------------------------------------------------------------
00022 #include <deque>
00023 #include <vector>
00024 #include <ostream>
00025 #include "PassiveTimer.h"
00026 //--------------------------------------------------------------------------
00027 namespace uniset
00028 {
00029 //--------------------------------------------------------------------------
00030 class DigitalFilter
00031 {
00032     public:
00033         DigitalFilter ( unsigned int bufsize = 5, double T = 0, double lsq = 0.2,
00034                         int iir_thr = 10000, double iir_coeff_prev = 0.5,
00035                         double iir_coeff_new = 0.5 );
00036         ~DigitalFilter ();
00037 
00038         // T <=0 - отключить вторую ступень фильтра
00039         void setSettings( unsigned int bufsize, double T, double lsq,
00040                           int iir_thr, double iir_coeff_prev, double iir_coeff_new );
00041 
00042         // Усреднение с учётом СКОС
00043         // На вход подается новое значение
00044         // возвращается фильтрованное с учётом
00045         // предыдущих значений...
00046         int filter1( int newValue );
00047 
00048         // RC-фильтр
00049         int filterRC( int newVal );
00050 
00051         // медианный фильтр
00052         int median( int newval );
00053 
00054         // адаптивный фильтр по схеме наименьших квадратов
00055         int leastsqr( int newval );
00056 
00057         // рекурсивный фильтр
00058         int filterIIR( int newval );
00059 
00060         // получить текущее фильтрованное значение
00061         int current1();
00062         int currentRC();
00063         int currentMedian();
00064         int currentLS();
00065         int currentIIR();
00066 
00067         // просто добавить очередное значение
00068         void add( int newValue );
00069 
00070         void init( int val );
00071 
00072         // void init( list<int>& data );
00073 
00074         inline int size()
00075         {
00076             return buf.size();
00077         }
00078 
00079         inline double middle()
00080         {
00081             return M;
00082         }
00083         inline double sko()
00084         {
00085             return S;
00086         }
00087 
00088         friend std::ostream& operator<<(std::ostream& os, const DigitalFilter& d);
00089         friend std::ostream& operator<<(std::ostream& os, const DigitalFilter* d);
00090 
00091     private:
00092 
00093         // Первая ступень фильтра
00094         double firstLevel();
00095         // Вторая ступень фильтра, математическая реализация RC фильтра
00096         double secondLevel( double val );
00097 
00098         double Ti;       // Постоянная времени для апериодического звена в милисекундах
00099         double val;      // Текущее значение второй ступени фильтра
00100         double M;        // Среднее арифметическое
00101         double S;        // Среднеквадратичное отклонение
00102         PassiveTimer tmr;
00103 
00104         typedef std::deque<int> FIFOBuffer;
00105         FIFOBuffer buf;
00106         unsigned int maxsize;
00107 
00108         typedef std::vector<int> MedianVector;
00109         MedianVector mvec;
00110         bool mvec_sorted; // флаг, что mvec остортирован (заполнен)
00111 
00112         typedef std::vector<double> Coeff;
00113         Coeff w;        // Вектор коэффициентов для filterIIR
00114 
00115         double lsparam; // Параметр для filterIIR
00116         double ls;      // Последнее значение, возвращённое filterIIR
00117 
00118         int thr;        // Порог для изменений, обрабатываемых рекурсивным фильтром
00119         int prev;       // Последнее значение, возвращённое рекурсивным фильтром
00120 
00121         // Коэффициенты для рекурсивного фильтра
00122         double coeff_prev;
00123         double coeff_new;
00124 };
00125 // -------------------------------------------------------------------------
00126 } // end of namespace uniset
00127 //--------------------------------------------------------------------------
00128 #endif // DigitalFilter_H_
00129 //--------------------------------------------------------------------------