user3025898
user3025898

Reputation: 571

align three time series in python

I have the following time series: enter image description here

I would like to align the signals only after time = 800, based on having the minimum points aligned.

I have tried the following to align two of the signals in pandas:

from pandas import *
import pandas as pd
s1 = Series(vec1)
s2 = Series(vec2)
s3 = s1.align(s2,join='inner')

s1 = np.array(s1)
s2 = np.array(s2)
s3 = np.array(s3)

plt.plot(t,s1)
plt.plot(t,s2)
plt.plot(t,s3)
plt.show()

Which shows the aligned signals exactly as the original form. Any recommendations on how to achieve the minimum alignment?

Upvotes: 6

Views: 3737

Answers (2)

cphlewis
cphlewis

Reputation: 16269

To find and align the minima:

import matplotlib.pyplot as plt  #making toy data
x = np.arange(0, 2, .05)

s = []
s.append(np.sin(x*3))
s.append(np.cos(x*4+0.3))
s.append(np.sin(x)*np.exp(2-x)*-1)
plt.clf()
plt.plot(x, s[0], x, s[1], x, s[2])
plt.show()
plt.clf()

enter image description here

mindexes = [i.argmin() for i in s]  #finding the minima
shifts = mindexes - min(mindexes)

def shifted(shift, x, s):
    if shift == 0:   #x[:-0] was not what I wanted.
        return x, s
    else:
        return x[:-1*shift], s[shift:]

for i in (0,1,2):
    px, py = shifted(shifts[i], x, s[i]) 
    plt.plot(px, py)
plt.show()

enter image description here

Upvotes: 6

tnknepp
tnknepp

Reputation: 6263

It is a bit crude, but why not implement a while loop? import numpy as np import pandas as pd

# assuming you are shifting s2 to the left
# cut off at 800 s
s1 = s1[s1.index>=800]
s2 = s2[s2.index>=800]

while s2.index[s2==s2.min()]>s1.index[s1==s1.min()]:
    s2 = s2[801:]
    s2.index = np.arange(800,800 + s2.shape[0])

Upvotes: 1

Related Questions