Shifting table entries based on criteria

7 views (last 30 days)
Hi, I've realized the tables in MAtlab is very powerful, but I've just started using it and so far it isn't very intuitive. This may be a simple question, but it's not obvious to me. Hope someone will help. I have two tables, T1 and T2. They each have a time variable, T1.time and T2.time. And each has a Y variable, T1.Y and T2.Y.
When the Y variables are plotted in a scatter plot, they look like this:
2019-04-04_7-54-57.jpg
Notice that T1.Y (green) is shifted by some number of time units to the left of T2.Y (red). I would like to find the number of time units and shift T1.Y to the right, so they are alinged, and then I can compute a correlation coefficient.
My thought is to find the maximum of T1.Y(i+3) - T1.Y(i) and T2.Y(i+3) - T2.Y(i) and return these indices. One might be at i = 105 (green) and the other might be i = 140 (red) for example. So the difference is 35 time segments. I can then add 35 time segments to the T1.time, and if replotted they should be aligned. And then I can compute the correlation?
First question, does this 'algorithm' make sense for alignment? And secondly, using the table variables, any suggestion how I might find the shift required and the index for for the shift (i.e. at what index value does the green plot have a large change, at which index value does the red plot have a large change). Does this make sense? I'm using R2018b.

Accepted Answer

Peter Perkins
Peter Perkins on 8 Apr 2019
This is in general a complicated question. You might have noisy data. You might have two different sample rates. It looks like maybe you have the first but not the second?
Then there's the question of how to align the two sequences. One thing to do is to find the maimum correlation over a range of shifts. But it sounds like you want to compute the correlation after you align them. So I guess you'd be trying to match up some feature in the data? I don't understand the thing with i+3, but I'm no expert at signal processing. I'm guessing that if you have access to the Signal Processing Toolbox, you'd find various tools in there.
In any case, it also sounded like you were having trouble figuring out how to use tables. If you tables have two numeric vars in them, then for the purpises of computing the optimal shift, you can treat T1.Y and T2.Y just as you would any other vector in MATLAB.
  1 Comment
Harold Singh
Harold Singh on 10 Apr 2019
Thank you for your response. I have same sample rates, and I'm assuming 'equally noisy' data.
Yes, I'd like to find the correlation after they're aligned on a particular feature. In this case, the feature is the largest change between the i and i+3 data points. If I know where this feature is (i.e. what the index i is) for both data sets, I can then compute the shift necessary, and use circshift to shift one data set. It seems to work when I 'eyeball' the amount of shift needed and use that in circshift:
flow.Time = circshift(flow.Time,-51);
for a shift of 51 samples. The question is how best to determine 51 programmatically, without having to eyeball the data. Any help appreciated.

Sign in to comment.

More Answers (0)

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!