| 1 | // digitalfilter.cxx - a selection of digital filters |
| 2 | // |
| 3 | // Written by Torsten Dreyer |
| 4 | // Based heavily on work created by Curtis Olson, started January 2004. |
| 5 | // |
| 6 | // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt |
| 7 | // Copyright (C) 2010 Torsten Dreyer - Torsten (at) t3r (dot) de |
| 8 | // |
| 9 | // Washout/high-pass filter, lead-lag filter and integrator added. |
| 10 | // low-pass and lag aliases added to Exponential filter, |
| 11 | // rate-limit added. A J Teeder 2013 |
| 12 | // |
| 13 | // This program is free software; you can redistribute it and/or |
| 14 | // modify it under the terms of the GNU General Public License as |
| 15 | // published by the Free Software Foundation; either version 2 of the |
| 16 | // License, or (at your option) any later version. |
| 17 | // |
| 18 | // This program is distributed in the hope that it will be useful, but |
| 19 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | // General Public License for more details. |
| 22 | // |
| 23 | // You should have received a copy of the GNU General Public License |
| 24 | // along with this program; if not, write to the Free Software |
| 25 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 26 | // |
| 27 | |
| 28 | #include "digitalfilter.hxx" |
| 29 | #include <GUI/Highlight.hxx> |
| 30 | #include <Main/globals.hxx> |
| 31 | |
| 32 | #include <deque> |
| 33 | #include <algorithm> |
| 34 | |
| 35 | #include <simgear/misc/strutils.hxx> |
| 36 | |
| 37 | namespace FGXMLAutopilot |
| 38 | { |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * |
| 43 | */ |
| 44 | class DigitalFilterImplementation: |
| 45 | public SGReferenced |
| 46 | { |
| 47 | public: |
| 48 | virtual ~DigitalFilterImplementation() {} |
| 49 | DigitalFilterImplementation(); |
| 50 | virtual void initialize( double initvalue ) {} |
| 51 | virtual double compute( double dt, double input ) = 0; |
| 52 | virtual bool configure( SGPropertyNode& cfg_node, |
| 53 | const std::string& cfg_name, |
| 54 | SGPropertyNode& prop_root ) = 0; |
| 55 | |
| 56 | void setDigitalFilter( DigitalFilter * digitalFilter ) { _digitalFilter = digitalFilter; } |
| 57 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const = 0; |
| 58 | protected: |
| 59 | DigitalFilter * _digitalFilter = nullptr; |
| 60 | }; |
| 61 | |
| 62 | /* --------------------------------------------------------------------------------- */ |
| 63 | /* --------------------------------------------------------------------------------- */ |
| 64 | class GainFilterImplementation : public DigitalFilterImplementation { |
| 65 | protected: |
| 66 | simgear::ValueList _gainInput; |
| 67 | bool configure(SGPropertyNode& cfg_node, |
| 68 | const std::string& cfg_name, |
| 69 | SGPropertyNode& prop_root); |
| 70 | public: |
| 71 | GainFilterImplementation() : _gainInput(1.0) {} |
| 72 | double compute( double dt, double input ); |
| 73 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const |
| 74 | { |
| 75 | _gainInput.collectDependentProperties(props); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | class ReciprocalFilterImplementation : public GainFilterImplementation { |
| 80 | public: |
| 81 | double compute( double dt, double input ); |
| 82 | }; |
| 83 | |
| 84 | class DerivativeFilterImplementation : public GainFilterImplementation { |
| 85 | simgear::ValueList _TfInput; |
| 86 | double _input_1; |
| 87 | bool configure( SGPropertyNode& cfg_node, |
| 88 | const std::string& cfg_name, |
| 89 | SGPropertyNode& prop_root ); |
| 90 | public: |
| 91 | DerivativeFilterImplementation(); |
| 92 | double compute( double dt, double input ); |
| 93 | virtual void initialize( double initvalue ); |
| 94 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const |
| 95 | { |
| 96 | GainFilterImplementation::collectDependentProperties(props); |
| 97 | _TfInput.collectDependentProperties(props); |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | class ExponentialFilterImplementation : public GainFilterImplementation { |
| 102 | protected: |
| 103 | simgear::ValueList _TfInput; |
| 104 | bool configure(SGPropertyNode& cfg_node, |
| 105 | const std::string& cfg_name, |
| 106 | SGPropertyNode& prop_root); |
| 107 | bool _isSecondOrder; |
| 108 | double _output_1, _output_2; |
| 109 | public: |
| 110 | ExponentialFilterImplementation(); |
| 111 | double compute( double dt, double input ); |
| 112 | virtual void initialize( double initvalue ); |
| 113 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const |
| 114 | { |
| 115 | GainFilterImplementation::collectDependentProperties(props); |
| 116 | _TfInput.collectDependentProperties(props); |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | class MovingAverageFilterImplementation : public DigitalFilterImplementation { |
| 121 | protected: |
| 122 | simgear::ValueList _samplesInput; |
| 123 | double _output_1; |
| 124 | std::deque<double> _inputQueue; |
| 125 | bool configure(SGPropertyNode& cfg_node, |
| 126 | const std::string& cfg_name, |
| 127 | SGPropertyNode& prop_root); |
| 128 | public: |
| 129 | MovingAverageFilterImplementation(); |
| 130 | double compute( double dt, double input ); |
| 131 | virtual void initialize( double initvalue ); |
| 132 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const |
| 133 | { |
| 134 | _samplesInput.collectDependentProperties(props); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | class NoiseSpikeFilterImplementation : public DigitalFilterImplementation { |
| 139 | protected: |
| 140 | double _output_1; |
| 141 | simgear::ValueList _rateOfChangeInput; |
| 142 | bool configure( SGPropertyNode& cfg_node, |
| 143 | const std::string& cfg_name, |
| 144 | SGPropertyNode& prop_root ); |
| 145 | public: |
| 146 | NoiseSpikeFilterImplementation(); |
| 147 | double compute( double dt, double input ); |
| 148 | virtual void initialize( double initvalue ); |
| 149 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const |
| 150 | { |
| 151 | _rateOfChangeInput.collectDependentProperties(props); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | class RateLimitFilterImplementation : public DigitalFilterImplementation { |
| 156 | protected: |
| 157 | double _output_1; |
| 158 | simgear::ValueList _rateOfChangeMax; |
| 159 | simgear::ValueList _rateOfChangeMin; |
| 160 | bool configure( SGPropertyNode& cfg_node, |
| 161 | const std::string& cfg_name, |
| 162 | SGPropertyNode& prop_root ); |
| 163 | public: |
| 164 | RateLimitFilterImplementation(); |
| 165 | double compute( double dt, double input ); |
| 166 | virtual void initialize( double initvalue ); |
| 167 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const |
| 168 | { |
| 169 | _rateOfChangeMax.collectDependentProperties(props); |
| 170 | _rateOfChangeMin.collectDependentProperties(props); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | class IntegratorFilterImplementation : public GainFilterImplementation { |
| 175 | protected: |
| 176 | simgear::ValueList _TfInput; |
| 177 | simgear::ValueList _minInput; |
| 178 | simgear::ValueList _maxInput; |
| 179 | double _input_1; |
| 180 | double _output_1; |
| 181 | bool configure(SGPropertyNode& cfg_node, |
| 182 | const std::string& cfg_name, |
| 183 | SGPropertyNode& prop_root); |
| 184 | public: |
| 185 | IntegratorFilterImplementation(); |
| 186 | double compute( double dt, double input ); |
| 187 | virtual void initialize( double initvalue ); |
| 188 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const |
| 189 | { |
| 190 | GainFilterImplementation::collectDependentProperties(props); |
| 191 | _TfInput.collectDependentProperties(props); |
| 192 | _minInput.collectDependentProperties(props); |
| 193 | _maxInput.collectDependentProperties(props); |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | // integrates x" + ax' + bx + c = 0 |
| 198 | class DampedOscillationFilterImplementation : public GainFilterImplementation { |
| 199 | protected: |
| 200 | simgear::ValueList _aInput; |
| 201 | simgear::ValueList _bInput; |
| 202 | simgear::ValueList _cInput; |
| 203 | double _x2; |
| 204 | double _x1; |
| 205 | double _x0; |
| 206 | bool configure(SGPropertyNode& cfg_node, |
| 207 | const std::string& cfg_name, |
| 208 | SGPropertyNode& prop_root); |
| 209 | public: |
| 210 | DampedOscillationFilterImplementation(); |
| 211 | double compute( double dt, double input ); |
| 212 | virtual void initialize( double initvalue ); |
| 213 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const |
| 214 | { |
| 215 | GainFilterImplementation::collectDependentProperties(props); |
| 216 | _aInput.collectDependentProperties(props); |
| 217 | _bInput.collectDependentProperties(props); |
| 218 | _cInput.collectDependentProperties(props); |
| 219 | } |
| 220 | }; |
| 221 | |
| 222 | class HighPassFilterImplementation : public GainFilterImplementation { |
| 223 | protected: |
| 224 | simgear::ValueList _TfInput; |
| 225 | double _input_1; |
| 226 | double _output_1; |
| 227 | bool configure(SGPropertyNode& cfg_node, |
| 228 | const std::string& cfg_name, |
| 229 | SGPropertyNode& prop_root); |
| 230 | public: |
| 231 | HighPassFilterImplementation(); |
| 232 | double compute( double dt, double input ); |
| 233 | virtual void initialize( double initvalue ); |
| 234 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const |
| 235 | { |
| 236 | GainFilterImplementation::collectDependentProperties(props); |
| 237 | _TfInput.collectDependentProperties(props); |
| 238 | } |
| 239 | }; |
| 240 | class LeadLagFilterImplementation : public GainFilterImplementation { |
| 241 | protected: |
| 242 | simgear::ValueList _TfaInput; |
| 243 | simgear::ValueList _TfbInput; |
| 244 | double _input_1; |
| 245 | double _output_1; |
| 246 | bool configure(SGPropertyNode& cfg_node, |
| 247 | const std::string& cfg_name, |
| 248 | SGPropertyNode& prop_root); |
| 249 | public: |
| 250 | LeadLagFilterImplementation(); |
| 251 | double compute( double dt, double input ); |
| 252 | virtual void initialize( double initvalue ); |
| 253 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const |
| 254 | { |
| 255 | GainFilterImplementation::collectDependentProperties(props); |
| 256 | _TfaInput.collectDependentProperties(props); |
| 257 | _TfbInput.collectDependentProperties(props); |
| 258 | } |
| 259 | }; |
| 260 | |
| 261 | class CoherentNoiseFilterImplementation : public DigitalFilterImplementation |
| 262 | { |
| 263 | protected: |
| 264 | simgear::ValueList _amplitude; |
| 265 | |
| 266 | std::vector<double> _discreteValues; |
| 267 | bool _absoluteVal = false; |
| 268 | size_t _numDiscreteValues = 1024; |
| 269 | |
| 270 | bool configure(SGPropertyNode& cfg_node, |
| 271 | const std::string& cfg_name, |
| 272 | SGPropertyNode& prop_root) override; |
| 273 | |
| 274 | public: |
| 275 | CoherentNoiseFilterImplementation(); |
| 276 | double compute(double dt, double input) override; |
| 277 | void initialize(double initvalue) override; |
| 278 | virtual void collectDependentProperties(std::set<const SGPropertyNode*>& props) const |
| 279 | { |
| 280 | _amplitude.collectDependentProperties(props); |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | /* --------------------------------------------------------------------------------- */ |
| 285 | /* --------------------------------------------------------------------------------- */ |
| 286 | |
| 287 | } // namespace FGXMLAutopilot |
| 288 | |
| 289 | using namespace FGXMLAutopilot; |
| 290 | |
| 291 | //------------------------------------------------------------------------------ |
| 292 | DigitalFilterImplementation::DigitalFilterImplementation() : |
| 293 | _digitalFilter(NULL) |
| 294 | { |
| 295 | |
| 296 | } |
| 297 | |
| 298 | //------------------------------------------------------------------------------ |
| 299 | double GainFilterImplementation::compute( double dt, double input ) |
| 300 | { |
| 301 | return _gainInput.get_value() * input; |
| 302 | } |
| 303 | |
| 304 | bool GainFilterImplementation::configure( SGPropertyNode& cfg_node, |
| 305 | const std::string& cfg_name, |
| 306 | SGPropertyNode& prop_root ) |
| 307 | { |
| 308 | if (cfg_name == "gain" ) { |
| 309 | _gainInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | /* --------------------------------------------------------------------------------- */ |
| 317 | /* --------------------------------------------------------------------------------- */ |
| 318 | |
| 319 | double ReciprocalFilterImplementation::compute( double dt, double input ) |
| 320 | { |
| 321 | if( input >= -SGLimitsd::min() && input <= SGLimitsd::min() ) |
| 322 | return SGLimitsd::max(); |
| 323 | |
| 324 | return _gainInput.get_value() / input; |
| 325 | |
| 326 | } |
| 327 | |
| 328 | /* --------------------------------------------------------------------------------- */ |
| 329 | /* --------------------------------------------------------------------------------- */ |
| 330 | |
| 331 | DerivativeFilterImplementation::DerivativeFilterImplementation() : |
| 332 | _input_1(0.0) |
| 333 | { |
| 334 | } |
| 335 | |
| 336 | void DerivativeFilterImplementation::initialize( double initvalue ) |
| 337 | { |
| 338 | _input_1 = initvalue; |
| 339 | } |
| 340 | |
| 341 | //------------------------------------------------------------------------------ |
| 342 | bool DerivativeFilterImplementation::configure( SGPropertyNode& cfg_node, |
| 343 | const std::string& cfg_name, |
| 344 | SGPropertyNode& prop_root ) |
| 345 | { |
| 346 | if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) ) |
| 347 | return true; |
| 348 | |
| 349 | if (cfg_name == "filter-time" ) { |
| 350 | _TfInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | double DerivativeFilterImplementation::compute( double dt, double input ) |
| 358 | { |
| 359 | double output = (input - _input_1) * _TfInput.get_value() * _gainInput.get_value() / dt; |
| 360 | _input_1 = input; |
| 361 | return output; |
| 362 | |
| 363 | } |
| 364 | |
| 365 | /* --------------------------------------------------------------------------------- */ |
| 366 | /* --------------------------------------------------------------------------------- */ |
| 367 | |
| 368 | MovingAverageFilterImplementation::MovingAverageFilterImplementation() : |
| 369 | _output_1(0.0) |
| 370 | { |
| 371 | } |
| 372 | |
| 373 | void MovingAverageFilterImplementation::initialize( double initvalue ) |
| 374 | { |
| 375 | _output_1 = initvalue; |
| 376 | } |
| 377 | |
| 378 | double MovingAverageFilterImplementation::compute( double dt, double input ) |
| 379 | { |
| 380 | typedef std::deque<double>::size_type size_type; |
| 381 | size_type samples = _samplesInput.get_value(); |
| 382 | |
| 383 | if (_inputQueue.size() != samples) { |
| 384 | // For constant size filters, this code executed once. |
| 385 | bool shrunk = _inputQueue.size() > samples; |
| 386 | _inputQueue.resize(samples, _output_1); |
| 387 | if (shrunk) { |
| 388 | _output_1 = 0.0; |
| 389 | for (size_type ii = 0; ii < samples; ii++) |
| 390 | _output_1 += _inputQueue[ii]; |
| 391 | _output_1 /= samples; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | double output_0 = _output_1 + (input - _inputQueue.back()) / samples; |
| 396 | |
| 397 | _output_1 = output_0; |
| 398 | _inputQueue.pop_back(); |
| 399 | _inputQueue.push_front(input); |
| 400 | return output_0; |
| 401 | } |
| 402 | |
| 403 | bool MovingAverageFilterImplementation::configure( SGPropertyNode& cfg_node, |
| 404 | const std::string& cfg_name, |
| 405 | SGPropertyNode& prop_root ) |
| 406 | { |
| 407 | if (cfg_name == "samples" ) { |
| 408 | _samplesInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 409 | return true; |
| 410 | } |
| 411 | |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | /* --------------------------------------------------------------------------------- */ |
| 416 | /* --------------------------------------------------------------------------------- */ |
| 417 | |
| 418 | NoiseSpikeFilterImplementation::NoiseSpikeFilterImplementation() : |
| 419 | _output_1(0.0) |
| 420 | { |
| 421 | } |
| 422 | |
| 423 | void NoiseSpikeFilterImplementation::initialize( double initvalue ) |
| 424 | { |
| 425 | _output_1 = initvalue; |
| 426 | } |
| 427 | |
| 428 | double NoiseSpikeFilterImplementation::compute( double dt, double input ) |
| 429 | { |
| 430 | double delta = input - _output_1; |
| 431 | if( fabs(delta) <= SGLimitsd::min() ) return input; // trivial |
| 432 | |
| 433 | double maxChange = _rateOfChangeInput.get_value() * dt; |
| 434 | const auto periodical = _digitalFilter->getPeriodicalValue(); |
| 435 | if( periodical ) delta = periodical->normalizeSymmetric( delta ); |
| 436 | |
| 437 | if( fabs(delta) <= maxChange ) |
| 438 | return (_output_1 = input); |
| 439 | else |
| 440 | return (_output_1 = _output_1 + copysign( maxChange, delta )); |
| 441 | } |
| 442 | |
| 443 | //------------------------------------------------------------------------------ |
| 444 | bool NoiseSpikeFilterImplementation::configure( SGPropertyNode& cfg_node, |
| 445 | const std::string& cfg_name, |
| 446 | SGPropertyNode& prop_root ) |
| 447 | { |
| 448 | if (cfg_name == "max-rate-of-change" ) { |
| 449 | _rateOfChangeInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | /* --------------------------------------------------------------------------------- */ |
| 457 | |
| 458 | RateLimitFilterImplementation::RateLimitFilterImplementation() : |
| 459 | _output_1(0.0) |
| 460 | { |
| 461 | } |
| 462 | |
| 463 | void RateLimitFilterImplementation::initialize( double initvalue ) |
| 464 | { |
| 465 | _output_1 = initvalue; |
| 466 | } |
| 467 | |
| 468 | double RateLimitFilterImplementation::compute( double dt, double input ) |
| 469 | { |
| 470 | double delta = input - _output_1; |
| 471 | double output; |
| 472 | |
| 473 | if( fabs(delta) <= SGLimitsd::min() ) return input; // trivial |
| 474 | |
| 475 | double maxChange = _rateOfChangeMax.get_value() * dt; |
| 476 | double minChange = _rateOfChangeMin.get_value() * dt; |
| 477 | // const PeriodicalValue * periodical = _digitalFilter->getPeriodicalValue(); |
| 478 | // if( periodical ) delta = periodical->normalizeSymmetric( delta ); |
| 479 | |
| 480 | output = input; |
| 481 | if(delta >= maxChange ) output = _output_1 + maxChange; |
| 482 | if(delta <= minChange ) output = _output_1 + minChange; |
| 483 | _output_1 = output; |
| 484 | |
| 485 | return (output); |
| 486 | } |
| 487 | |
| 488 | bool RateLimitFilterImplementation::configure( SGPropertyNode& cfg_node, |
| 489 | const std::string& cfg_name, |
| 490 | SGPropertyNode& prop_root ) |
| 491 | { |
| 492 | // std::cout << "RateLimitFilterImplementation " << cfg_name << std::endl; |
| 493 | if (cfg_name == "max-rate-of-change" ) { |
| 494 | _rateOfChangeMax.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 495 | return true; |
| 496 | } |
| 497 | if (cfg_name == "min-rate-of-change" ) { |
| 498 | _rateOfChangeMin.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 499 | return true; |
| 500 | } |
| 501 | |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | /* --------------------------------------------------------------------------------- */ |
| 506 | /* --------------------------------------------------------------------------------- */ |
| 507 | |
| 508 | ExponentialFilterImplementation::ExponentialFilterImplementation() |
| 509 | : _isSecondOrder(false), |
| 510 | _output_1(0.0), |
| 511 | _output_2(0.0) |
| 512 | { |
| 513 | } |
| 514 | |
| 515 | void ExponentialFilterImplementation::initialize( double initvalue ) |
| 516 | { |
| 517 | _output_1 = _output_2 = initvalue; |
| 518 | } |
| 519 | |
| 520 | double ExponentialFilterImplementation::compute( double dt, double input ) |
| 521 | { |
| 522 | input = GainFilterImplementation::compute( dt, input ); |
| 523 | double tf = _TfInput.get_value(); |
| 524 | |
| 525 | double output_0; |
| 526 | |
| 527 | // avoid negative filter times |
| 528 | // and div by zero if -tf == dt |
| 529 | |
| 530 | double alpha = tf > 0.0 ? 1 / ((tf/dt) + 1) : 1.0; |
| 531 | |
| 532 | if(_isSecondOrder) { |
| 533 | output_0 = alpha * alpha * input + |
| 534 | 2 * (1 - alpha) * _output_1 - |
| 535 | (1 - alpha) * (1 - alpha) * _output_2; |
| 536 | } else { |
| 537 | output_0 = alpha * input + (1 - alpha) * _output_1; |
| 538 | } |
| 539 | _output_2 = _output_1; |
| 540 | return (_output_1 = output_0); |
| 541 | } |
| 542 | |
| 543 | //------------------------------------------------------------------------------ |
| 544 | bool ExponentialFilterImplementation::configure( SGPropertyNode& cfg_node, |
| 545 | const std::string& cfg_name, |
| 546 | SGPropertyNode& prop_root ) |
| 547 | { |
| 548 | if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) ) |
| 549 | return true; |
| 550 | |
| 551 | if (cfg_name == "filter-time" ) { |
| 552 | _TfInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 553 | return true; |
| 554 | } |
| 555 | |
| 556 | if (cfg_name == "type" ) { |
| 557 | std::string type = simgear::strutils::strip(cfg_node.getStringValue()); |
| 558 | _isSecondOrder = type == "double-exponential"; |
| 559 | } |
| 560 | |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | /* --------------------------------------------------------------------------------- */ |
| 565 | |
| 566 | IntegratorFilterImplementation::IntegratorFilterImplementation() : |
| 567 | _input_1(0.0), |
| 568 | _output_1(0.0) |
| 569 | { |
| 570 | } |
| 571 | |
| 572 | void IntegratorFilterImplementation::initialize( double initvalue ) |
| 573 | { |
| 574 | _input_1 = _output_1 = initvalue; |
| 575 | } |
| 576 | |
| 577 | //------------------------------------------------------------------------------ |
| 578 | bool IntegratorFilterImplementation::configure( SGPropertyNode& cfg_node, |
| 579 | const std::string& cfg_name, |
| 580 | SGPropertyNode& prop_root ) |
| 581 | { |
| 582 | if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) ) |
| 583 | return true; |
| 584 | |
| 585 | if (cfg_name == "u_min" ) { |
| 586 | _minInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 587 | return true; |
| 588 | } |
| 589 | if (cfg_name == "u_max" ) { |
| 590 | _maxInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 591 | return true; |
| 592 | } |
| 593 | return false; |
| 594 | } |
| 595 | |
| 596 | double IntegratorFilterImplementation::compute( double dt, double input ) |
| 597 | { |
| 598 | double output = _output_1 + input * _gainInput.get_value() * dt; |
| 599 | double u_min = _minInput.get_value(); |
| 600 | double u_max = _maxInput.get_value(); |
| 601 | if (output >= u_max) output = u_max; // clamping inside "::compute" prevents integrator wind-up |
| 602 | if (output <= u_min) output = u_min; |
| 603 | _input_1 = input; |
| 604 | _output_1 = output; |
| 605 | return output; |
| 606 | |
| 607 | } |
| 608 | |
| 609 | /* --------------------------------------------------------------------------------- */ |
| 610 | DampedOscillationFilterImplementation::DampedOscillationFilterImplementation() : |
| 611 | _x0(0.0) |
| 612 | { |
| 613 | } |
| 614 | |
| 615 | void DampedOscillationFilterImplementation::initialize( double initvalue ) |
| 616 | { |
| 617 | _x2 = _x1 = _x0 = initvalue; |
| 618 | } |
| 619 | |
| 620 | bool DampedOscillationFilterImplementation::configure( SGPropertyNode& cfg_node, |
| 621 | const std::string& cfg_name, |
| 622 | SGPropertyNode& prop_root ) |
| 623 | { |
| 624 | if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) ) |
| 625 | return true; |
| 626 | |
| 627 | if (cfg_name == "a" ) { |
| 628 | _aInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 629 | return true; |
| 630 | } |
| 631 | if (cfg_name == "b" ) { |
| 632 | _bInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 633 | return true; |
| 634 | } |
| 635 | if (cfg_name == "c" ) { |
| 636 | _cInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 637 | return true; |
| 638 | } |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | double DampedOscillationFilterImplementation::compute( double dt, double input ) |
| 643 | { |
| 644 | if (fabs(input) > 1e-15) { |
| 645 | double dz = dt * input; |
| 646 | _x0 = _x1 - dz; |
| 647 | _x2 = _x1 + dz; |
| 648 | } else { |
| 649 | double a = _aInput.get_value(); |
| 650 | double b = _bInput.get_value(); |
| 651 | double c = _cInput.get_value(); |
| 652 | _x0 = (_x1 * (2. + dt * (a - b * dt)) - _x2 - c * dt * dt) / (1. + a * dt); |
| 653 | _x2 = _x1; |
| 654 | _x1 = _x0; |
| 655 | } |
| 656 | return _x0; |
| 657 | } |
| 658 | |
| 659 | /* --------------------------------------------------------------------------------- */ |
| 660 | |
| 661 | HighPassFilterImplementation::HighPassFilterImplementation() : |
| 662 | _input_1(0.0), |
| 663 | _output_1(0.0) |
| 664 | |
| 665 | { |
| 666 | } |
| 667 | |
| 668 | void HighPassFilterImplementation::initialize( double initvalue ) |
| 669 | { |
| 670 | _input_1 = initvalue; |
| 671 | _output_1 = initvalue; |
| 672 | } |
| 673 | |
| 674 | //double HighPassFilterImplementation::compute( double dt, double input ) |
| 675 | //{ |
| 676 | // input = GainFilterImplementation::compute( dt, input ); |
| 677 | // double tf = _TfInput.get_value(); |
| 678 | // |
| 679 | // double output; |
| 680 | // |
| 681 | // // avoid negative filter times |
| 682 | // // and div by zero if -tf == dt |
| 683 | // |
| 684 | // double alpha = tf > 0.0 ? 1 / ((tf/dt) + 1) : 1.0; |
| 685 | // output = (1 - alpha) * (input - _input_1 + _output_1); |
| 686 | // _input_1 = input; |
| 687 | // _output_1 = output; |
| 688 | // return output; |
| 689 | //} |
| 690 | |
| 691 | double HighPassFilterImplementation::compute(double dt, double input) |
| 692 | { |
| 693 | if (SGMiscd::isNaN(input)) |
| 694 | SG_LOG(SG_AUTOPILOT, SG_ALERT, "High pass filter output is NaN."); |
| 695 | |
| 696 | input = GainFilterImplementation::compute(dt, input); |
| 697 | double tf = _TfInput.get_value(); |
| 698 | |
| 699 | double output; |
| 700 | |
| 701 | // avoid negative filter times |
| 702 | // and div by zero if -tf == dt |
| 703 | |
| 704 | |
| 705 | double alpha = tf > 0.0 ? 1 / ((tf / dt) + 1) : 1.0; |
| 706 | output = (1 - alpha) * (input - _input_1 + _output_1); |
| 707 | _input_1 = input; |
| 708 | |
| 709 | // Catch NaN before it causes damage |
| 710 | |
| 711 | if (output != output) { |
| 712 | SG_LOG(SG_AUTOPILOT, SG_ALERT, "High pass filter output is NaN."); |
| 713 | output = 0.0; |
| 714 | } |
| 715 | _output_1 = output; |
| 716 | return output; |
| 717 | } |
| 718 | //------------------------------------------------------------------------------ |
| 719 | bool HighPassFilterImplementation::configure( SGPropertyNode& cfg_node, |
| 720 | const std::string& cfg_name, |
| 721 | SGPropertyNode& prop_root ) |
| 722 | { |
| 723 | if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) ) |
| 724 | return true; |
| 725 | |
| 726 | if (cfg_name == "filter-time" ) { |
| 727 | _TfInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 728 | return true; |
| 729 | } |
| 730 | |
| 731 | return false; |
| 732 | } |
| 733 | |
| 734 | /* --------------------------------------------------------------------------------- */ |
| 735 | |
| 736 | LeadLagFilterImplementation::LeadLagFilterImplementation() : |
| 737 | _input_1(0.0), |
| 738 | _output_1(0.0) |
| 739 | |
| 740 | { |
| 741 | } |
| 742 | |
| 743 | void LeadLagFilterImplementation::initialize( double initvalue ) |
| 744 | { |
| 745 | _input_1 = initvalue; |
| 746 | _output_1 = initvalue; |
| 747 | } |
| 748 | |
| 749 | #if 0 |
| 750 | double LeadLagFilterImplementation::compute( double dt, double input ) |
| 751 | { |
| 752 | input = GainFilterImplementation::compute( dt, input ); |
| 753 | double tfa = _TfaInput.get_value(); |
| 754 | double tfb = _TfbInput.get_value(); |
| 755 | |
| 756 | double output; |
| 757 | |
| 758 | // avoid negative filter times |
| 759 | // and div by zero if -tf == dt |
| 760 | |
| 761 | double alpha = tfa > 0.0 ? 1 / ((tfa/dt) + 1) : 1.0; |
| 762 | double beta = tfb > 0.0 ? 1 / ((tfb/dt) + 1) : 1.0; |
| 763 | output = (1 - beta) * (input / (1 - alpha) - _input_1 + _output_1); |
| 764 | _input_1 = input; |
| 765 | _output_1 = output; |
| 766 | return output; |
| 767 | } |
| 768 | #endif |
| 769 | |
| 770 | // Alternative Z transform substitution method |
| 771 | double LeadLagFilterImplementation::compute(double dt, double input) |
| 772 | { |
| 773 | if (SGMiscd::isNaN(input)) |
| 774 | SG_LOG(SG_AUTOPILOT, SG_ALERT, "LeadLag filter input is NaN."); |
| 775 | |
| 776 | double Ginput = GainFilterImplementation::compute(dt, input); |
| 777 | double tfa = 1.0 / _TfaInput.get_value(); |
| 778 | double tfb = 1.0 / _TfbInput.get_value(); |
| 779 | double output; |
| 780 | |
| 781 | double Cb = exp(-dt / tfb); |
| 782 | |
| 783 | output = _output_1 * Cb + Ginput * (1 - Cb) + (tfa / tfb) * (Ginput - _input_1) * Cb; |
| 784 | double o1 = _output_1 * Cb; |
| 785 | double o2 = input * (1 - Cb); |
| 786 | double o3 = (tfa / tfb) * (input - _input_1) * Cb; |
| 787 | |
| 788 | _input_1 = Ginput; |
| 789 | _output_1 = output; |
| 790 | return output; |
| 791 | } |
| 792 | |
| 793 | //------------------------------------------------------------------------------ |
| 794 | bool LeadLagFilterImplementation::configure( SGPropertyNode& cfg_node, |
| 795 | const std::string& cfg_name, |
| 796 | SGPropertyNode& prop_root ) |
| 797 | { |
| 798 | if( GainFilterImplementation::configure(cfg_node, cfg_name, prop_root) ) |
| 799 | return true; |
| 800 | |
| 801 | if (cfg_name == "filter-time-a" ) { |
| 802 | _TfaInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 803 | return true; |
| 804 | } |
| 805 | if (cfg_name == "filter-time-b" ) { |
| 806 | _TfbInput.push_back(new simgear::Value(prop_root, cfg_node, 1)); |
| 807 | return true; |
| 808 | } |
| 809 | return false; |
| 810 | } |
| 811 | |
| 812 | |
| 813 | /* --------------------------------------------------------------------------------- */ |
| 814 | |
| 815 | CoherentNoiseFilterImplementation::CoherentNoiseFilterImplementation() : _amplitude(1.0), |
| 816 | _numDiscreteValues(1024) |
| 817 | { |
| 818 | } |
| 819 | |
| 820 | void CoherentNoiseFilterImplementation::initialize(double initvalue) |
| 821 | { |
| 822 | // allocate the array one bigger so we don't need to worry about |
| 823 | // wrapping.bound checking the discrete +1 lookup |
| 824 | _discreteValues.resize(_numDiscreteValues + 1); |
| 825 | std::generate(_discreteValues.begin(), _discreteValues.end(), []() { |
| 826 | return (sg_random() * 2.0) - 1.0; |
| 827 | }); |
| 828 | } |
| 829 | |
| 830 | static double lerp(double a, double b, double t) |
| 831 | { |
| 832 | return (a * (1.0 - t)) + (b * t); |
| 833 | } |
| 834 | |
| 835 | double CoherentNoiseFilterImplementation::compute(double dt, double input) |
| 836 | { |
| 837 | const double a = _amplitude.get_value(); |
| 838 | |
| 839 | const double t = input * _numDiscreteValues; |
| 840 | const int i = static_cast<int>(floor(t)) % _numDiscreteValues; |
| 841 | const auto v0 = _discreteValues.at(i); |
| 842 | const auto v1 = _discreteValues.at(i + 1); |
| 843 | |
| 844 | const auto weight = t - floor(t); |
| 845 | const double output = lerp(v0, v1, weight); |
| 846 | |
| 847 | return _absoluteVal ? fabs(output) * a : output * a; |
| 848 | } |
| 849 | |
| 850 | //------------------------------------------------------------------------------ |
| 851 | bool CoherentNoiseFilterImplementation::configure(SGPropertyNode& cfg_node, |
| 852 | const std::string& cfg_name, |
| 853 | SGPropertyNode& prop_root) |
| 854 | { |
| 855 | if (cfg_name == "discrete-resolution") { |
| 856 | _numDiscreteValues = cfg_node.getIntValue(); |
| 857 | return true; |
| 858 | } |
| 859 | |
| 860 | if (cfg_name == "amplitude") { |
| 861 | _amplitude.push_back(new simgear::Value(prop_root, cfg_node, 1.0)); |
| 862 | return true; |
| 863 | } |
| 864 | |
| 865 | if (cfg_name == "absolute") { |
| 866 | _absoluteVal = cfg_node.getBoolValue(); |
| 867 | return true; |
| 868 | } |
| 869 | return false; |
| 870 | } |
| 871 | |
| 872 | /* -------------------------------------------------------------------------- */ |
| 873 | /* Digital Filter Component Implementation */ |
| 874 | /* -------------------------------------------------------------------------- */ |
| 875 | |
| 876 | DigitalFilter::DigitalFilter() : |
| 877 | AnalogComponent(), |
| 878 | _initializeTo(INITIALIZE_INPUT) |
| 879 | { |
| 880 | } |
| 881 | |
| 882 | DigitalFilter::~DigitalFilter() |
| 883 | { |
| 884 | } |
| 885 | |
| 886 | //------------------------------------------------------------------------------ |
| 887 | template<class DigitalFilterType> |
| 888 | DigitalFilterImplementation* digitalFilterFactory() |
| 889 | { |
| 890 | return new DigitalFilterType(); |
| 891 | } |
| 892 | |
| 893 | typedef std::map<std::string, DigitalFilterImplementation*(*)()> |
| 894 | DigitalFilterMap; |
| 895 | static DigitalFilterMap componentForge; |
| 896 | |
| 897 | //------------------------------------------------------------------------------ |
| 898 | bool DigitalFilter::configure( SGPropertyNode& prop_root, |
| 899 | SGPropertyNode& cfg ) |
| 900 | { |
| 901 | if( componentForge.empty() ) |
| 902 | { |
| 903 | componentForge["gain" ] = digitalFilterFactory<GainFilterImplementation>; |
| 904 | componentForge["exponential" ] = digitalFilterFactory<ExponentialFilterImplementation>; |
| 905 | componentForge["double-exponential" ] = digitalFilterFactory<ExponentialFilterImplementation>; |
| 906 | componentForge["moving-average" ] = digitalFilterFactory<MovingAverageFilterImplementation>; |
| 907 | componentForge["noise-spike" ] = digitalFilterFactory<NoiseSpikeFilterImplementation>; |
| 908 | componentForge["rate-limit" ] = digitalFilterFactory<RateLimitFilterImplementation>; |
| 909 | componentForge["reciprocal" ] = digitalFilterFactory<ReciprocalFilterImplementation>; |
| 910 | componentForge["derivative" ] = digitalFilterFactory<DerivativeFilterImplementation>; |
| 911 | componentForge["high-pass" ] = digitalFilterFactory<HighPassFilterImplementation>; |
| 912 | componentForge["lead-lag" ] = digitalFilterFactory<LeadLagFilterImplementation>; |
| 913 | componentForge["integrator" ] = digitalFilterFactory<IntegratorFilterImplementation>; |
| 914 | componentForge["damped-oscillation" ] = digitalFilterFactory<DampedOscillationFilterImplementation>; |
| 915 | componentForge["coherent-noise"] = digitalFilterFactory<CoherentNoiseFilterImplementation>; |
| 916 | } |
| 917 | |
| 918 | const auto type = simgear::strutils::strip(cfg.getStringValue("type")); |
| 919 | DigitalFilterMap::iterator component_factory = componentForge.find(type); |
| 920 | if( component_factory == componentForge.end() ) |
| 921 | { |
| 922 | SG_LOG(SG_AUTOPILOT, SG_WARN, "unhandled filter type '" << type << "'"); |
| 923 | return false; |
| 924 | } |
| 925 | |
| 926 | _implementation = (*component_factory->second)(); |
| 927 | _implementation->setDigitalFilter( this ); |
| 928 | |
| 929 | for( int i = 0; i < cfg.nChildren(); ++i ) |
| 930 | { |
| 931 | SGPropertyNode_ptr child = cfg.getChild(i); |
| 932 | std::string cname(child->getNameString()); |
| 933 | bool ok = false; |
| 934 | if (!ok) ok = _implementation->configure(*child, cname, prop_root); |
| 935 | if (!ok) ok = configure(*child, cname, prop_root); |
| 936 | if (!ok) ok = (cname == "type"); |
| 937 | if (!ok) ok = (cname == "params"); // 'params' is usually used to specify parameters in PropertList files. |
| 938 | if (!ok) { |
| 939 | SG_LOG |
| 940 | ( |
| 941 | SG_AUTOPILOT, |
| 942 | SG_ALERT, |
| 943 | "DigitalFilter: unknown config node: " << cname |
| 944 | ); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | /* Send information about associations between our input and output |
| 949 | properties to Highlight. */ |
| 950 | std::set<const SGPropertyNode*> inputs; |
| 951 | _implementation->collectDependentProperties(inputs); |
| 952 | collectDependentProperties(inputs); |
| 953 | |
| 954 | auto highlight = globals->get_subsystem<Highlight>(); |
| 955 | if (highlight) { |
| 956 | for (auto in: inputs) { |
| 957 | for (auto& out: _output_list) { |
| 958 | highlight->addPropertyProperty( |
| 959 | in->getPath(true /*simplify*/), |
| 960 | out->getPath(true /*simplify*/) |
| 961 | ); |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | return true; |
| 967 | } |
| 968 | |
| 969 | //------------------------------------------------------------------------------ |
| 970 | bool DigitalFilter::configure( SGPropertyNode& cfg_node, |
| 971 | const std::string& cfg_name, |
| 972 | SGPropertyNode& prop_root ) |
| 973 | { |
| 974 | if( cfg_name == "initialize-to" ) |
| 975 | { |
| 976 | const auto s = simgear::strutils::strip(cfg_node.getStringValue()); |
| 977 | if( s == "input" ) |
| 978 | _initializeTo = INITIALIZE_INPUT; |
| 979 | else if( s == "output" ) |
| 980 | _initializeTo = INITIALIZE_OUTPUT; |
| 981 | else if( s == "none" ) |
| 982 | _initializeTo = INITIALIZE_NONE; |
| 983 | else |
| 984 | SG_LOG |
| 985 | ( |
| 986 | SG_AUTOPILOT, |
| 987 | SG_WARN, "DigitalFilter: initialize-to (" << s << ") ignored" |
| 988 | ); |
| 989 | |
| 990 | return true; |
| 991 | } |
| 992 | |
| 993 | return AnalogComponent::configure(cfg_node, cfg_name, prop_root); |
| 994 | } |
| 995 | |
| 996 | //------------------------------------------------------------------------------ |
| 997 | void DigitalFilter::update( bool firstTime, double dt) |
| 998 | { |
| 999 | if( _implementation == NULL ) return; |
| 1000 | |
| 1001 | if( firstTime ) { |
| 1002 | switch( _initializeTo ) { |
| 1003 | |
| 1004 | case INITIALIZE_INPUT: |
| 1005 | SG_LOG(SG_AUTOPILOT,SG_DEBUG, "First time initialization of " << subsystemId() << " to " << _valueInput.get_value() ); |
| 1006 | _implementation->initialize( _valueInput.get_value() ); |
| 1007 | break; |
| 1008 | |
| 1009 | case INITIALIZE_OUTPUT: |
| 1010 | SG_LOG(SG_AUTOPILOT,SG_DEBUG, "First time initialization of " << subsystemId() << " to " << get_output_value() ); |
| 1011 | _implementation->initialize( get_output_value() ); |
| 1012 | break; |
| 1013 | |
| 1014 | default: |
| 1015 | SG_LOG(SG_AUTOPILOT,SG_DEBUG, "First time initialization of " << subsystemId() << " to (uninitialized)" ); |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | double input = _valueInput.get_value() - _referenceInput.get_value(); |
| 1021 | if (SGMiscd::isNaN(input)) |
| 1022 | input = _valueInput.get_value() - _referenceInput.get_value(); |
| 1023 | double output = _implementation->compute( dt, input ); |
| 1024 | |
| 1025 | set_output_value( output ); |
| 1026 | |
| 1027 | if(_debug) { |
| 1028 | std::cout << subsystemId() << ": input=" << input |
| 1029 | << "\toutput=" << output << std::endl; |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | // Register the subsystem. |
| 1035 | SGSubsystemMgr::Registrant<DigitalFilter> registrantDigitalFilter; |