Time difference between two curves
5 views (last 30 days)
Show older comments
Hi !
I have two curves, one is a ramp, the second is more like many steps. The two curves don't start at the same time, but finish at the same time. What I'm asking is how can I have the time difference when curveA.value = curveB.value (the two curves are exporting with a From Workspace block). I don't know how to do because the curves don't have the same value at the same time.
Thanks !
2 Comments
Walter Roberson
on 11 Apr 2011
Are you looking for the first possible match, or all the possible matches, or ??
Accepted Answer
Arnaud Miege
on 11 Apr 2011
If you are using Simulink, which it sounds as if you are, then you can feed both signals to a Relational Operator block, and feed the output of that block to a Stop Simulation block. The stop time will then tell you when one signal becomes greater than (or smaller than) the other one.
HTH,
Arnaud
0 Comments
More Answers (4)
Sébastien Malengé
on 11 Apr 2011
1 Comment
Arnaud Miege
on 11 Apr 2011
No, it's not clear at all... Are you using Simulink? If so, both curves will have the same timestamp, you should therefore be able to compute the time difference between the two.
Sébastien Malengé
on 11 Apr 2011
1 Comment
Arnaud Miege
on 11 Apr 2011
I think in Simulink, you can only compare two signals at the same point in time. What you can do is compare each curve independently to the same constant value using the relational operator block as I suggested. You can then look at these two signals and when they change from 0 to 1 is when the curve in question is greater than/smaller than the constant value. The time difference between these two trigger signals is what you're after. If you want to do this in Simulink, you'll probably need to use either an Embedded MATLAB Function block (now simply called a "MATLAB Function" block in R2011a) or a Stateflow chart to work out the logic to extract the time difference from the two trigger signals, it's not straightforward.
Walter Roberson
on 11 Apr 2011
If you are asking for all possible matches, then potentially each sample of curveA could match against each sample of curveB at all time instances in curveB. You would then need an output matrix which was length(curveA) by length(curveB), unless you use the very new Simulink dynamic array sizes. If you are going to use the array of maximum size, you might as well make it a boolean array with a 1 for each location in which the values match. You can do that in a fairly simple matlab statement,
bsxfun(@eq, curveA.', curveB)
If these are floating point values, don't be surprised if you get no matches: you may wish to match to a tolerance rather than matching exactly.
0 Comments
Teja Muppirala
on 11 Apr 2011
If all you really want to know is what the difference between the curves is, then one way would be to use kind of a reverse look-up table.
t = linspace(0,10,1001);
y1 = 1+t;
y2 = (t-1.7) .* (t >= 1.7);
y2 = round(2*y2)/2;
plot(t,y1,t,y2); %My two curves
gap = interp1(y1,t,y2) - t;
figure
plot(y2,gap);
title('The gap between the two curves as a function of y2');
Clearly the gap is about -1.7.
2 Comments
Walter Roberson
on 11 Apr 2011
If that kind of number is what was being sought, then xcorr or equivalent should give you the "best" time match. However, the original poster wants _all_ of the time matches. (Not sure what the original poster wants to do about the list of times corresponding to the horizontal part of the steps....)
See Also
Categories
Find more on Sources in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!