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.
How to increase the tolerance for comparing time points when synchronizing timetables?
10 views (last 30 days)
Show older comments
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)
tt2 = timetable(v2, 'RowTimes', t2)
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.Time(7) - tt.Time(6)
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))
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 Comments
Star Strider
on 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.
Answers (2)
Les Beckham
on 1 Sep 2023
Edited: Les Beckham
on 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
TT = synchronize(tt1, tt2, t, 'linear')
5 Comments
Seth Furman
on 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)
tt2 = timetable(v2, 'RowTimes', t2)
tt = synchronize(tt1, tt2)
tt.Time(7) - tt.Time(6)
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!