return calloc(sizeof(struct owparser_state), i);
}
-static int might_be_glitch(double data, const struct owparser_state *s)
+static double max_glitch_delta(const struct owparser_state *s)
{
- double max_delta = 0, delta;
+ double max_delta = 0;
int i;
for (i = 0; i < ARRAY_SIZE(s->prev_delta); i++)
max_delta = max(s->prev_delta[i], max_delta);
+ return max_delta;
+}
+
+static int might_be_glitch(double data, const struct owparser_state *s)
+{
+ double max_delta, delta;
+
+ max_delta = max_glitch_delta(s);
+
/* Probably no enough data yet, so no glitch detection */
- if (!max_delta)
+ if (max_delta == 0)
return 0;
/*
* Simple glitch detection. If delta to previous value is more
- * than twice as larger as some older delta, we might have a
- * glit
+ * than twice as larger as any of the older delta, we might
+ * have a glitch
*/
delta = fabs(data - s->prev_data);
+
return delta > max_delta * 2;
}
tmp = NULL;
/*
- * If we read the same value as previously,
- * it's not a glitch
+ * If we read the almost same value as
+ * previously, it's not a glitch
*/
- if (glitches && prev_data == data)
- break;
+ if (glitches && prev_data != 85) {
+ double d = max_glitch_delta(&state[i]);
+
+ if (fabs(data - prev_data) <= d * 2)
+ break;
+ }
if (might_be_glitch(data, &state[i]) &&
glitches < 2 && retries < 7) {