# Resampling Data using Temporal Data

I am having a problem using TemporalData. I am simulating a two state system, and every time the system goes into a new state (which happens randomly), then I put a time stamp on it. The simulation finishes at t = 10.

If I run multiple simulations, then I end up generating a series of events that took place at different times in the interval t = 0 to t = 10. The problem is that the time axis for the events are not aligned, and I want to align them.

If I use TemporalData, then it seems to resample my "y" axis which represents the state of the system. Consequently, rather than having a plot that oscillates between state 1 and state 2, I get a plot that stays constant at 1.5, which is not what I want because the states are discrete {1,2,...}

How do I align the time axis of the two paths without affecting the Y data and making sure all paths have the same time axis so that I can do arithmatic on paths (ie I want to add them).

Here is an example: The data is in the form of {time, state}, and notice that the length of lst1 and lst2 are different. (there could be up to 1000 lists of different lengths for my application)

lst1 = {{5.90442, 1}, {6.22215, 2}, {6.59706, 1}, {7.82687, 2},
{9.18754, 1}, {9.85393, 2}, {10.6098, 1}};

lst2 = {{7.27318, 1}, {9.51751, 2}, {10.424, 1}};

lst = {lst1, lst2}


If I plot one for example:

ListPlot[lst[[1]]]


The raw trace is not what I want because the states should be discrete. So I resample the trace at a finer grid.

temporalData =
TemporalData[lst,
ResamplingMethod -> {"Interpolation", InterpolationOrder -> 0,
"HoldFrom" -> Left}];
tsr = TimeSeriesResample[temporalData, 0.001];
ListPlot[{tsr["Paths"][[1]], lst[[1]]}, Joined -> True]


If we plot the raw trace and the corrected trace together we can see that the code above gives the desired shape of the trace. (blue trace)

However I can't do arithmetic on on the traces because they are of different lengths. How do I both evenly space the traces and make them the same length?

tsr["Paths"][[1]] // Length
tsr["Paths"][[2]] // Length


EDIT: If I add {0,1} to each list and make both lists end at the same time then temporal data slices them up evenly.

 lst1 = {{0,1},{5.90442, 1}, {6.22215, 2}, {6.59706, 1}, {7.82687, 2},
{9.18754, 1}, {9.85393, 2}, {10.6098, 1}};

lst2 = {{0,1},{7.27318, 1}, {9.51751, 2}, {10.424, 1}, {10.6098,1}};

• You haven't defined the list lst Oct 19 '15 at 22:49
• Thanks @belisariusisforth. I have updated my question. Oct 20 '15 at 0:23
• Your example data is incompatible with the assertion that state transitions occur for $t \in [0,10]$ since transitions at $t = 10.6098$ and $t=10.424$ appear. What is the actual range of times for the simulation? Oct 20 '15 at 5:33
• You may wish to add a state "transition" at time $t=0$ corresponding to the initial state. Oct 20 '15 at 5:36
• @EricTowers I've edited my question. And I have noticed that if I add the transition at t=0 and I make both paths end at the same time, by adding a final transition where time is equal to the greatest time among the paths, then Paths will resample the paths at equal intervals. So far i've done this manually and i'm working on some code to do it for an arbitrary number of lists. Oct 20 '15 at 17:32

lst1 = {{5.90442, 1}, {6.22215, 2}, {6.59706, 1}, {7.82687, 2}, {9.18754, 1},
{9.85393, 2}, {10.6098, 1}};
lst2 = {{7.27318, 1}, {9.51751, 2}, {10.424, 1}};
lst = {lst1, lst2};


and their TemporalData

temporalData =
TemporalData[lst,
ResamplingMethod -> {"Interpolation", InterpolationOrder -> 0, "HoldFrom" -> Left}];


the individual paths can be plotted using

Plot[Evaluate[#[t] & /@ temporalData["PathFunction", All]], {t, 0, 12}]


and the sum of both paths can be plotted using

Plot[Evaluate[Total[#[t] & /@ temporalData["PathFunction", All]]], {t, 0, 12}]


or

Plot[Evaluate[Through[(Plus @@ temporalData["PathFunction", All])[t]]], {t, 0,12}]