MSP - fix buffers in MSP_MULTIPLE_MSP (#13881)
[betaflight.git] / src / main / common / filter.h
blobcc1bb9c43cd22fb51efa5d83825fc5da115b0d07
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #pragma once
23 #include <stdbool.h>
24 #include <stdint.h>
26 struct filter_s;
27 typedef struct filter_s filter_t;
28 typedef float (*filterApplyFnPtr)(filter_t *filter, float input);
30 typedef enum {
31 FILTER_PT1 = 0,
32 FILTER_BIQUAD,
33 FILTER_PT2,
34 FILTER_PT3,
35 } lowpassFilterType_e;
37 typedef enum {
38 FILTER_LPF, // 2nd order Butterworth section
39 FILTER_NOTCH,
40 FILTER_BPF,
41 } biquadFilterType_e;
43 typedef struct pt1Filter_s {
44 float state;
45 float k;
46 } pt1Filter_t;
48 typedef struct pt2Filter_s {
49 float state;
50 float state1;
51 float k;
52 } pt2Filter_t;
54 typedef struct pt3Filter_s {
55 float state;
56 float state1;
57 float state2;
58 float k;
59 } pt3Filter_t;
61 /* this holds the data required to update samples thru a filter */
62 typedef struct biquadFilter_s {
63 float b0, b1, b2, a1, a2;
64 float x1, x2, y1, y2;
65 float weight;
66 } biquadFilter_t;
68 typedef struct phaseComp_s {
69 float b0, b1, a1;
70 float x1, y1;
71 } phaseComp_t;
73 typedef struct slewFilter_s {
74 float state;
75 float slewLimit;
76 float threshold;
77 } slewFilter_t;
79 typedef struct laggedMovingAverage_s {
80 uint16_t movingWindowIndex;
81 uint16_t windowSize;
82 float movingSum;
83 float *buf;
84 bool primed;
85 } laggedMovingAverage_t;
87 typedef struct simpleLowpassFilter_s {
88 int32_t fp;
89 int32_t beta;
90 int32_t fpShift;
91 } simpleLowpassFilter_t;
93 typedef struct meanAccumulator_s {
94 int32_t accumulator;
95 int32_t count;
96 } meanAccumulator_t;
98 float nullFilterApply(filter_t *filter, float input);
100 float pt1FilterGain(float f_cut, float dT);
101 float pt1FilterGainFromDelay(float delay, float dT);
102 void pt1FilterInit(pt1Filter_t *filter, float k);
103 void pt1FilterUpdateCutoff(pt1Filter_t *filter, float k);
104 float pt1FilterApply(pt1Filter_t *filter, float input);
106 float pt2FilterGain(float f_cut, float dT);
107 float pt2FilterGainFromDelay(float delay, float dT);
108 void pt2FilterInit(pt2Filter_t *filter, float k);
109 void pt2FilterUpdateCutoff(pt2Filter_t *filter, float k);
110 float pt2FilterApply(pt2Filter_t *filter, float input);
112 float pt3FilterGain(float f_cut, float dT);
113 float pt3FilterGainFromDelay(float delay, float dT);
114 void pt3FilterInit(pt3Filter_t *filter, float k);
115 void pt3FilterUpdateCutoff(pt3Filter_t *filter, float k);
116 float pt3FilterApply(pt3Filter_t *filter, float input);
118 float filterGetNotchQ(float centerFreq, float cutoffFreq);
120 void biquadFilterInitLPF(biquadFilter_t *filter, float filterFreq, uint32_t refreshRate);
121 void biquadFilterInit(biquadFilter_t *filter, float filterFreq, uint32_t refreshRate, float Q, biquadFilterType_e filterType, float weight);
122 void biquadFilterUpdate(biquadFilter_t *filter, float filterFreq, uint32_t refreshRate, float Q, biquadFilterType_e filterType, float weight);
123 void biquadFilterUpdateLPF(biquadFilter_t *filter, float filterFreq, uint32_t refreshRate);
124 float biquadFilterApplyDF1(biquadFilter_t *filter, float input);
125 float biquadFilterApplyDF1Weighted(biquadFilter_t *filter, float input);
126 float biquadFilterApply(biquadFilter_t *filter, float input);
128 void phaseCompInit(phaseComp_t *filter, const float centerFreq, const float centerPhase, const uint32_t looptimeUs);
129 void phaseCompUpdate(phaseComp_t *filter, const float centerFreq, const float centerPhase, const uint32_t looptimeUs);
130 float phaseCompApply(phaseComp_t *filter, const float input);
132 void slewFilterInit(slewFilter_t *filter, float slewLimit, float threshold);
133 float slewFilterApply(slewFilter_t *filter, float input);
135 void laggedMovingAverageInit(laggedMovingAverage_t *filter, uint16_t windowSize, float *buf);
136 float laggedMovingAverageUpdate(laggedMovingAverage_t *filter, float input);
138 void simpleLPFilterInit(simpleLowpassFilter_t *filter, int32_t beta, int32_t fpShift);
139 int32_t simpleLPFilterUpdate(simpleLowpassFilter_t *filter, int32_t newVal);
141 void meanAccumulatorInit(meanAccumulator_t *filter);
142 void meanAccumulatorAdd(meanAccumulator_t *filter, const int8_t newVal);
143 int8_t meanAccumulatorCalc(meanAccumulator_t *filter, const int8_t defaultValue);