3-phase PV router
Loading...
Searching...
No Matches
ct_filter.h
Go to the documentation of this file.
1
31
32#ifndef CT_FILTER_H
33#define CT_FILTER_H
34
35#include <stdint.h>
36
37#include "mult_asm.h"
38
39namespace CtFilter
40{
42inline constexpr uint8_t LPF_FRACTION_BITS{ 2 };
43
45inline constexpr uint8_t MAX_ALPHA_SHIFT{ 4 };
46
51inline constexpr int16_t ALPHA_FIXED_LIMIT{ 4096 };
52
55{
56 int16_t alpha;
57 int16_t alphaX2;
58 int16_t alphaX4;
59 uint8_t alphaShift;
60 int16_t gain;
61 int16_t gainHalf;
62 int16_t gainQuarter;
63};
64
72constexpr Coefficients coefficientsOf(const float gain, const float alpha)
73{
74 uint8_t shift{ MAX_ALPHA_SHIFT };
75 while ((shift != 0) && (alpha * static_cast< float >(1UL << (16 + shift)) + 0.5F >= static_cast< float >(ALPHA_FIXED_LIMIT)))
76 {
77 --shift;
78 }
79 const int16_t alphaFixed{ static_cast< int16_t >(alpha * static_cast< float >(1UL << (16 + shift)) + 0.5F) };
80 return { alphaFixed,
81 static_cast< int16_t >(2 * alphaFixed),
82 static_cast< int16_t >(4 * alphaFixed),
83 shift,
84 static_cast< int16_t >(gain * 256.0F + 0.5F),
85 static_cast< int16_t >(gain * 128.0F + 0.5F),
86 static_cast< int16_t >(gain * 64.0F + 0.5F) };
87}
88
95constexpr bool isAccurate(const float gain, const float alpha)
96{
97 if (!(alpha > 0.0F) || !(alpha < 0.0625F) || !(gain >= 0.0F) || !(gain < 64.0F))
98 {
99 return false;
100 }
101 const Coefficients coefficients{ coefficientsOf(gain, alpha) };
102 const float fixedAlpha{ static_cast< float >(coefficients.alpha) / static_cast< float >(1UL << (16 + coefficients.alphaShift)) };
103 const float fixedGain{ static_cast< float >(coefficients.gain) / 256.0F };
104 const float alphaError{ fixedAlpha > alpha ? fixedAlpha - alpha : alpha - fixedAlpha };
105 const float gainError{ fixedGain > gain ? fixedGain - gain : gain - fixedGain };
106 return (coefficients.alpha < ALPHA_FIXED_LIMIT) && (alphaError <= alpha * 0.01F) && (gainError <= gain * 0.01F);
107}
108
117{
118 int16_t lpfX64{};
119 uint8_t lpfLowBits{};
120
122 int32_t lowPass() const
123 {
124 return 4 * static_cast< int32_t >(lpfX64) + lpfLowBits;
125 }
126
137 int16_t apply(const int16_t sampleMinusDC, const Coefficients &coefficients)
138 {
139 // alpha x (4 x sample - lpf), in units of 2^-(16 + shift) at x256:
140 // 4 x alpha x sample - 4 x alpha x lpfX64 - alpha x lpfLowBits
141 int32_t alphaSample;
142 int32_t alphaLpf;
143 multS16x16_to32(alphaSample, sampleMinusDC, coefficients.alphaX4);
144 multS16x16_to32(alphaLpf, lpfX64, coefficients.alphaX4);
145 int32_t increment{ alphaSample - alphaLpf }; // below 2^30
146 if (lpfLowBits & 1)
147 {
148 increment -= coefficients.alpha;
149 }
150 if (lpfLowBits & 2)
151 {
152 increment -= coefficients.alphaX2;
153 }
154
155 // lpf + increment / 2^(16 + shift), truncated towards zero: floored, plus one when the
156 // result is negative with a fraction dropped
157 const uint8_t shift{ coefficients.alphaShift };
158 const int16_t incrementHigh{ static_cast< int16_t >(increment >> 16) }; // a word move, below 2^14
159 const bool fractionDropped{ (static_cast< uint16_t >(increment) != 0) || ((incrementHigh & ((1 << shift) - 1)) != 0) };
160 const int16_t sum{ static_cast< int16_t >(lpfLowBits + (incrementHigh >> shift)) };
161 lpfX64 += sum >> 2; // floored
162 lpfLowBits = static_cast< uint8_t >(sum & 3);
163 if ((lpfX64 < 0) && fractionDropped)
164 {
165 if (++lpfLowBits == 4)
166 {
167 lpfLowBits = 0;
168 ++lpfX64;
169 }
170 }
171
172 // gain x lpf at x64, x 2^8: gain x 2^8 x lpfX64 + gain x 2^6 x lpfLowBits
173 int32_t boost;
174 multS16x16_to32(boost, lpfX64, coefficients.gain); // below 2^29: the gain below 64
175 if (lpfLowBits & 1)
176 {
177 boost += coefficients.gainQuarter;
178 }
179 if (lpfLowBits & 2)
180 {
181 boost += coefficients.gainHalf;
182 }
183
184 const int32_t sample{ static_cast< int32_t >(sampleMinusDC) + (boost >> 8) }; // a byte move
185
186 if (sample > INT16_MAX)
187 {
188 return INT16_MAX;
189 }
190 if (sample < INT16_MIN)
191 {
192 return INT16_MIN;
193 }
194 return static_cast< int16_t >(sample);
195 }
196};
197} // namespace CtFilter
198
199#endif // CT_FILTER_H
constexpr float alpha
Definition calibration.h:73
static void multS16x16_to32(int32_t &result, A a, B b)
Optimized 16×16→32 signed multiplication with assembly.
Definition mult_asm.h:47
Assembly-optimized multiplication functions for AVR microcontrollers.
constexpr uint8_t LPF_FRACTION_BITS
The fractional bits of the low-pass state below the x64 scale of the samples: x256.
Definition ct_filter.h:42
constexpr uint8_t MAX_ALPHA_SHIFT
The largest extra shift of alpha: more resolution for a small alpha, one more turn each.
Definition ct_filter.h:45
constexpr int16_t ALPHA_FIXED_LIMIT
alpha x 2^(16 + shift) stays below this: 4 x alpha fits an int16, and 4 x alpha x the largest differe...
Definition ct_filter.h:51
constexpr Coefficients coefficientsOf(const float gain, const float alpha)
Converts the float settings to fixed point, at compile time.
Definition ct_filter.h:72
constexpr bool isAccurate(const float gain, const float alpha)
Whether the settings fit the fixed point, the coefficients within 1 % of them.
Definition ct_filter.h:95
The coefficients of the filter, in fixed point: the multiples the filter needs, ready.
Definition ct_filter.h:55
The state of the filter of one CT (one per phase), and the filter itself.
Definition ct_filter.h:117
int16_t apply(const int16_t sampleMinusDC, const Coefficients &coefficients)
Filters one sample.
Definition ct_filter.h:137
int32_t lowPass() const
The low-pass state, x256 scale (Robin's lpf_long).
Definition ct_filter.h:122