MATLAB Answers

Calculate shifts/translation between two curves

14 views (last 30 days)
Angelavtc
Angelavtc on 21 Oct 2020
Commented: Star Strider on 23 Oct 2020
Dear all,
Is there a way to determine if the blue curve has shifted from the purple curve? (see graph). As you can see, before the black line the blue curve shifts to the right of the purple one and after the black line the curve shifts to the left. Is there a way to do an analysis that tells us this? Maybe a separate analysis of regions that tells us the shift of the curve? I have seen that the xcorr command is used to measure displacements, but in a generalized way. When I apply it to this case, the maximum lags are zero.
Thanks in advance!

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 21 Oct 2020
The blue curve is not ‘shifting’. It has a different scale with respect to the independent variable than the purple curve. The correct way to characterise it would be to compare the characteristics of the two curves.
For example —
x = linspace(-100, 100);
yb = 1./(1+exp(-0.1*x));
yp = 1./(1+exp(-0.05*x));
ratio = mean(gradient(yb)./gradient(x)) / mean(gradient(yp)./gradient(x));
figure
plot(x, yb, '-b')
hold on
plot(x, yp, '-m')
hold off
grid
Here, the ‘ratio’ metric compares the means of the numeric derivatives (calculated by the the gradient function) to characterise them. I am certain there are more appropriate metrics for your curves, however something like that would likely be more appropriate than measuring a ‘shift’ that is actually not a shift.

  5 Comments

Show 2 older comments
Star Strider
Star Strider on 21 Oct 2020
This is a different curve from the one you originally posted. This one is shifted. The finddelay function (Signal Processing Toolbox) might do what you want.
The first set of curves (in your original post) are not shifted, and the example I posted illustrates that.
Angelavtc
Angelavtc on 23 Oct 2020
@Star Strider, thank you for your answer! Do you have any idea why when I use the finddelay function in the attached data, the answer is 0? Even when there is a clear shift to the right on my data? Both codes give me zero :(
finddelay(x1,x2)
finddelay(y1,y2)
Star Strider
Star Strider on 23 Oct 2020
As always, my pleasure!
The problem is that there does not appear to be much difference between the signals, at least with respect to the indices. The finddelay funciton assumes that one signal is zero-padded with respect to the other, and the related functions also assume a common independent variable vector. Also, the independent variable vectors are not only not common, they also have different sampling intervals and ranges.
That is most evident with:
[S1,L1] = bounds(x1);
[S2,L2] = bounds(x2);
range_x1 = L1-S1
range_x2 = L2-S2
producing:
range_x1 =
9666
range_x2 =
16096
even though they have the same number of elements(30000 each).
so likely the best you can hope for is:
delay = min(x2)-min(x1)
producing:
delay =
3257
assuming that the units of the independent variables are the same (for example, seconds).
One function you can experiment with is dtw that measures the distance between signals using ‘dynamic time warping’:
[disty,ix,iy] = dtw(y1,y2);
figure
dtw(y1,y2)
I defer to you to interpret the results, sdince I do not know what your signals are.
.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!