From 32a58a93a6c479e430e349895807ec6f7ab8b3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ge=CC=81ry=20Casiez?= <=> Date: Tue, 13 Aug 2024 17:55:32 +0200 Subject: [PATCH] Integrating the use case where the new timestamp is equal or even smaller than the last timestamp and causing incorrect freq update --- C/SF1eFilter.c | 2 +- CppUsingTemplates/1efilter.cc | 2 +- java/OneEuroFilter.java | 2 +- javascript/OneEuroFilter.js | 2 +- python/OneEuroFilter/OneEuroFilter.py | 2 +- typescript/src/OneEuroFilter.ts | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/C/SF1eFilter.c b/C/SF1eFilter.c index c114e9d..17fd731 100644 --- a/C/SF1eFilter.c +++ b/C/SF1eFilter.c @@ -112,7 +112,7 @@ float SF1eFilterDo(SF1eFilter *filter, float x) float SF1eFilterDoAtTime(SF1eFilter *filter, float x, double timestamp) { - if(filter->lastTime != 0) { + if(filter->lastTime != 0 && timestamp > filter->lastTime) { filter->frequency = 1.0f / (timestamp - filter->lastTime); } filter->lastTime = timestamp; diff --git a/CppUsingTemplates/1efilter.cc b/CppUsingTemplates/1efilter.cc index 09098a7..e587e31 100644 --- a/CppUsingTemplates/1efilter.cc +++ b/CppUsingTemplates/1efilter.cc @@ -56,7 +56,7 @@ struct one_euro_filter { T operator()(T x, timestamp_t t = -1) { T dx = 0; - if(last_time_ != -1 && t != -1 && t != last_time_) { + if(last_time_ != -1 && t != -1 && t != last_time_ && t > last_time_) { freq = 1.0 / (t - last_time_); } last_time_ = t; diff --git a/java/OneEuroFilter.java b/java/OneEuroFilter.java index ddf1197..61ed2dc 100755 --- a/java/OneEuroFilter.java +++ b/java/OneEuroFilter.java @@ -159,7 +159,7 @@ private void init(double freq, double filter(double value, double timestamp) throws Exception { // update the sampling frequency based on timestamps - if (lasttime != UndefinedTime && timestamp != UndefinedTime) { + if (lasttime != UndefinedTime && timestamp != UndefinedTime && timestamp > lasttime) { freq = 1.0 / (timestamp - lasttime); } diff --git a/javascript/OneEuroFilter.js b/javascript/OneEuroFilter.js index 388bd4d..cbdf0ae 100755 --- a/javascript/OneEuroFilter.js +++ b/javascript/OneEuroFilter.js @@ -125,7 +125,7 @@ export class OneEuroFilter { filter(value, timestamp=undefined) { // update the sampling frequency based on timestamps - if (this.lasttime!=undefined && timestamp!=undefined) + if (this.lasttime!=undefined && timestamp!=undefined && timestamp > this.lasttime) this.freq = 1.0 / (timestamp-this.lasttime) ; this.lasttime = timestamp ; // estimate the current variation per second diff --git a/python/OneEuroFilter/OneEuroFilter.py b/python/OneEuroFilter/OneEuroFilter.py index 581022b..7b01b8f 100644 --- a/python/OneEuroFilter/OneEuroFilter.py +++ b/python/OneEuroFilter/OneEuroFilter.py @@ -124,7 +124,7 @@ def __call__(self, x:float, timestamp:float=None) -> float: """ # ---- update the sampling frequency based on timestamps - if self.__lasttime and timestamp: + if self.__lasttime and timestamp and timestamp>self.__lasttime: self.__freq = 1.0 / (timestamp-self.__lasttime) self.__lasttime = timestamp # ---- estimate the current variation per second diff --git a/typescript/src/OneEuroFilter.ts b/typescript/src/OneEuroFilter.ts index dc792b7..e461f55 100755 --- a/typescript/src/OneEuroFilter.ts +++ b/typescript/src/OneEuroFilter.ts @@ -195,7 +195,7 @@ export class OneEuroFilter { public filter(value : number, timestamp : number | undefined) : number { // update the sampling frequency based on timestamps - if (this.lasttime!=undefined && timestamp!=undefined) + if (this.lasttime!=undefined && timestamp!=undefined && timestamp>this.lasttime) this.freq = 1.0 / (timestamp-this.lasttime) ; this.lasttime = timestamp ; // estimate the current variation per second