Reputation: 3
I have a timeseries that outputs data (time
vs theta
) from a Simulink model into the MATLAB workspace. I want to automatically interpolate this data so I can find what time (t1
) it would be when theta == 45
.
I then need to look up t1
in another time series (time
vs velocity
) and then output what the velocity would be at that time.
How can I do this? Is there a better way to approach this problem?
EDIT: I can interpolate the first timeseries with a new time vector and finer resolution so it has theta == 45
exactly, but my method is pretty brute force and requires manually looking at the time series first to determine what times I need to interpolate between (else it would take forever) which I'd like to avoid, e.g:
theta2 = resample(theta, 1.68:0.0000001:1.685)
Upvotes: 0
Views: 315
Reputation: 5251
I assume that theta
and velocity
are timeseries objects.
You can search the time interval [time_start, time_finish]
as follows. However, this assumes that you know that there is a sign change in this interval.
t1 = fzero(@(t) theta.resample(t).Data - 45, [time_start, time_finish]);
Then, given t1
, you can interpolate the velocity directly.
vel = velocity.resample(t1).Data;
Upvotes: 1
Reputation: 13886
You can use this function from the File Exchange to find the intersection of your timeseries and a straight line at theta = 45
:
% assume t and theta exist in the base workspace
theta_45 = 45*ones(size(theta));
[t1,theta1] = intersections(t,theta,t,theta_45); % t1 and theta1 are column vectors of the intersection points
Then take say the first element of t1
and look it up in your second time vector t2
(I assume t2
is monotonically increasing so that should be straightforward) and find the corresponding index idx2
. velocity(idx2)
is your desired value.
Upvotes: 0