1

Problem

I have two arrays (Xa and Xb) that contain measurements of the same physical signal, but they are taken at different sample rates. Lastly, physical logging of Xa data starts at a different time, than that of Xb. The logging of data also stops at different time.

i.e.

(The following is just a summary of important statements, not code.)

sampleRatea > sampleRateb % Resolution of Xa is greater than that of Xb
t0a ~= t0b % Start times are not equal
t1a ~= t1b % End times are not equal

Objective

Find the necessary shift in indices that will best line up these sets of data.

Approach

Use fmincon to find the index that minimizes the mean squared error (MSE) between versions Xa and Xb that are edited to have the same sample rate (perhaps using the interpolation function).

I have tried to do this but it always seems that I have too many degrees of freedom. Is there anyone who can shed some light on a process that might facilitate this process?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Danny G.
  • 71
  • 1
  • 7
  • isn't it easier to look on the x-correlation \ convolution of the two signals? the maximum value is where they will overlap... – bla Nov 07 '13 at 04:55
  • Is the sampling rate different by an integer factor? If so, then just downsample the one at the higher rate. Cross-correlation will tell you where Xa occurs in Xb (or vice versa.) Are the signals periodic? If so, there is not a unique solution. – nispio Nov 07 '13 at 05:08
  • Have you tried to look at the problem in the Fourier domain? a time shift is just a pahse shift in fourier - dividing the signals should give you the correct shift. – Shai Nov 07 '13 at 06:27
  • @Shai the Fourier domain won't work because the signals have different start and end times. What you are talking about would only work if the two signals were just cyclic shifts of each other. – nispio Nov 07 '13 at 06:58
  • @nispio since the start and end times are different you will have error in the estimation, but if there is a significant overlap - you'll be able to recover the shift and sample rate difference. – Shai Nov 07 '13 at 07:03
  • @Shai I actually think it is an interesting proposal, but I'm having a hard time working out the details in my head. What size DFT would you use? It seems like the frequency bins would be different sizes and different centers, which would make the division useless. Is there a simple way to overcome that problem? – nispio Nov 07 '13 at 07:09
  • @nispio think about it. You only need the low freq info to recover the temporal shift - the high freq will only introduce noise. So you FFT both sequences, pick the `k` low freq entries and recover the shift. I ignored the issue of the different sampling rate here, assuming the sampling rates are known and signals can be interpolated to the same sampling rate. – Shai Nov 07 '13 at 07:19
  • @Shai I see. So the solution still requires that the sampling rates match first. Thanks for clarifying. – nispio Nov 07 '13 at 07:21
  • @nispio I suspect the sampling rate issue can also be addressed using freq domain. I just don't have the time now to do the exact math. Would be a nice exercise though ;) – Shai Nov 07 '13 at 07:33
  • All of the suggested solutions seem unnecessarily complicated. Couldn't you just interpolate the data to use a common time vector/sample rate? – horchler Nov 07 '13 at 15:50
  • @Shai The start times and end times vary sometimes by a few seconds (one technician has to start and stop each sample rate). This means that some of the peaks found in **Xa** are not found in **Xb** or vice versa. Will a Fourier analysis catch these discrepancies? – Danny G. Nov 07 '13 at 16:41
  • @nispio I wrote a message directed at Shai that is also a question for you. Again, thanks for your help. – Danny G. Nov 07 '13 at 16:43
  • @horchler To deal with the sample rate, I will be doing some interpolation or a step down in resolution. Still, the main issue is resolving the correct index shift in which they match the best. – Danny G. Nov 07 '13 at 16:44
  • @natan It sounds like a great idea, but could you give me a bit more detail about the process? Both signals, even when I account for the difference in sample rate, are different sizes, so they cannot be convoluted without re-sizing. – Danny G. Nov 07 '13 at 16:50
  • @DannyG. do you know the two sampling rates? can you interpolate (one or both signals) so that you'll have two signals with the same sample rate (different number of samples, different start/end time)? – Shai Nov 07 '13 at 16:52
  • @DannyG. Convolution will not require that two signals have the same size. – nispio Nov 07 '13 at 16:52
  • @Shai Yes, I know both sampling rates. Sampling up or down will not be too much of an issue. Thanks for asking. – Danny G. Nov 07 '13 at 17:19

2 Answers2

1

Assuming you have two samples with constant frequencies, the problem reduces to something quite simple:

Find scale, location such that:

Xa , at timestamps corresponding to its index, makes the best match with Xb at timstamps corresponding to location + scale * its index.

If you agree with this you can see that only two degrees of freedom are left, if you know the ratio of sample rates it even reduces to just 1 degree of freedom.


I believe that now the hard part is done, but some work still remains:

  1. Judge how good two samples with timestamps and values match
  2. Find the optimal combination of your location and scale parameter

Note that, assuming you complete these 2 steps properly, the solution should be optimal for finding the optimal timestamps. As you are looking for a shift in (integer) indices, translating these timestamps back to indices may not be result in the real optimum but it should be pretty close.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • I'm not sure that I understand how you achieve the "hard part." Are you suggesting iteration over every possible time shift and testing the distance between the two signals? – nispio Nov 07 '13 at 16:58
  • I don't believe these signals have constant frequencies. Think of the signals as measurements of elevation during a trip. I want to move shift one of the signals with respect to the other until they line up. This shift in indices will then correspond to an approximate time shift that I should move the data by so I can analyze these signals, and other accompanying signals (e.g. Ya, Rb, Wa, etc.). I saw that there are two – Danny G. Nov 07 '13 at 17:09
  • These signals don't have constant frequencies. Think of the signals as measurements of elevation. I want to shift one of the signals with respect to the other until they line up. This shift in indices will correspond to an approximate time shift that I should move the data by so I can analyze these signals, and other accompanying signals (e.g. Ya, Rb, Wa, etc.). Once the signals are stepped down or stepped up to get the same sample rates, I still have two degrees of freedom. One is the size of the arrays I will be comparing, and the other is the index shift. – Danny G. Nov 07 '13 at 17:15
  • @Dennis Jaheruddin The comment above is for you. – Danny G. Nov 07 '13 at 17:16
  • @nispio I am attempting to avoid a brute force iteration scheme by using **fmincon**. – Danny G. Nov 07 '13 at 17:18
0

Here is a quick-and-dirty solution that should be enough to get you started. Given your input signals Xa and Xb sampled at sampleRatea and sampleRateb respectively:

g = gcd(sampleRatea,sampleRateb);
Ya = interp(Xa,sampleRateb/g);
Yb = interp(Xb,sampleRatea/g);
Yfs = sampleRatea*sampleRateb/g;

[acor,lag] = xcorr(Ya,Yb);
time_shift = lag(acor == max(acor))/Yfs;

The variable time_shift will tell you the time elapsed between the start of A and the start of B. If B starts first, the result will be negative.

If your sampling rates are relatively prime, this will be horribly inefficient. If one is an integer multiple of the other, or they have a relatively large GCD, it will be much better.

nispio
  • 1,743
  • 11
  • 22