3-phase PV router
Loading...
Searching...
No Matches
ct_filter.h File Reference

The CT filter: a low-pass of the current added back with a gain, one per phase. More...

#include <stdint.h>
#include "mult_asm.h"
Include dependency graph for ct_filter.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  CtFilter::Coefficients
 The coefficients of the filter, in fixed point: the multiples the filter needs, ready. More...
 
struct  CtFilter::LowPassBoost
 The state of the filter of one CT (one per phase), and the filter itself. More...
 

Namespaces

namespace  CtFilter
 

Functions

constexpr Coefficients CtFilter::coefficientsOf (const float gain, const float alpha)
 Converts the float settings to fixed point, at compile time.
 
constexpr bool CtFilter::isAccurate (const float gain, const float alpha)
 Whether the settings fit the fixed point, the coefficients within 1 % of them.
 

Variables

constexpr int16_t CtFilter::ALPHA_FIXED_LIMIT { 4096 }
 alpha x 2^(16 + shift) stays below this: 4 x alpha fits an int16, and 4 x alpha x the largest difference (2^16 at x64) an int32 with 2 bits to spare.
 
constexpr uint8_t CtFilter::LPF_FRACTION_BITS { 2 }
 The fractional bits of the low-pass state below the x64 scale of the samples: x256.
 
constexpr uint8_t CtFilter::MAX_ALPHA_SHIFT { 4 }
 The largest extra shift of alpha: more resolution for a small alpha, one more turn each.
 

Detailed Description

The CT filter: a low-pass of the current added back with a gain, one per phase.

Author
Frédéric Metrich (frede.nosp@m.ric..nosp@m.metri.nosp@m.ch@l.nosp@m.ive.f.nosp@m.r)
Version
1.0
Date
2026-11-22

A current transformer acts as a high-pass filter. Robin's sketches offset it with an IIR low-pass filter of the current, added back with a gain (lpf_gain, alpha in calibration.h). This is his filter, Mk2_fasterControl_Full/processing.cpp, in fixed point instead of his float maths (two float multiplies and four conversions took ~1100 cycles of the ADC interrupt, per phase; this takes ~210):

  • his state, at x256 scale (one ADC count = 256): it moves as soon as alpha x the difference reaches one unit (3.6 ADC counts with alpha 0.0011);
  • his update, lpf += alpha x (sample - lpf), truncated towards zero as his float to int32 conversion does. That truncation holds his state up to 0.5 / alpha units short of the input's mean (1.8 counts with alpha 0.0011, 16 counts at the output with a gain of 9): the filter the old boards were tuned with, kept as it is;
  • alpha and the gain turned into fixed point at compile time (coefficientsOf()), from the float settings of calibration.h: alpha x 2^(16 + shift), gain x 2^8.

The firmware's sample is DC-removed and left-aligned (x64 scale, int16): it is filtered at x256, the output is back at x64, saturated to the int16 range, so that a peak beyond full scale cannot wrap and flip the sign of the power.

Pure C++, no Arduino: the native tests include it.

Definition in file ct_filter.h.