1 // digitalfilter.cxx - a selection of digital filters
3 // Written by Torsten Dreyer
4 // Based heavily on work created by Curtis Olson, started January 2004.
6 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
7 // Copyright (C) 2010 Torsten Dreyer - Torsten (at) t3r (dot) de
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "digitalfilter.hxx"
25 #include "functor.hxx"
33 namespace FGXMLAutopilot {
39 class DigitalFilterImplementation : public SGReferenced {
41 virtual bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode) = 0;
43 virtual ~DigitalFilterImplementation() {}
44 DigitalFilterImplementation();
45 virtual void initialize( double initvalue ) {}
46 virtual double compute( double dt, double input ) = 0;
47 bool configure( SGPropertyNode_ptr configNode );
49 void setDigitalFilter( DigitalFilter * digitalFilter ) { _digitalFilter = digitalFilter; }
52 DigitalFilter * _digitalFilter;
55 /* --------------------------------------------------------------------------------- */
56 /* --------------------------------------------------------------------------------- */
57 class GainFilterImplementation : public DigitalFilterImplementation {
59 InputValueList _gainInput;
60 bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
62 GainFilterImplementation() : _gainInput(1.0) {}
63 double compute( double dt, double input );
66 class ReciprocalFilterImplementation : public GainFilterImplementation {
68 double compute( double dt, double input );
71 class DerivativeFilterImplementation : public GainFilterImplementation {
72 InputValueList _TfInput;
74 bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
76 DerivativeFilterImplementation();
77 double compute( double dt, double input );
78 virtual void initialize( double initvalue );
81 class ExponentialFilterImplementation : public GainFilterImplementation {
83 InputValueList _TfInput;
84 bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
86 double output_1, output_2;
88 ExponentialFilterImplementation();
89 double compute( double dt, double input );
90 virtual void initialize( double initvalue );
93 class MovingAverageFilterImplementation : public DigitalFilterImplementation {
95 InputValueList _samplesInput;
97 std::deque <double> _inputQueue;
98 bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
100 MovingAverageFilterImplementation();
101 double compute( double dt, double input );
102 virtual void initialize( double initvalue );
105 class NoiseSpikeFilterImplementation : public DigitalFilterImplementation {
108 InputValueList _rateOfChangeInput;
109 bool configure( const std::string & nodeName, SGPropertyNode_ptr configNode );
111 NoiseSpikeFilterImplementation();
112 double compute( double dt, double input );
113 virtual void initialize( double initvalue );
116 /* --------------------------------------------------------------------------------- */
117 /* --------------------------------------------------------------------------------- */
119 } // namespace FGXMLAutopilot
121 using namespace FGXMLAutopilot;
123 /* --------------------------------------------------------------------------------- */
124 /* --------------------------------------------------------------------------------- */
125 DigitalFilterImplementation::DigitalFilterImplementation() :
130 bool DigitalFilterImplementation::configure( SGPropertyNode_ptr configNode )
132 for (int i = 0; i < configNode->nChildren(); ++i ) {
133 SGPropertyNode_ptr prop;
135 SGPropertyNode_ptr child = configNode->getChild(i);
136 string cname(child->getName());
138 if( configure( cname, child ) )
141 } // for configNode->nChildren()
146 /* --------------------------------------------------------------------------------- */
147 /* --------------------------------------------------------------------------------- */
149 double GainFilterImplementation::compute( double dt, double input )
151 return _gainInput.get_value() * input;
154 bool GainFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
156 if (nodeName == "gain" ) {
157 _gainInput.push_back( new InputValue( configNode, 1 ) );
164 /* --------------------------------------------------------------------------------- */
165 /* --------------------------------------------------------------------------------- */
167 double ReciprocalFilterImplementation::compute( double dt, double input )
169 if( input >= -SGLimitsd::min() && input <= SGLimitsd::min() )
170 return SGLimitsd::max();
172 return _gainInput.get_value() / input;
176 /* --------------------------------------------------------------------------------- */
177 /* --------------------------------------------------------------------------------- */
179 DerivativeFilterImplementation::DerivativeFilterImplementation() :
184 void DerivativeFilterImplementation::initialize( double initvalue )
186 _input_1 = initvalue;
190 bool DerivativeFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
192 if( GainFilterImplementation::configure( nodeName, configNode ) )
195 if (nodeName == "filter-time" ) {
196 _TfInput.push_back( new InputValue( configNode, 1 ) );
203 double DerivativeFilterImplementation::compute( double dt, double input )
205 double output = (input - _input_1) * _TfInput.get_value() * _gainInput.get_value() / dt;
211 /* --------------------------------------------------------------------------------- */
212 /* --------------------------------------------------------------------------------- */
214 MovingAverageFilterImplementation::MovingAverageFilterImplementation() :
219 void MovingAverageFilterImplementation::initialize( double initvalue )
221 _output_1 = initvalue;
224 double MovingAverageFilterImplementation::compute( double dt, double input )
226 std::deque<double>::size_type samples = _samplesInput.get_value();
227 _inputQueue.resize(samples+1, 0.0);
229 double output_0 = _output_1 + (input - _inputQueue.back()) / samples;
231 _output_1 = output_0;
232 _inputQueue.push_front(input);
236 bool MovingAverageFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
238 if (nodeName == "samples" ) {
239 _samplesInput.push_back( new InputValue( configNode, 1 ) );
246 /* --------------------------------------------------------------------------------- */
247 /* --------------------------------------------------------------------------------- */
249 NoiseSpikeFilterImplementation::NoiseSpikeFilterImplementation() :
254 void NoiseSpikeFilterImplementation::initialize( double initvalue )
256 _output_1 = initvalue;
259 double NoiseSpikeFilterImplementation::compute( double dt, double input )
261 double delta = input - _output_1;
262 if( fabs(delta) <= SGLimitsd::min() ) return input; // trivial
264 double maxChange = _rateOfChangeInput.get_value() * dt;
265 const PeriodicalValue * periodical = _digitalFilter->getPeriodicalValue();
266 if( periodical ) delta = periodical->normalizeSymmetric( delta );
268 if( fabs(delta) <= maxChange )
269 return (_output_1 = input);
271 return (_output_1 = _output_1 + copysign( maxChange, delta ));
274 bool NoiseSpikeFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
276 if (nodeName == "max-rate-of-change" ) {
277 _rateOfChangeInput.push_back( new InputValue( configNode, 1 ) );
284 /* --------------------------------------------------------------------------------- */
285 /* --------------------------------------------------------------------------------- */
287 ExponentialFilterImplementation::ExponentialFilterImplementation()
288 : _isSecondOrder(false),
294 void ExponentialFilterImplementation::initialize( double initvalue )
296 output_1 = output_2 = initvalue;
299 double ExponentialFilterImplementation::compute( double dt, double input )
301 input = GainFilterImplementation::compute( dt, input );
302 double tf = _TfInput.get_value();
306 // avoid negative filter times
307 // and div by zero if -tf == dt
309 double alpha = tf > 0.0 ? 1 / ((tf/dt) + 1) : 1.0;
312 output_0 = alpha * alpha * input +
313 2 * (1 - alpha) * output_1 -
314 (1 - alpha) * (1 - alpha) * output_2;
316 output_0 = alpha * input + (1 - alpha) * output_1;
319 return (output_1 = output_0);
322 bool ExponentialFilterImplementation::configure( const std::string & nodeName, SGPropertyNode_ptr configNode )
324 if( GainFilterImplementation::configure( nodeName, configNode ) )
327 if (nodeName == "filter-time" ) {
328 _TfInput.push_back( new InputValue( configNode, 1 ) );
332 if (nodeName == "type" ) {
333 string type(configNode->getStringValue());
334 _isSecondOrder = type == "double-exponential";
340 /* --------------------------------------------------------------------------------- */
341 /* Digital Filter Component Implementation */
342 /* --------------------------------------------------------------------------------- */
344 DigitalFilter::DigitalFilter() :
346 _initializeTo(INITIALIZE_INPUT)
350 DigitalFilter::~DigitalFilter()
355 static map<string,FunctorBase<DigitalFilterImplementation> *> componentForge;
357 bool DigitalFilter::configure(const string& nodeName, SGPropertyNode_ptr configNode)
359 if( componentForge.empty() ) {
360 componentForge["gain"] = new CreateAndConfigureFunctor<GainFilterImplementation,DigitalFilterImplementation>();
361 componentForge["exponential"] = new CreateAndConfigureFunctor<ExponentialFilterImplementation,DigitalFilterImplementation>();
362 componentForge["double-exponential"] = new CreateAndConfigureFunctor<ExponentialFilterImplementation,DigitalFilterImplementation>();
363 componentForge["moving-average"] = new CreateAndConfigureFunctor<MovingAverageFilterImplementation,DigitalFilterImplementation>();
364 componentForge["noise-spike"] = new CreateAndConfigureFunctor<NoiseSpikeFilterImplementation,DigitalFilterImplementation>();
365 componentForge["reciprocal"] = new CreateAndConfigureFunctor<ReciprocalFilterImplementation,DigitalFilterImplementation>();
366 componentForge["derivative"] = new CreateAndConfigureFunctor<DerivativeFilterImplementation,DigitalFilterImplementation>();
369 SG_LOG( SG_AUTOPILOT, SG_BULK, "DigitalFilter::configure(" << nodeName << ")" << endl );
370 if( AnalogComponent::configure( nodeName, configNode ) )
373 if (nodeName == "type" ) {
374 string type( configNode->getStringValue() );
375 if( componentForge.count(type) == 0 ) {
376 SG_LOG( SG_AUTOPILOT, SG_BULK, "unhandled filter type <" << type << ">" << endl );
379 _implementation = (*componentForge[type])( configNode->getParent() );
380 _implementation->setDigitalFilter( this );
384 if( nodeName == "initialize-to" ) {
385 string s( configNode->getStringValue() );
387 _initializeTo = INITIALIZE_INPUT;
388 } else if( s == "output" ) {
389 _initializeTo = INITIALIZE_OUTPUT;
390 } else if( s == "none" ) {
391 _initializeTo = INITIALIZE_NONE;
393 SG_LOG( SG_AUTOPILOT, SG_WARN, "unhandled initialize-to value '" << s << "' ignored" );
398 SG_LOG( SG_AUTOPILOT, SG_BULK, "DigitalFilter::configure(" << nodeName << ") [unhandled]" << endl );
399 return false; // not handled by us, let the base class try
402 void DigitalFilter::update( bool firstTime, double dt)
404 if( _implementation == NULL ) return;
407 switch( _initializeTo ) {
409 case INITIALIZE_INPUT:
410 SG_LOG(SG_AUTOPILOT,SG_DEBUG, "First time initialization of " << get_name() << " to " << _valueInput.get_value() );
411 _implementation->initialize( _valueInput.get_value() );
414 case INITIALIZE_OUTPUT:
415 SG_LOG(SG_AUTOPILOT,SG_DEBUG, "First time initialization of " << get_name() << " to " << get_output_value() );
416 _implementation->initialize( get_output_value() );
420 SG_LOG(SG_AUTOPILOT,SG_DEBUG, "First time initialization of " << get_name() << " to (uninitialized)" );
425 double input = _valueInput.get_value() - _referenceInput.get_value();
426 double output = _implementation->compute( dt, input );
428 set_output_value( output );
431 cout << "input:" << input
432 << "\toutput:" << output << endl;