75 while ((shift != 0) && (
alpha *
static_cast< float >(1UL << (16 + shift)) + 0.5F >=
static_cast< float >(
ALPHA_FIXED_LIMIT)))
79 const int16_t alphaFixed{
static_cast< int16_t
>(
alpha *
static_cast< float >(1UL << (16 + shift)) + 0.5F) };
81 static_cast< int16_t
>(2 * alphaFixed),
82 static_cast< int16_t
>(4 * alphaFixed),
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) };
97 if (!(
alpha > 0.0F) || !(
alpha < 0.0625F) || !(gain >= 0.0F) || !(gain < 64.0F))
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);
145 int32_t increment{ alphaSample - alphaLpf };
148 increment -= coefficients.
alpha;
152 increment -= coefficients.
alphaX2;
157 const uint8_t shift{ coefficients.
alphaShift };
158 const int16_t incrementHigh{
static_cast< int16_t
>(increment >> 16) };
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)) };
163 if ((
lpfX64 < 0) && fractionDropped)
184 const int32_t sample{
static_cast< int32_t
>(sampleMinusDC) + (boost >> 8) };
186 if (sample > INT16_MAX)
190 if (sample < INT16_MIN)
194 return static_cast< int16_t
>(sample);
static void multS16x16_to32(int32_t &result, A a, B b)
Optimized 16×16→32 signed multiplication with assembly.
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.
constexpr uint8_t MAX_ALPHA_SHIFT
The largest extra shift of alpha: more resolution for a small alpha, one more turn each.
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...
constexpr Coefficients coefficientsOf(const float gain, const float alpha)
Converts the float settings to fixed point, at compile time.
constexpr bool isAccurate(const float gain, const float alpha)
Whether the settings fit the fixed point, the coefficients within 1 % of them.
The coefficients of the filter, in fixed point: the multiples the filter needs, ready.
The state of the filter of one CT (one per phase), and the filter itself.
int16_t apply(const int16_t sampleMinusDC, const Coefficients &coefficients)
Filters one sample.
int32_t lowPass() const
The low-pass state, x256 scale (Robin's lpf_long).