Search code examples
pythonarraystimestamputc

how to align similar values in two arrays in python


I am trying to align two videos using their utc timestamps.

for example:

video 1 timestamps = 1234.4321, 1234.4731, 1234.5432, 1234.5638, ...

video 2 timestamps = 1234.4843, 1234.5001, 1234.5632, 1234.5992, ...

I would like to align them so that the closest timestamps within a .0150s window are aligned without aligning two values from one array to one value in the second array.

example output:

video 1 timestamps = 1234.4321, 1234.4731, _________, 1234.5432, 1234.5638, _________, ...

video 2 timestamps = _________, 1234.4843, 1234.5001, _________, 1234.5632, 1234.5992, ...

Can someone help?

EDIT

There was a little confusion with the timestamps. The issue isn't that they simply need to be shifted once every two values. Hopefully this updated example will clear it up. Both examples are correct. A single solution should be able to solve both.

Example 2:

timestamp3 = 1590595834.6775, 1590595834.70479, 1590595834.73812, 1590595834.77163, 1590595834.80438

timestamp4 = 1590595835.70971, 1590595835.73674, 1590595835.7695, 1590595835.80338, 1590595835.83634

output:

timestamp3 = 1590595835.6775, 1590595835.70479, 1590595835.73812, 1590595835.77163, 1590595835.80438, _______________, ...

timestamp4 = _______________, 1590595835.70971, 1590595835.73674, 1590595835.7695, 1590595835.80338, 1590595835.83634, ...


Solution

  • Something like this:

    timestamp3 = [1590595834.6775, 1590595834.70479, 1590595834.73812, 1590595834.77163, 1590595834.80438]
    timestamp4 = [1590595834.70971, 1590595834.73674, 1590595834.7695, 1590595834.80338, 1590595834.83634]
    
    len3 = len(timestamp3)
    len4 = len(timestamp4)
    ins = '_____________'
    diff = 0.015
    ii = jj = 0
    while True:
        if timestamp3[ii] < timestamp4[jj] - diff:
            timestamp4.insert(jj, ins)
            len4 += 1
        elif timestamp4[ii] < timestamp3[jj] - diff:
            timestamp3.insert(ii, ins)
            len3 += 1
    
        ii += 1
        jj += 1
        if ii == len3 or jj == len4:
            if len3 > len4:
                timestamp4.extend([ins]*(len3-len4))
            elif len4 > len3:
                timestamp3.extend([ins]*(len4-len3))
            break
    
    print(timestamp3)
    print(timestamp4)
    

    Gives:

    [1590595834.6775, 1590595834.70479, 1590595834.73812, 1590595834.77163, 1590595834.80438, '_____________']
    ['_____________', 1590595834.70971, 1590595834.73674, 1590595834.7695, 1590595834.80338, 1590595834.83634]