Nifty Snippets

IIR Low Pass Filter

class LowPassFilter {
 public:
  LowPassFilter(float time_constant_s, float initial_value = 0.0f) :
    time_constant_s_, output_(initial_value), rate_(0.0f) {}

  void Update(float dt_sec, float sample) {
    rate_ = (sample - output_) / time_constant_s_;
    output_ += rate_ * dt_sec;
  }

  float output() const { return output_; }
  float rate() const { return rate_; }

private:
  const float time_constant_s_;
  float output_;
  float rate_;
};