0

I have two sensors measuring the same thing over time. They don't sample at the same rate and one of them samples at irregular times. The start times recorded by the sensors are also not the same (their clocks aren't in sync.. and I can't change that). Still, they sample often enough that it's obviously the same function and the human eye has no trouble aligning them.

I want to align the data, which involves some combination of stretching and shifting the arrays (something in scipy.signal?). I'm totally fine with using a solver to do this.. I'm just not sure how to do it.

I found a few posts where people had just shifted the data or simply resampled time series data that's taken at regular intervals. But I can't find a problem that's as much of a mess as mine, unfortunately. I'm not entirely sure that this is possible without resampling the irregularly sampled data to make it regular and then doing the stretch/shift... but I'm not sure how to do any of those three things. This seems like a signal processing problem that people have surely run across before. Is there a canonical answer to this?

Here's an example of the sort of data:

x = np.arange(-2,2,0.01)
def f(x):
    return x**2

# Now find a bunch of random indices to represent the irregular sampling by one sensor
x1 = np.sort((np.array((np.random.random(20)*4-2)*100).astype(int))/100)

# sample this one at regular intervals
x2 = x[::19]

fig,ax = plt.subplots(1,3,figsize=(8,3))
ax[0].plot(x,f(x))
ax[1].scatter(x1,f(x1),color=[0,0,1,0.5])
ax[1].plot(x1,f(x1),color=[0,0,0,0.2])
ax[2].scatter(x2,f(x2),lw=1,color=[0,.5,.5,.5])
ax[2].plot(x2,f(x2),color=[0,0,0,0.2]);

enter image description here

Burnt Umber
  • 133
  • 1
  • 9
  • Could you add a text sample with raw data and what you wish as transformed data please? – Synthase Dec 01 '20 at 05:28
  • That's what x1 and x2 are supposed to be. I'd like to align x1 to x2. It's difficult because, even though they come from the same truth source, x1 doesn't start in the same place... I don't see an obvious way of stretching x1 to the size of x2 while also fitting it in the best way. It's rather like taking the curves created by x1 and x2 and asking how best to overlay them to minimize error – Burnt Umber Dec 01 '20 at 13:55
  • I believe that it would be possible to feed this to a solver with the stretching coefficient and the shift amount as the two variables, where the output is the MSE (mean squared error) between x2 and the modified form of x1. If someone could help with this, I would greatly appreciate it. I tried using scipy.interpolate.interp1d, but that didn't work properly... and I'm not sure which solver to use or how to feed it. – Burnt Umber Dec 01 '20 at 13:59
  • The goal of this, I should have said, is to see how good one sensor is relative to another (so if x2 is used as truth, how good is x1 in comparison?) – Burnt Umber Dec 01 '20 at 14:58

0 Answers0