UniSet 2.32.1
DigitalFilter.h
1/*
2 * Copyright (c) 2015 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Lesser Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16//--------------------------------------------------------------------------
17// Реализация различных цифровых фильтров
18//--------------------------------------------------------------------------
19#ifndef DigitalFilter_H_
20#define DigitalFilter_H_
21//--------------------------------------------------------------------------
22#include <deque>
23#include <vector>
24#include <ostream>
25#include "PassiveTimer.h"
26//--------------------------------------------------------------------------
27namespace uniset
28{
29 //--------------------------------------------------------------------------
31 {
32 public:
33 DigitalFilter ( unsigned int bufsize = 5, double T = 0, double lsq = 0.2,
34 int iir_thr = 10000, double iir_coeff_prev = 0.5,
35 double iir_coeff_new = 0.5 );
37
38 // T <=0 - отключить вторую ступень фильтра
39 void setSettings( unsigned int bufsize, double T, double lsq,
40 int iir_thr, double iir_coeff_prev, double iir_coeff_new );
41
42 // Усреднение с учётом СКОС
43 // На вход подается новое значение
44 // возвращается фильтрованное с учётом
45 // предыдущих значений...
46 int filter1( int newValue );
47
48 // RC-фильтр
49 int filterRC( int newVal );
50
51 // медианный фильтр
52 int median( int newval );
53
54 // адаптивный фильтр по схеме наименьших квадратов
55 int leastsqr( int newval );
56
57 // рекурсивный фильтр
58 int filterIIR( int newval );
59
60 // получить текущее фильтрованное значение
61 int current1();
62 int currentRC();
63 int currentMedian();
64 int currentLS();
65 int currentIIR();
66
67 // просто добавить очередное значение
68 void add( int newValue );
69
70 void init( int val );
71
72 // void init( list<int>& data );
73
74 inline int size()
75 {
76 return buf.size();
77 }
78
79 inline double middle()
80 {
81 return M;
82 }
83 inline double sko()
84 {
85 return S;
86 }
87
88 friend std::ostream& operator<<(std::ostream& os, const DigitalFilter& d);
89 friend std::ostream& operator<<(std::ostream& os, const DigitalFilter* d);
90
91 private:
92
93 // Первая ступень фильтра
94 double firstLevel();
95 // Вторая ступень фильтра, математическая реализация RC фильтра
96 double secondLevel( double val );
97
98 double Ti; // Постоянная времени для апериодического звена в миллисекундах
99 double val; // Текущее значение второй ступени фильтра
100 double M; // Среднее арифметическое
101 double S; // Среднеквадратичное отклонение
102 PassiveTimer tmr;
103
104 typedef std::deque<int> FIFOBuffer;
105 FIFOBuffer buf;
106 unsigned int maxsize;
107
108 typedef std::vector<int> MedianVector;
109 MedianVector mvec;
110 bool mvec_sorted; // флаг, что mvec отсортирован (заполнен)
111
112 typedef std::vector<double> Coeff;
113 Coeff w; // Вектор коэффициентов для filterIIR
114
115 double lsparam; // Параметр для filterIIR
116 double ls; // Последнее значение, возвращённое filterIIR
117
118 int thr; // Порог для изменений, обрабатываемых рекурсивным фильтром
119 int prev; // Последнее значение, возвращённое рекурсивным фильтром
120
121 // Коэффициенты для рекурсивного фильтра
122 double coeff_prev;
123 double coeff_new;
124 };
125 // -------------------------------------------------------------------------
126} // end of namespace uniset
127//--------------------------------------------------------------------------
128#endif // DigitalFilter_H_
129//--------------------------------------------------------------------------
Definition DigitalFilter.h:31
Пассивный таймер
Definition PassiveTimer.h:94
Definition Calibration.h:27