How to calculate the response time of an instrument based on a second?

2 views (last 30 days)
Kofial
Kofial on 13 Oct 2021
Answered: Mathieu NOE on 13 Oct 2021
% The second instrument has the correct values. I would like to know how delayed (in seconds or minutes) is the first one?
% Here is the data in which the data is collected
Time_datenum={'20-Apr-2020 11:06:00','20-Apr-2020 11:20:10','20-Apr-2020 11:45:30','20-Apr-2020 12:07:00','20-Apr-2020 12:35:40','20-Apr-2020 12:40:50','20-Apr-2020 13:07:00'};
Time_datetime = datetime(Time_datenum,'InputFormat','dd-MM-yyyy HH:mm:ss');
% The values recevied from both intsruments
First_Instrument_Amplitude=[1.5 2.3 2.5 4 6.2 7.1 8];
Second_Instrument_Amplitude=[2 2.4 2.7 3.9 6.2 7.3 9];
plot(Time_datenum,First_Instrument_Amplitude);
hold on
plot(Time_datenum,First_Instrument_Amplitude);
plot(Time_datenum,Second_Instrument_Amplitude,"r");
hold off
S1 = stepinfo(First_Instrument_Amplitude,Time_datenum);
% I tried to use stepinfo command following this matlab document but it doesn't work
% https://nl.mathworks.com/help/control/ref/lti.stepinfo.html#d123e150093
% I would like to have a graph like this one.

Accepted Answer

Mathieu NOE
Mathieu NOE on 13 Oct 2021
hello Kofial
I did a few mods in your code ... but the response data of the instruments has little to do with the graph of the damped 2nd order system
they look more like an almost straight line , with non constant delay (vs time)
I compute the average time difference at the crossing point at several common amplitude levels (threshold values)
and I get :
time_difference_seconds = 130.0312 (average value , in seconds)
plot
code
% The second instrument has the correct values. I would like to know how delayed (in seconds or minutes) is the first one?
% Here is the data in which the data is collected
Time_datenum={'20-Apr-2020 11:06:00','20-Apr-2020 11:20:10','20-Apr-2020 11:45:30','20-Apr-2020 12:07:00','20-Apr-2020 12:35:40','20-Apr-2020 12:40:50','20-Apr-2020 13:07:00'};
Time_datetime = datetime(Time_datenum);
ts=seconds(Time_datetime-Time_datetime(1));
% The values recevied from both intsruments
First_Instrument_Amplitude=[1.5 2.3 2.5 4 6.2 7.1 8];
Second_Instrument_Amplitude=[2 2.4 2.7 3.9 6.2 7.3 9];
threshold = max(First_Instrument_Amplitude(1),Second_Instrument_Amplitude(1))+0.1:0.1:min(First_Instrument_Amplitude(end),Second_Instrument_Amplitude(end))-0.1;
for ci = 1:numel(threshold)
[t1_pos(ci),~,~,~]= crossing_V7(First_Instrument_Amplitude,ts,threshold(ci),'linear'); % positive (pos) and negative (neg) slope crossing points
[t2_pos(ci),~,~,~]= crossing_V7(Second_Instrument_Amplitude,ts,threshold(ci),'linear'); % positive (pos) and negative (neg) slope crossing points
% ind => time index (samples)
% t0 => corresponding time (x) values
% s0 => corresponding function (y) values , obviously they must be equal to "threshold"
end
figure(1)
plot(ts,First_Instrument_Amplitude,'b',ts,Second_Instrument_Amplitude,'r');
hold on
plot(t1_pos,threshold,'+r',t2_pos,threshold,'+g','linewidth',2,'markersize',12);grid on
legend('First Instrument','Second Instrument','First Instrument crossing points','Second Instrument crossing points');
xlabel('seconds');
% finally ...
time_difference_seconds = abs(mean(t2_pos - t1_pos)); % in seconds
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [t0_pos,s0_pos,t0_neg,s0_neg] = crossing_V7(S,t,level,imeth)
% [ind,t0,s0,t0close,s0close] = crossing_V6(S,t,level,imeth,slope_sign) % older format
% CROSSING find the crossings of a given level of a signal
% ind = CROSSING(S) returns an index vector ind, the signal
% S crosses zero at ind or at between ind and ind+1
% [ind,t0] = CROSSING(S,t) additionally returns a time
% vector t0 of the zero crossings of the signal S. The crossing
% times are linearly interpolated between the given times t
% [ind,t0] = CROSSING(S,t,level) returns the crossings of the
% given level instead of the zero crossings
% ind = CROSSING(S,[],level) as above but without time interpolation
% [ind,t0] = CROSSING(S,t,level,par) allows additional parameters
% par = {'none'|'linear'}.
% With interpolation turned off (par = 'none') this function always
% returns the value left of the zero (the data point thats nearest
% to the zero AND smaller than the zero crossing).
%
% check the number of input arguments
error(nargchk(1,4,nargin));
% check the time vector input for consistency
if nargin < 2 | isempty(t)
% if no time vector is given, use the index vector as time
t = 1:length(S);
elseif length(t) ~= length(S)
% if S and t are not of the same length, throw an error
error('t and S must be of identical length!');
end
% check the level input
if nargin < 3
% set standard value 0, if level is not given
level = 0;
end
% check interpolation method input
if nargin < 4
imeth = 'linear';
end
% make row vectors
t = t(:)';
S = S(:)';
% always search for zeros. So if we want the crossing of
% any other threshold value "level", we subtract it from
% the values and search for zeros.
S = S - level;
% first look for exact zeros
ind0 = find( S == 0 );
% then look for zero crossings between data points
S1 = S(1:end-1) .* S(2:end);
ind1 = find( S1 < 0 );
% bring exact zeros and "in-between" zeros together
ind = sort([ind0 ind1]);
% and pick the associated time values
t0 = t(ind);
s0 = S(ind);
if ~isempty(ind)
if strcmp(imeth,'linear')
% linear interpolation of crossing
for ii=1:length(t0)
%if abs(S(ind(ii))) >= eps(S(ind(ii))) % MATLAB V7 et +
if abs(S(ind(ii))) >= eps*abs(S(ind(ii))) % MATLAB V6 et - EPS * ABS(X)
% interpolate only when data point is not already zero
NUM = (t(ind(ii)+1) - t(ind(ii)));
DEN = (S(ind(ii)+1) - S(ind(ii)));
slope = NUM / DEN;
slope_sign(ii) = sign(slope);
t0(ii) = t0(ii) - S(ind(ii)) * slope;
s0(ii) = level;
end
end
end
% extract the positive slope crossing points
ind_pos = find(sign(slope_sign)>0);
t0_pos = t0(ind_pos);
s0_pos = s0(ind_pos);
% extract the negative slope crossing points
ind_neg = find(sign(slope_sign)<0);
t0_neg = t0(ind_neg);
s0_neg = s0(ind_neg);
else
% empty output
ind_pos = [];
t0_pos = [];
s0_pos = [];
% extract the negative slope crossing points
ind_neg = [];
t0_neg = [];
s0_neg = [];
end
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by