1

I would like to use Bacon to combine time series with irregular timestamps in a single EventStream. Each event would contain the last value of each time series at the given time. Here is a an example

var ts1 = new Bacon.fromArray([
    [1,2],  //Each array is an event,i.e here timestamp is 1 and value is 2
    [2,3],
    [5,9]
    ])

var ts2 = new Bacon.fromArray([
    [4,2],
    [9,3],
    [12,9]
    ])

What I would like to have is something like this

var ts12 =[
    [1,2,undefined], //At time 1, only ts1 was defined
    [2,3,undefined],
    [4,3,2],         //at time 4, we take the last value of ts1 (3) and ts2 (2)
    [5,9,2],
    [9,9,3],
    [12,9,9],
]

I tried to implement it using Bacon.update but I didn't go very far. How would you approach the problem?

LouisChiffre
  • 691
  • 7
  • 15
  • 1
    Are these time series available beforehand in full, as they are in the example, or do their values arrive as events at runtime? In the former case, it might be easier to skip Bacon altogether and simply merge sort the two arrays. – Alex Grigorovitch Sep 02 '14 at 07:10

1 Answers1

1

I make the assumption that your time series values arrive as values in the order of the time parameter. Hence the change to Bacon.later in my code.

var ts1 = new Bacon.mergeAll([
   Bacon.later(100, [1,2]),
   Bacon.later(200, [2,3]),
   Bacon.later(500, [5,9])
])

var ts2 = new Bacon.mergeAll([
   Bacon.later(400, [4,2]),
   Bacon.later(900, [9,3]),
   Bacon.later(1200, [12,9])
])

var ts12 = ts1.toProperty(null).combine(ts2.toProperty(null), function(v1, v2) {
    if(v1 && v2) {
        return [Math.max(v1[0], v2[0]), v1[1], v2[1]]
    } else if(v1) {
        return [v1[0], v1[1], undefined] 
    } else if(v2) {
        return [v2[0], undefined, v2[1]]
    }
}).changes()

ts12.log()

You can play around with the solution in this JSfiddle.

OlliM
  • 7,023
  • 1
  • 36
  • 47