28static double reference(int32_t averagePower,
float cal)
36 const double exact{
static_cast< double >(cal) * ldexp(1.0, shift) };
41static constexpr float typicalCal[]{ 0.042346F, 0.043453F, 0.042969F };
42static constexpr float largeCal[]{ 0.1F, 0.1F, 0.1F };
43static constexpr float smallCal[]{ 0.02F, 0.02F, 0.02F };
53 TEST_ASSERT_EQUAL_UINT8(20, shift);
65 static constexpr float mixed[]{ 0.02F, 0.1F, 0.05F };
75 TEST_ASSERT_GREATER_OR_EQUAL_UINT32(32768,
Energy::toFixed(0.043453F, shift));
76 TEST_ASSERT_LESS_OR_EQUAL_UINT32(65535,
Energy::toFixed(0.043453F, shift));
83 TEST_ASSERT_EQUAL_UINT16(44403, fixed);
91 for (
float cal = 0.005F; cal < 0.5F; cal *= 1.07F)
93 const float set[]{ cal };
112 TEST_ASSERT_TRUE(valid);
119 static constexpr float withZero[]{ 0.05F, 0.0F, 0.05F };
120 static constexpr float withNegative[]{ 0.05F, -0.05F, 0.05F };
121 static constexpr float huge[]{ 10.0F, 10.0F, 10.0F };
137 for (uint8_t phase = 0; phase < 3; ++phase)
143 const double expected{
reference(power, cal[phase]) };
147 TEST_ASSERT_TRUE_MESSAGE(fabs(actual - expected) <= 1.0 + fabs(expected) * 2e-5,
"contribution differs from the float reference");
183 const bool negative{ power < 0 };
184 const uint64_t magnitude{
static_cast< uint64_t
>(negative ? -
static_cast< int64_t
>(power) : power) };
185 const uint64_t product{ (magnitude * calFixed) >> 8 };
187 const auto rounded{
static_cast< int32_t
>((product + (1ULL << (remaining - 1))) >> remaining) };
188 return negative ? -rounded : rounded;
196 uint32_t lcg{ 12345 };
197 const auto next = [&lcg]()
199 lcg = lcg * 1664525UL + 1013904223UL;
203 for (uint8_t shift = 17; shift <= 28; ++shift)
205 const uint8_t remaining{
static_cast< uint8_t
>(shift - 12) };
208 for (int32_t k = 0; k < 64; ++k)
210 for (int32_t d = -1; d <= 1; ++d)
212 const int64_t edge{ (
static_cast< int64_t
>(k) << remaining) + (1L << (remaining - 1)) + d };
213 if ((edge < 0) || (edge >= (1L << 24)))
215 const auto power{
static_cast< int32_t
>(edge) };
222 for (uint16_t
i = 0;
i < 20000; ++
i)
224 const auto power{
static_cast< int32_t
>(next() & 0xFFFFFFUL) * ((next() & 1) ? 1 : -1) };
225 const auto calFixed{
static_cast< uint16_t
>((
i < 100) ? 65535 -
i : next() >> 16) };
237 for (int32_t power = 1; power <=
FULL_SCALE; power += 1009)
250 double expected{ 0.0 };
254 const int32_t power{
static_cast< int32_t
>(20000.0 + 15000.0 * sin(
cycle * 0.01)) + (
cycle % 7) };
259 TEST_ASSERT_TRUE_MESSAGE(fabs(
static_cast< double >(bucket) - expected) <= fabs(expected) * 2e-5 + 64.0,
"the integer bucket drifts from the float one");
280 TEST_ASSERT_EQUAL_UINT8(25, table.shift);
282 TEST_ASSERT_EQUAL_UINT16(44403, table.value[0][32 -
N_MIN_50HZ]);
283 TEST_ASSERT_EQUAL_UINT16(45564, table.value[1][32 -
N_MIN_50HZ]);
284 TEST_ASSERT_EQUAL_UINT16(45056, table.value[2][32 -
N_MIN_50HZ]);
291 TEST_ASSERT_EQUAL_UINT8(24, table.shift);
292 TEST_ASSERT_EQUAL_UINT16(22202, table.value[0][32 - 20]);
300 TEST_ASSERT_GREATER_OR_EQUAL_UINT32(32768, table.value[1][0]);
301 TEST_ASSERT_LESS_OR_EQUAL_UINT32(65535, table.value[1][0]);
308 for (uint8_t phase = 0; phase < 3; ++phase)
312 const int32_t fullScale{
static_cast< int32_t
>(n) *
FULL_SCALE };
314 for (int32_t sumP = -fullScale; sumP <= fullScale; sumP += 49999)
320 TEST_ASSERT_TRUE_MESSAGE(fabs(actual - expected) <= 1.0 + fabs(expected) * 4e-5,
"per-sample contribution differs from the exact reference");
337 for (int32_t sumP = -
static_cast< int32_t
>(n) *
FULL_SCALE; sumP <= static_cast< int32_t >(n) *
FULL_SCALE; sumP += 77777)
342 TEST_ASSERT_TRUE_MESSAGE(fabs(
static_cast< double >(direct - divided)) <= 2.0 + fabs(
static_cast< double >(divided)) * 6e-5,
"the per-sample and division paths disagree");
352 static constexpr uint8_t unexpected[]{ 1, 5, 20, 27, 37, 64, 100, 255 };
354 for (
const uint8_t n : unexpected)
359 const int32_t sumP{ average * n + ((average < 0) ? -(n - 1) : (n - 1)) };
360 const int32_t rescaled{ (sumP / n) *
N_MIN_50HZ };
362 const double actual{
static_cast< double >(
Energy::contribution(rescaled, table.value[1][0], table.shift)) };
365 TEST_ASSERT_TRUE_MESSAGE(fabs(actual - expected) <= 2.0 + fabs(expected) * 4e-5,
"the fallback differs from the exact reference");
380 TEST_ASSERT_TRUE(fabs(
Energy::contribution(sumP, calOverN, table.shift) - expected) <= 1.0 + expected * 4e-5);
381 TEST_ASSERT_TRUE(fabs(
Energy::contribution(-sumP, calOverN, table.shift) + expected) <= 1.0 + expected * 4e-5);
390 double expected{ 0.0 };
394 const uint8_t n{
static_cast< uint8_t
>(31 +
cycle % 3) };
395 const int32_t sumP{ n *
static_cast< int32_t
>(20000.0 + 15000.0 * sin(
cycle * 0.01)) + (
cycle % 11) };
401 TEST_ASSERT_TRUE_MESSAGE(fabs(
static_cast< double >(bucket) - expected) <= fabs(expected) * 4e-5 + 64.0,
"the per-sample bucket drifts from the exact one");
425template< u
int8_t SUPPLY_FREQUENCY >
431 for (int32_t units = -2 * capacity; units <= 2 * capacity; units += 7)
433 const auto expected{
static_cast< int32_t
>(llround(units * 100.0 / unitsPerJoule)) };
constexpr uint8_t SUPPLY_FREQUENCY
Integer arithmetic of the energy bucket.
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.
constexpr uint8_t FRACTION_BITS
int32_t contribution(const int32_t averagePower, const uint16_t calFixed, const uint8_t shift)
Contribution of one mains cycle of one phase to the bucket.
constexpr PerSampleCalibration< NMin, NMax, N > toFixedPerSample(const float(&cal)[N])
Build the per-sample calibration table at compile time.
constexpr int32_t toHundredthsOfJoule(const int32_t units)
An energy in bucket units, in hundredths of a joule (for the datalog).
constexpr bool isValidCalibration(const float(&cal)[N])
Check that the calibration values can be represented.
constexpr int32_t fromWatts(const int32_t watts)
A power in watts, in bucket units per mains cycle.
static constexpr float smallCal[]
static constexpr int32_t FULL_SCALE
static constexpr float largeCal[]
void test_valid_calibration_accepts_realistic_values(void)
void test_contribution_matches_the_reference_for_extreme_calibrations(void)
static void checkAgainstReference(const float(&cal)[3])
Every average power from -FULL_SCALE to +FULL_SCALE, in uneven steps.
void test_hundredths_of_joule_at_50Hz(void)
static double exactReference(int32_t sumP, uint8_t n, float cal)
The exact energy of one cycle: no truncating division, in bucket units.
static double quantisationError(float cal, uint8_t shift)
Relative error of a quantised calibration value.
void test_no_drift_over_one_minute(void)
void test_per_sample_shift_for_60Hz(void)
void test_largest_value_fills_16_bits(void)
static constexpr float typicalCal[]
void test_per_sample_largest_value_fills_16_bits(void)
void test_toFixed_rounds_to_nearest(void)
void test_contribution_rounding_shortcut_is_exact(void)
void test_per_sample_contribution_matches_the_exact_reference(void)
static constexpr uint8_t N_MIN_50HZ
void test_contribution_at_full_scale_does_not_overflow(void)
void test_shift_for_a_typical_calibration(void)
void test_per_sample_shift_and_values(void)
void test_contribution_is_sign_symmetric(void)
static double reference(int32_t averagePower, float cal)
The float arithmetic being replaced, in bucket units.
void test_valid_calibration_rejects_zero_negative_and_huge_values(void)
void test_shift_for_large_and_small_values(void)
void test_per_sample_agrees_with_the_division_path(void)
void test_fromWatts_scales_by_the_fraction_bits(void)
void checkHundredthsOfJoule()
void test_bucket_capacity_fits_in_32_bits(void)
void test_per_sample_fallback_for_an_unexpected_cycle_length(void)
void test_contribution_of_zero_is_zero(void)
void test_shift_follows_the_largest_value(void)
void test_typical_set_quantisation_error(void)
void test_quantisation_error_is_negligible(void)
void test_per_sample_no_drift_over_one_minute(void)
static constexpr uint8_t N_MAX_50HZ
static int32_t generalRounding(int32_t power, uint16_t calFixed, uint8_t shift)
contribution() with the general rounding, (product + 2^(r-1)) >> r, in 64 bits.
void test_contribution_matches_the_reference_for_a_typical_calibration(void)
void test_hundredths_of_joule_at_60Hz(void)
void test_per_sample_full_scale_does_not_overflow(void)
static void cycle(RemoteLoadCore< NumUnits > &core, const LoadStates(&states)[NumLoads])
Run one mains cycle: feed the states to the core, for the given map.