CODE¶
Detection Implementation — Per-Mode Comparison + Thresholding
void tiny_damage_detect(const tiny_sysid_result_t *cur, tiny_damage_detect_t *out)
{
memset(out, 0, sizeof(*out));
if (!g_has_baseline || !cur || cur->n_modes < 1) return;
int nm = min(cur->n_modes, g_baseline.n_modes);
float fd_max = 0, ml_max = 0, dd_max = 0;
for (int m = 0; m < nm; m++) {
// Frequency shift %
float fd = 100.0f * fabsf(cur->frequencies[m] - g_baseline.frequencies[m])
/ g_baseline.frequencies[m];
// MAC loss
float ml = 1.0f - tiny_sysid_mac(cur->shapes[m], g_baseline.shapes[m], cur->n_ch);
// Damping change %
float dd = 100.0f * fabsf(cur->damping[m] - g_baseline.damping[m])
/ g_baseline.damping[m];
if (fd > fd_max) fd_max = fd;
if (ml > ml_max) ml_max = ml;
if (dd > dd_max) dd_max = dd;
}
bool f = (fd_max > 5.0f), m = (ml_max > 0.15f), d = (dd_max > 100.0f);
out->flags = (f ? 1 : 0) | (m ? 2 : 0) | (d ? 4 : 0);
float di = (fd_max/15.0f + ml_max/0.30f + dd_max/300.0f) / 3.0f;
out->index = min(di, 1.0f);
out->damaged = (out->flags >= 2) || (out->index > 0.5f);
}
Key logic: per-mode max comparison, normalized weighted average, bitmask flags.