30static constexpr float newBoard50Cal[]{ 0.1432025F, 0.1432025F, 0.1432025F };
31static constexpr float newBoard20Cal[]{ 0.057281F, 0.057281F, 0.057281F };
32static constexpr float smallCal[]{ 0.02F, 0.02F, 0.02F };
33static constexpr float largeCal[]{ 0.5F, 0.5F, 0.5F };
37static constexpr float newBoardVCal[]{ 0.888065256F, 0.888065256F, 0.888065256F };
42 return -
static_cast< double >(averagePower) * cal;
46static double referenceVrms(uint32_t meanSquare, uint16_t factor,
float cal)
48 return static_cast< double >(factor) * cal * sqrt(
static_cast< double >(meanSquare));
67 if (fabs(expected) > INT16_MAX)
71 const double error{ fabs(
Datalog::watts(-average, fixed.value[0], fixed.shift) - expected) - fabs(expected) * ldexp(1.0, -16) };
89static double worstVrmsError(uint16_t factor,
const float (&cal)[N], uint32_t maxMeanSquare)
93 for (uint8_t phase = 0; phase < N; ++phase)
95 for (uint32_t meanSquare = 0; meanSquare <= maxMeanSquare; meanSquare += 13)
97 const double expected{
referenceVrms(meanSquare, factor, cal[phase]) };
98 const double error{ fabs(
Datalog::vrms(meanSquare, fixed.value[phase], fixed.shift) - expected) - expected * ldexp(1.0, -14) };
118 for (uint32_t root = 0; root <= UINT16_MAX; ++root)
120 const uint32_t square{ root * root };
127 if (root < UINT16_MAX)
129 TEST_ASSERT_EQUAL_UINT16(root + 1,
Datalog::isqrt(square + root + 1));
137 constexpr uint32_t STRIDE{ 2147 };
139 for (uint32_t
i = 0;
i < (UINT32_MAX / STRIDE); ++
i, value += STRIDE)
141 const auto expected{
static_cast< uint32_t
>(lround(sqrt(
static_cast< double >(value)))) };
142 if (
Datalog::isqrt(value) != (expected > UINT16_MAX ? UINT16_MAX : expected))
144 TEST_FAIL_MESSAGE(
"isqrt() differs from round(sqrt())");
168 uint8_t fractionBits;
169 for (uint32_t value = 1; value < (1UL << 30); value = value * 3 + 1)
172 TEST_ASSERT_GREATER_OR_EQUAL_UINT16(1U << 15, root);
173 TEST_ASSERT_FLOAT_WITHIN(0.5, sqrt(
static_cast< double >(value)) * ldexp(1.0, fractionBits), root);
179 uint8_t fractionBits{ 99 };
181 TEST_ASSERT_EQUAL_UINT8(0, fractionBits);
193 TEST_ASSERT_EQUAL_UINT16(
Energy::toFixed(0.05F, fixed.shift), fixed.value[0]);
216 for (int32_t average = 0; average <= 200000; average += 101)
235 auto truncated{
static_cast< int16_t
>(sumP *
oldBoardCal[0]) };
237 TEST_ASSERT_INT_WITHIN(1, truncated,
Datalog::watts(-sumP, fixed.value[0], fixed.shift));
260 const uint32_t sets{ 30UL * 50 * 36 };
261 TEST_ASSERT_TRUE(
static_cast< uint64_t
>(sets) * ((1UL << 18) >>
Datalog::powerShift(sets)) <= INT32_MAX);
274 const int32_t sums[]{ 0, 1, -1, 15, -15, 123456789, -123456789, 54000L * 16384 - 1, -(54000L * 16384 - 1), 3374, -3374 };
275 const uint16_t counts[]{ 1, 7, 9000, 16383, 16384, 54000, UINT16_MAX };
276 for (
const int32_t sum : sums)
278 for (
const uint16_t n : counts)
280 const auto exact{
static_cast< int32_t
>(
static_cast< int64_t
>(sum) * 16 / n) };
288static void checkShiftedAverage(
const int32_t amplitude,
const int32_t offset,
const int32_t noise,
const int32_t delta)
290 const uint16_t n{ 30U * 50 * 32 };
291 uint32_t lcg{ 12345 };
294 for (uint16_t
i = 0;
i != n; ++
i)
296 lcg = lcg * 1664525UL + 1013904223UL;
297 const int32_t dither{ noise ?
static_cast< int32_t
>(lcg >> 16) % (2 * noise + 1) - noise : 0 };
298 const auto instP{
static_cast< int32_t
>(lround(offset + amplitude * sin(2.0 * M_PI *
i / 32.0 + 0.1))) + dither };
299 scaled += (instP + 8) >> 4;
308 const int32_t amplitudes[]{ 0, 1, 7, 100, 5000, 131072 };
309 const int32_t offsets[]{ 0, 3, -3, 1000, -77777, 131071 };
310 for (
const int32_t amplitude : amplitudes)
312 for (
const int32_t offset : offsets)
322 const int32_t amplitudes[]{ 0, 100, 5000, 131072 };
323 const int32_t offsets[]{ 0, 3, -3, 1000, -77777 };
324 for (
const int32_t amplitude : amplitudes)
326 for (
const int32_t offset : offsets)
337 TEST_ASSERT_EQUAL_UINT8(9, fixed.shift);
338 TEST_ASSERT_GREATER_OR_EQUAL_UINT16(1U << 15, fixed.value[2]);
359 const auto meanSquare{
static_cast< uint32_t
>(steps * steps) };
362 TEST_ASSERT_INT_WITHIN(2, 23000,
Datalog::vrms(meanSquare, fixed.value[0], fixed.shift));
368 TEST_ASSERT_EQUAL_UINT16(0,
Datalog::vrms(0, fixed.value[0], fixed.shift));
374 TEST_ASSERT_EQUAL_UINT16(UINT16_MAX,
Datalog::vrms(UINT32_MAX, fixed.value[0], fixed.shift));
388 static constexpr float zeroCal[]{ 0.8F, 0.0F, 0.8F };
389 static constexpr float negativeCal[]{ 0.8F, 0.8F, -0.8F };
390 static constexpr float tinyCal[]{ 0.0001F, 0.0001F, 0.0001F };
Integer arithmetic of the datalog: power and RMS voltage per phase.
constexpr uint16_t vrmsFactor(const uint8_t datalogPeriodInSeconds)
Scaling of the datalog voltage, Vrms_L_x100.
constexpr FixedCalibration< N > voltageCalibration(const uint16_t factor, const float(&cal)[N])
The voltage calibration in fixed point, for vrms(), built at compile time.
int16_t watts(const int32_t averagePower, const uint16_t calFixed, const uint8_t shift)
The average power of a phase, in watts.
uint16_t isqrt(const uint32_t value)
Integer square root, rounded to the nearest integer.
constexpr FixedCalibration< N > powerCalibration(const float(&cal)[N])
The power calibration in fixed point, for watts(), built at compile time.
constexpr bool isValidVoltageCalibration(const uint16_t factor, const float(&cal)[N])
Check that the voltage calibration values can be represented.
constexpr uint8_t powerShift(const uint32_t maxSampleSets)
The shift by which the ISR scales the power down before adding it to the datalog sum.
int32_t averagePower(const int32_t sum, const Count sampleSets)
The average power of a datalog period, back at the x1 scale of V_ADC x I_ADC.
uint16_t vrms(const uint32_t meanSquare, const uint16_t calFixed, const uint8_t shift)
The RMS voltage of a phase.
uint16_t sqrtScaled(const uint32_t meanSquare, uint8_t &fractionBits)
The square root of a mean square, with as many fractional bits as fit.
constexpr uint8_t calibrationShift(const float(&cal)[N])
Largest shift that keeps every calibration value within 16 bits.
constexpr uint16_t toFixed(const float cal, const uint8_t shift)
A calibration value as a 16-bit fixed-point constant.
void test_voltage_calibration_is_constexpr(void)
static constexpr float newBoard50Cal[]
void test_power_shift_follows_the_sample_count(void)
void test_isqrt_at_every_boundary(void)
Every rounding boundary of every root: round(sqrt(x)) changes from r to r + 1 between r^2 + r and r^2...
void test_power_calibration_is_constexpr(void)
void test_vrms_matches_the_reference_short_period(void)
void test_watts_saturates_beyond_int16(void)
static double referenceVrms(uint32_t meanSquare, uint16_t factor, float cal)
The float arithmetic being replaced, in hundredths of a volt (x4 for long periods).
static constexpr float oldBoardCal[]
void test_average_power_shifted_is_the_exact_average(void)
void test_shifted_sum_keeps_the_average_of_a_noisy_signal_over_30_s(void)
static double referenceWatts(int32_t averagePower, float cal)
The float arithmetic being replaced, in watts (import = +ve).
void test_vrms_of_zero(void)
void test_watts_matches_the_reference_extremes(void)
static void checkShiftedAverage(const int32_t amplitude, const int32_t offset, const int32_t noise, const int32_t delta)
void test_watts_differs_from_the_float_code_by_at_most_1_W(void)
void test_isqrt_matches_the_reference(void)
2 million values over the whole 32-bit range, against sqrt() in double precision (exact here).
static constexpr float smallCal[]
static double worstWattsError(const float(&cal)[N])
Largest error of watts() against the reference, over the full input range, beyond what the 16-bit cal...
void test_isqrt_edge_cases(void)
void test_sqrtScaled_keeps_15_significant_bits(void)
void test_average_power_unshifted_is_the_quotient(void)
void test_isqrt_of_the_largest_value_saturates(void)
static constexpr int32_t FULL_SCALE
void test_shifted_sum_is_within_half_the_shift_over_30_s(void)
void test_watts_is_sign_symmetric(void)
void test_watts_matches_the_reference_old_board(void)
static constexpr float largeCal[]
static constexpr float oldBoardVCal[]
void test_vrms_factor_follows_the_scaling_of_the_isr(void)
void test_watts_matches_the_reference_new_board(void)
void test_vrms_matches_the_reference_long_period(void)
void test_vrms_at_230_V(void)
static constexpr float newBoard20Cal[]
void test_power_shift_keeps_30_s_of_any_signal_in_32_bits(void)
static constexpr float newBoardVCal[]
void test_sqrtScaled_of_zero(void)
static double worstVrmsError(uint16_t factor, const float(&cal)[N], uint32_t maxMeanSquare)
Largest error of vrms() against the reference, from 0 to the largest ADC voltage, beyond what the 16-...
void test_valid_voltage_calibration(void)
void test_vrms_saturates(void)