2017-08-16 02:27:39 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/*
|
|
|
|
notch filter with settable sample rate, center frequency, bandwidth and attenuation
|
|
|
|
|
|
|
|
Design by Leonard Hall
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <cmath>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class NotchFilter {
|
|
|
|
public:
|
|
|
|
// set parameters
|
|
|
|
void init(float sample_freq_hz, float center_freq_hz, float bandwidth_hz, float attenuation_dB);
|
2019-06-17 05:43:08 -03:00
|
|
|
void init_with_A_and_Q(float sample_freq_hz, float center_freq_hz, float A, float Q);
|
2017-08-16 02:27:39 -03:00
|
|
|
T apply(const T &sample);
|
2019-05-17 12:59:54 -03:00
|
|
|
void reset();
|
2017-08-16 02:27:39 -03:00
|
|
|
|
2019-06-17 05:43:08 -03:00
|
|
|
// calculate attenuation and quality from provided center frequency and bandwidth
|
|
|
|
static void calculate_A_and_Q(float center_freq_hz, float bandwidth_hz, float attenuation_dB, float& A, float& Q);
|
|
|
|
|
2017-08-16 02:27:39 -03:00
|
|
|
private:
|
2019-06-17 05:43:08 -03:00
|
|
|
|
2022-06-05 20:40:34 -03:00
|
|
|
bool initialised, need_reset;
|
2017-08-16 02:27:39 -03:00
|
|
|
float b0, b1, b2, a1, a2, a0_inv;
|
2022-09-21 06:45:42 -03:00
|
|
|
float _center_freq_hz, _sample_freq_hz;
|
2017-08-16 02:27:39 -03:00
|
|
|
T ntchsig, ntchsig1, ntchsig2, signal2, signal1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2019-05-17 12:59:54 -03:00
|
|
|
notch filter enable and filter parameters
|
2017-08-16 02:27:39 -03:00
|
|
|
*/
|
2019-05-17 12:59:54 -03:00
|
|
|
class NotchFilterParams {
|
2017-08-16 02:27:39 -03:00
|
|
|
public:
|
2019-06-17 05:43:08 -03:00
|
|
|
float center_freq_hz(void) const { return _center_freq_hz; }
|
|
|
|
float bandwidth_hz(void) const { return _bandwidth_hz; }
|
2019-05-17 12:59:54 -03:00
|
|
|
float attenuation_dB(void) const { return _attenuation_dB; }
|
|
|
|
uint8_t enabled(void) const { return _enable; }
|
2022-03-06 11:00:01 -04:00
|
|
|
void enable() { _enable.set(true); }
|
2017-08-16 02:27:39 -03:00
|
|
|
|
2019-06-17 05:43:08 -03:00
|
|
|
protected:
|
2019-05-17 12:59:54 -03:00
|
|
|
AP_Int8 _enable;
|
2019-06-17 05:43:08 -03:00
|
|
|
AP_Float _center_freq_hz;
|
|
|
|
AP_Float _bandwidth_hz;
|
2019-05-17 12:59:54 -03:00
|
|
|
AP_Float _attenuation_dB;
|
2017-08-16 02:27:39 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef NotchFilter<float> NotchFilterFloat;
|
2021-06-17 03:36:03 -03:00
|
|
|
typedef NotchFilter<Vector2f> NotchFilterVector2f;
|
2017-08-16 02:27:39 -03:00
|
|
|
typedef NotchFilter<Vector3f> NotchFilterVector3f;
|
|
|
|
|