Browse Source

Filter: add get_cutoff_frequency

mission-4.1.18
Jonathan Challinger 10 years ago committed by Randy Mackay
parent
commit
57f8a4d29d
  1. 10
      libraries/Filter/LowPassFilter.h

10
libraries/Filter/LowPassFilter.h

@ -35,6 +35,7 @@ public: @@ -35,6 +35,7 @@ public:
LowPassFilter();
void set_cutoff_frequency(float time_step, float cutoff_freq);
float get_cutoff_frequency() { return _cutoff_hz; }
void set_time_constant(float time_step, float time_constant);
// apply - Add a new raw value to the filter, retrieve the filtered result
@ -56,7 +57,8 @@ public: @@ -56,7 +57,8 @@ public:
private:
float _alpha; // gain value (like 0.02) applied to each new value
bool _base_value_set; // true if the base value has been set
float _base_value; // the number of samples in the filter, maxes out at size of the filter
float _base_value; // filter output
float _cutoff_hz; // cutoff frequency in hz
};
// Typedef for convenience (1st argument is the data type, 2nd is a larger datatype to handle overflows, 3rd is buffer size)
@ -89,6 +91,7 @@ LowPassFilter<T>::LowPassFilter() : @@ -89,6 +91,7 @@ LowPassFilter<T>::LowPassFilter() :
template <class T>
void LowPassFilter<T>::set_cutoff_frequency(float time_step, float cutoff_freq)
{
_cutoff_hz = cutoff_freq;
// avoid divide by zero and allow removing filtering
if (cutoff_freq <= 0.0f) {
_alpha = 1.0f;
@ -96,7 +99,7 @@ void LowPassFilter<T>::set_cutoff_frequency(float time_step, float cutoff_freq) @@ -96,7 +99,7 @@ void LowPassFilter<T>::set_cutoff_frequency(float time_step, float cutoff_freq)
}
// calculate alpha
float rc = 1/(2*PI*cutoff_freq);
float rc = 1/(2*M_PI_F*cutoff_freq);
_alpha = time_step / (time_step + rc);
}
@ -105,10 +108,13 @@ void LowPassFilter<T>::set_time_constant(float time_step, float time_constant) @@ -105,10 +108,13 @@ void LowPassFilter<T>::set_time_constant(float time_step, float time_constant)
{
// avoid divide by zero
if (time_constant + time_step <= 0.0f) {
_cutoff_hz = 0.0f;
_alpha = 1.0f;
return;
}
_cutoff_hz = 1/(2*M_PI_F*time_constant);
// calculate alpha
_alpha = time_step / (time_constant + time_step);
}

Loading…
Cancel
Save