Documentation
Tools for embedded systems
Loading...
Searching...
No Matches
smoother.hpp
1
8
9#ifndef QLIBS_SMOOTHER
10#define QLIBS_SMOOTHER
11
12#include <include/qlibs_types.hpp>
13#include <include/tdl.hpp>
14
18namespace qlibs {
23
27 class smoother {
28 protected:
30 bool init{ true };
31 bool isSetup{ false };
32 static void windowSet( real_t *w,
33 const size_t wsize,
34 const real_t x );
36 public:
37 virtual ~smoother() {}
38
44 bool isInitialized( void ) const
45 {
46 return isSetup;
47 }
48
54 explicit operator bool() const noexcept
55 {
56 return isInitialized();
57 }
58
65 virtual real_t smooth( const real_t x ) = 0;
66
71 inline bool reset( void )
72 {
73 init = true;
74 return true;
75 }
76 };
77
81 class smootherLPF1 : public smoother {
82 protected:
84 real_t alpha{ 0.9_re };
85 real_t y1{ 0.0_re };
87 public:
88 virtual ~smootherLPF1() {}
89
96 bool setup( const real_t a = 0.9_re );
97
104 real_t smooth( const real_t x ) override;
105 };
106
110 class smootherLPF2 : public smoother {
111 protected:
113 real_t y1{ 0.0_re };
114 real_t y2{ 0.0_re };
115 real_t x1{ 0.0_re };
116 real_t x2{ 0.0_re };
117 real_t k{ 0.0_re };
118 real_t a1{ 0.0_re };
119 real_t a2{ 0.0_re };
120 real_t b1{ 0.0_re };
122 public:
123 virtual ~smootherLPF2() {}
124
131 bool setup( const real_t a = 0.9_re );
132
139 real_t smooth( const real_t x ) override;
140 };
141
146 class smootherMWM1 : public smoother, private nonCopyable {
147 protected:
149 real_t *w{ nullptr };
150 size_t wsize{ 0U };
152 public:
153 virtual ~smootherMWM1() {}
154
161 bool setup( real_t *window,
162 const size_t w_size );
163
169 template <size_t windowSize>
170 bool setup( real_t (&window)[ windowSize ] )
171 {
172 return setup( window, windowSize );
173 }
174
181 real_t smooth( const real_t x ) override;
182 };
183
188 class smootherMWM2 : public smoother, public tdl {
189 protected:
191 real_t sum{ 0.0_re };
193 public:
194 virtual ~smootherMWM2() {}
195
202 bool setup( real_t *window,
203 const size_t w_size );
204
210 template <size_t windowSize>
211 bool setup( real_t (&window)[ windowSize ] )
212 {
213 return setup( window, windowSize );
214 }
215
222 real_t smooth( const real_t x ) override;
223 };
224
229 class smootherMOR1 : public smoother, private nonCopyable {
230 protected:
232 real_t *w{ nullptr };
233 real_t m{ 0.0_re };
234 real_t alpha{ 0.8_re };
235 size_t wsize{ 0U };
237 public:
238 virtual ~smootherMOR1() {}
239
247 bool setup( real_t *window,
248 const size_t w_size,
249 const real_t a = 0.9_re );
250
257 template <size_t windowSize>
258 bool setup( real_t (&window)[ windowSize ],
259 const real_t a = 0.9_re )
260 {
261 return setup( window, windowSize, a );
262 }
263
270 real_t smooth( const real_t x ) override;
271 };
272
277 class smootherMOR2 : public smoother, public tdl {
278 protected:
280 real_t sum{ 0.0_re };
281 real_t m{ 0.0_re };
282 real_t alpha{ 0.8_re };
284 public:
285 virtual ~smootherMOR2() {}
286
294 bool setup( real_t *window,
295 const size_t w_size,
296 const real_t a = 0.9_re );
297
304 template <size_t windowSize>
305 bool setup( real_t (&window)[ windowSize ],
306 const real_t a = 0.9_re )
307 {
308 return setup( window, windowSize, a );
309 }
310
317 real_t smooth( const real_t x ) override;
318 };
319
323 class smootherGMWF : public smoother, private nonCopyable {
324 protected:
326 real_t *w{ nullptr };
327 real_t *k{ nullptr };
328 size_t wsize;
330 public:
331 virtual ~smootherGMWF() {}
332
343 bool setup( const real_t sg,
344 const real_t c,
345 real_t *window,
346 real_t *kernel,
347 const size_t wk_size );
348
358 template <size_t windowAndKernelSize>
359 bool setup( const real_t sg,
360 const real_t c,
361 real_t (&window)[ windowAndKernelSize ],
362 real_t (&kernel)[ windowAndKernelSize ] )
363 {
364 return setup( sg, c, window, kernel, windowAndKernelSize );
365 }
366
373 real_t smooth( const real_t x ) override;
374 };
375
379 class smootherEXPW : public smoother {
380 protected:
382 real_t lambda{ 0.8_re };
383 real_t m{ 0_re };
384 real_t w{ 1.0_re };
386 public:
387 virtual ~smootherEXPW() {}
388
394 bool setup( const real_t lam = 0.8_re );
395
402 real_t smooth( const real_t x ) override;
403 };
404
408 class smootherKLMN : public smoother {
409 protected:
411 real_t xS{ 0.0_re }; /* state */
412 real_t A{ 1.0_re }; /* x(n)=A*x(n-1)+u(n),u(n)~N(0,q) */
413 real_t H{ 1.0_re }; /* z(n)=H*x(n)+w(n),w(n)~N(0,r) */
414 real_t q{ 100.0_re }; /* process(predict) noise covariance */
415 real_t r{ 0.9_re }; /* measure noise covariance */
416 real_t p{ 100.0_re }; /* estimated error covariance */
417 real_t gain;
419 public:
420 virtual ~smootherKLMN() {}
421
429 bool setup( const real_t processNoiseCov,
430 const real_t measureNoiseCov,
431 const real_t estErrorCov );
432
439 real_t smooth( const real_t x ) override;
440 };
441
445 class smootherDESF : public smoother {
446 protected:
448 real_t alpha;
449 real_t beta;
450 real_t lt;
451 real_t bt;
452 real_t n;
454 public:
455 virtual ~smootherDESF() {}
456
464 bool setup( const real_t a,
465 const real_t b,
466 const size_t nS );
467
474 real_t smooth( const real_t x ) override;
475 };
476
480 class smootherALNF : public smoother, private nonCopyable {
481 protected:
483 real_t alpha;
484 real_t mu;
485 real_t *w{ nullptr };
486 real_t *w_1{ nullptr };
487 real_t *xx{ nullptr };
488 size_t n{ 0U };
490 public:
491 virtual ~smootherALNF() {}
492
504 bool setup( const real_t a,
505 const real_t m,
506 const size_t wsize,
507 real_t *window,
508 real_t *weights,
509 real_t *w1 = nullptr );
510
520 template <size_t windowSize>
521 bool setup( const real_t a,
522 const real_t m,
523 real_t (&window)[ windowSize ],
524 real_t (&weights)[ windowSize ],
525 real_t (&w1)[ windowSize ] )
526 {
527 return setup( a, m, windowSize, window, weights, w1 );
528 }
529
537 template <size_t windowSize>
538 bool setup( const real_t a,
539 real_t (&window)[ windowSize ],
540 real_t (&weights)[ windowSize ] )
541 {
542 return setup( a, 0.0F, windowSize, window, weights, nullptr);
543 }
544
551 real_t smooth( const real_t x ) override;
552 };
553
555}
556
557
558#endif /*QLIBS_SMOOTHER*/
Adaptive Filter LMS.
Definition smoother.hpp:480
bool setup(const real_t a, const real_t m, real_t(&window)[windowSize], real_t(&weights)[windowSize], real_t(&w1)[windowSize])
Setup an initialize the Adaptive Filter LMS.
Definition smoother.hpp:521
bool setup(const real_t a, real_t(&window)[windowSize], real_t(&weights)[windowSize])
Setup an initialize the Adaptive Filter LMS.
Definition smoother.hpp:538
real_t smooth(const real_t x) override
Perform the smooth operation recursively for the input signal x.
Definition smoother.cpp:400
virtual ~smootherALNF()
Definition smoother.hpp:491
bool setup(const real_t a, const real_t m, const size_t wsize, real_t *window, real_t *weights, real_t *w1=nullptr)
Setup an initialize the Adaptive Filter LMS.
Definition smoother.cpp:377
Double exponential smoothing (Holt’s Method)
Definition smoother.hpp:445
virtual ~smootherDESF()
Definition smoother.hpp:455
real_t smooth(const real_t x) override
Perform the smooth operation recursively for the input signal x.
Definition smoother.cpp:361
bool setup(const real_t a, const real_t b, const size_t nS)
Setup an initialize the Double exponential smoothing instance.
Definition smoother.cpp:342
An Exponential weighting filter.
Definition smoother.hpp:379
real_t smooth(const real_t x) override
Perform the smooth operation recursively for the input signal x.
Definition smoother.cpp:287
virtual ~smootherEXPW()
Definition smoother.hpp:387
bool setup(const real_t lam=0.8_re)
Setup an initialize the Exponential weighting filter.
Definition smoother.cpp:272
A Gaussian filter.
Definition smoother.hpp:323
virtual ~smootherGMWF()
Definition smoother.hpp:331
bool setup(const real_t sg, const real_t c, real_t *window, real_t *kernel, const size_t wk_size)
Setup an initialize the Gaussian filter.
Definition smoother.cpp:222
bool setup(const real_t sg, const real_t c, real_t(&window)[windowAndKernelSize], real_t(&kernel)[windowAndKernelSize])
Setup an initialize the Gaussian filter.
Definition smoother.hpp:359
real_t smooth(const real_t x) override
Perform the smooth operation recursively for the input signal x.
Definition smoother.cpp:263
The smoother base abstract class.
Definition smoother.hpp:27
virtual ~smoother()
Definition smoother.hpp:37
bool reset(void)
Reset the smoother filter.
Definition smoother.hpp:71
bool isInitialized(void) const
Check if the smoother filter has been initialized using setup().
Definition smoother.hpp:44
virtual real_t smooth(const real_t x)=0
Perform the smooth operation recursively for the input signal x.
A scalar Kalman filter.
Definition smoother.hpp:408
bool setup(const real_t processNoiseCov, const real_t measureNoiseCov, const real_t estErrorCov)
Setup an initialize the scalar Kalman filter.
Definition smoother.cpp:303
real_t smooth(const real_t x) override
Perform the smooth operation recursively for the input signal x.
Definition smoother.cpp:322
virtual ~smootherKLMN()
Definition smoother.hpp:420
A 1st order Low-Pass Filter.
Definition smoother.hpp:81
virtual ~smootherLPF1()
Definition smoother.hpp:88
bool setup(const real_t a=0.9_re)
Setup an initialize the 1st order Low-Pass Filter.
Definition smoother.cpp:18
real_t smooth(const real_t x) override
Perform the smooth operation recursively for the input signal x.
Definition smoother.cpp:31
A 2nd order Low-Pass Filter.
Definition smoother.hpp:110
bool setup(const real_t a=0.9_re)
Setup an initialize the 2nd order Low-Pass Filter.
Definition smoother.cpp:45
real_t smooth(const real_t x) override
Perform the smooth operation recursively for the input signal x.
Definition smoother.cpp:67
virtual ~smootherLPF2()
Definition smoother.hpp:123
A Moving Outlier Removal filter.
Definition smoother.hpp:229
real_t smooth(const real_t x) override
Perform the smooth operation recursively for the input signal x.
Definition smoother.cpp:160
virtual ~smootherMOR1()
Definition smoother.hpp:238
bool setup(real_t *window, const size_t w_size, const real_t a=0.9_re)
Setup an initialize the Moving Outlier Removal filter.
Definition smoother.cpp:143
bool setup(real_t(&window)[windowSize], const real_t a=0.9_re)
Setup an initialize the Moving Outlier Removal filter.
Definition smoother.hpp:258
A Moving Outlier Removal filter.
Definition smoother.hpp:277
real_t smooth(const real_t x) override
Perform the smooth operation recursively for the input signal x.
Definition smoother.cpp:199
bool setup(real_t *window, const size_t w_size, const real_t a=0.9_re)
Setup an initialize the Moving Outlier Removal filter.
Definition smoother.cpp:181
bool setup(real_t(&window)[windowSize], const real_t a=0.9_re)
Setup an initialize the Moving Outlier Removal filter.
Definition smoother.hpp:305
virtual ~smootherMOR2()
Definition smoother.hpp:285
A Moving Window Median filter.
Definition smoother.hpp:146
bool setup(real_t *window, const size_t w_size)
Setup an initialize the Moving Window Median filter.
Definition smoother.cpp:87
virtual ~smootherMWM1()
Definition smoother.hpp:153
real_t smooth(const real_t x) override
Perform the smooth operation recursively for the input signal x.
Definition smoother.cpp:102
bool setup(real_t(&window)[windowSize])
Setup an initialize the Moving Window Median filter.
Definition smoother.hpp:170
A Moving Window Median filter.
Definition smoother.hpp:188
real_t smooth(const real_t x) override
Perform the smooth operation recursively for the input signal x.
Definition smoother.cpp:127
virtual ~smootherMWM2()
Definition smoother.hpp:194
bool setup(real_t *window, const size_t w_size)
Setup an initialize the Moving Window Median filter.
Definition smoother.cpp:113
bool setup(real_t(&window)[windowSize])
Setup an initialize the Moving Window Median filter.
Definition smoother.hpp:211
tdl()=default
The qLibs++ library namespace.
Definition mat.hpp:18
float real_t
A type to instantiate a real variable double-precision of 64-bits IEEE 754.
Definition qlibs_types.hpp:43