How to increase the tolerance for comparing time points when synchronizing timetables?

13 vues (au cours des 30 derniers jours)
Hi,
TL;DR: I am suffering from floating-point errors in time comparisons when synchronizing timetables and I am hoping that there exist a simple way to increase the tolerance of the comparison to mitigate the problem. Thanks in advance!
---
The following code generates two timetables with timelines that are supposed to be identical besides a shift in their initial values:
N = 9;
dt = .3;
mkTime = @(t0) seconds(t0+dt : dt : t0+N*dt);
t1 = mkTime(0);
t2 = mkTime(4*dt);
v1 = (1:N)';
v2 = (1:N)';
tt1 = timetable(v1, 'RowTimes', t1)
tt1 = 9×1 timetable
Time v1 _______ __ 0.3 sec 1 0.6 sec 2 0.9 sec 3 1.2 sec 4 1.5 sec 5 1.8 sec 6 2.1 sec 7 2.4 sec 8 2.7 sec 9
tt2 = timetable(v2, 'RowTimes', t2)
tt2 = 9×1 timetable
Time v2 _______ __ 1.5 sec 1 1.8 sec 2 2.1 sec 3 2.4 sec 4 2.7 sec 5 3 sec 6 3.3 sec 7 3.6 sec 8 3.9 sec 9
However, when tt1 and tt2 are synchronized, the resulting timetable contains multiple 'duplicates' of time points that are on the order of 1e-16 apart from each other (in this case at times 1.8 and 2.1):
tt = synchronize(tt1, tt2)
tt = 15×2 timetable
Time v1 v2 _______ ___ ___ 0.3 sec 1 NaN 0.6 sec 2 NaN 0.9 sec 3 NaN 1.2 sec 4 NaN 1.5 sec 5 1 1.8 sec 6 NaN 1.8 sec NaN 2 2.1 sec 7 NaN 2.1 sec NaN 3 2.4 sec 8 4 2.7 sec 9 5 3 sec NaN 6 3.3 sec NaN 7 3.6 sec NaN 8 3.9 sec NaN 9
tt.Time(7) - tt.Time(6)
ans = duration
2.2737e-16 sec
I would very much like for these duplicates to be recognized as having identical times instead, hence my question: How can the tolerance for comparing time points be increased, so that it does not suffer from these precision errors?
---
Some remarks:
The older version of synchronize for timeseries offered this possibility via the 'tolerance' keyword, which leads me to assume that something similar should be available for timetable.
The documentation for synchronize offers multiple interpolation and resampling strategies for dealing with overlapping but misaligned time data:
tt_wrong = synchronize(tt1, tt2, 'regular', 'linear', TimeStep = seconds(dt))
tt_wrong = 13×2 timetable
Time v1 v2 _______ __ __ 0.3 sec 1 -3 0.6 sec 2 -2 0.9 sec 3 -1 1.2 sec 4 0 1.5 sec 5 1 1.8 sec 6 2 2.1 sec 7 3 2.4 sec 8 4 2.7 sec 9 5 3 sec 10 6 3.3 sec 11 7 3.6 sec 12 8 3.9 sec 13 9
However, it seems like overkill to employ such methods for mere precision errors and, moreover, they do unfortunately not apply in the current scenario, since the result pads the nonoverlapping pieces of each timetable with nonsense values.
---
Any help will be much appreciated, thanks in advance!
Julius
  2 commentaires
Julius
Julius le 2 Sep 2023
Note to self: it appears that these errors are preventable by swapping the timelines to single-precision floats, but I am not knowledgeable enough to predict whether this will prevent these issues in all cases. In case anyone can comment on this I would be happy to hear it.
Star Strider
Star Strider le 2 Sep 2023
I am not certain what the exact problem is (since this seems to be a proxy problem). Consider using the ismembertol function.

Connectez-vous pour commenter.

Réponses (2)

Les Beckham
Les Beckham le 1 Sep 2023
Modifié(e) : Les Beckham le 1 Sep 2023
Based on this section of the documentation: Synchronize Timetables to Arbitrary Time Vector
N = 9;
dt = .3;
mkTime = @(t0) seconds(t0+dt : dt : t0+N*dt);
t1 = mkTime(0);
t2 = mkTime(4*dt);
v1 = (1:N)';
v2 = (1:N)';
tt1 = timetable(v1, 'RowTimes', t1);
tt2 = timetable(v2, 'RowTimes', t2);
% create the time vector for synchronizing with a new time vector
t = seconds(uniquetol(seconds(union(t1, t2)), 1e-4)) % this is the tricky/messy bit
t = 1×13 duration array
0.3 sec 0.6 sec 0.9 sec 1.2 sec 1.5 sec 1.8 sec 2.1 sec 2.4 sec 2.7 sec 3 sec 3.3 sec 3.6 sec 3.9 sec
TT = synchronize(tt1, tt2, t, 'linear')
TT = 13×2 timetable
Time v1 v2 _______ __ __ 0.3 sec 1 -3 0.6 sec 2 -2 0.9 sec 3 -1 1.2 sec 4 0 1.5 sec 5 1 1.8 sec 6 2 2.1 sec 7 3 2.4 sec 8 4 2.7 sec 9 5 3 sec 10 6 3.3 sec 11 7 3.6 sec 12 8 3.9 sec 13 9
  5 commentaires
Julius
Julius le 2 Sep 2023
@Les Beckham My problem is as I described in my original post: I am dealing with "timetables with timelines that are supposed to be identical besides a shift in their initial values", i.e. partially overlapping and partially nonoverlapping. Further, I wish to obtain a synchronized timetable containing both original timetables as separate columns. This combined scenario disallows both vertical and horizontal concatenation of timetables, the former because it would attempt to merge the tables into a single column and the latter because that operation requires equal timelines for the operands.
Don't mistake my appreciation for your efforts, but your answer does not solve the problem stated in my question. Please note that I even added a remark at the bottom to explain why interpolation is not a possible solution for my problem.
If the problem scenario is not clear enough I will happily supply further data/explanation, but it seems to me that all the relevant parts are present.
Julius
Julius le 2 Sep 2023
@Star Strider Thank you, I am aware of this feature for timeseries, which was part of the motivation for this inquiry about potential equivalent functionality for timetables, since they hold my preference over timeseries. More importantly, however, as far as I can tell, timeseries synchronization offers no possibility of including nonoverlapping parts of the input series. In case my perception is wrong and you know a way to enable this I would love to hear it!

Connectez-vous pour commenter.


Seth Furman
Seth Furman le 13 Sep 2023
If possible, please provide more detail about the actual data so that I can try to provide better guidance. For example,
  • Where are the row-times coming from?
  • Are the row-times regular?
  • Do the data only need to be precise to second?
In this particular case, simply creating the time vectors using duration arithmetic instead of double arithmetic avoids the precision issue.
N = 9;
dt = seconds(.3);
mkTime = @(t0) t0+dt : dt : t0+N*dt;
t1 = mkTime(seconds(0));
t2 = mkTime(4*dt);
v1 = (1:N)';
v2 = (1:N)';
tt1 = timetable(v1, 'RowTimes', t1)
tt1 = 9×1 timetable
Time v1 _______ __ 0.3 sec 1 0.6 sec 2 0.9 sec 3 1.2 sec 4 1.5 sec 5 1.8 sec 6 2.1 sec 7 2.4 sec 8 2.7 sec 9
tt2 = timetable(v2, 'RowTimes', t2)
tt2 = 9×1 timetable
Time v2 _______ __ 1.5 sec 1 1.8 sec 2 2.1 sec 3 2.4 sec 4 2.7 sec 5 3 sec 6 3.3 sec 7 3.6 sec 8 3.9 sec 9
tt = synchronize(tt1, tt2)
tt = 13×2 timetable
Time v1 v2 _______ ___ ___ 0.3 sec 1 NaN 0.6 sec 2 NaN 0.9 sec 3 NaN 1.2 sec 4 NaN 1.5 sec 5 1 1.8 sec 6 2 2.1 sec 7 3 2.4 sec 8 4 2.7 sec 9 5 3 sec NaN 6 3.3 sec NaN 7 3.6 sec NaN 8 3.9 sec NaN 9
tt.Time(7) - tt.Time(6)
ans = duration
0.3 sec

Catégories

En savoir plus sur Time Series dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by