除了@HansHirse 的回答之外,插入数据以找到阈值交叉点可能很有用。
例如,如果您的数据如下所示:
x = [ 1 2 3 4];
y = [47 49 51 53];
y 不包含确切的阈值 (50),因此我们可以对这些数据进行插值,以猜测根据x,我们将到达y = 50。
x_interp = [ 1 2 2.5 3 4];
y_interp = [47 49 50 51 53];
没有插值的交叉点:
% Dummy data
x = 0:0.2:5*pi;
y = sin(x)*10;
% Threshold
T = 5;
% Crossing points
ind = find(abs(diff(sign(y-T)))==2)+1
xind = x(ind)
yind = y(ind)
% Plot
plot(x,y);
hold on
plot(xind,yind,'o','markersize',2,'color','r')
插值交叉点:
% Dummy data
x = 0:0.2:5*pi;
y = sin(x)*10;
% Threshold
T = 5;
%% Crossing points interpolation
% Index where intersection occurs
ind = [find(abs(diff(sign(y-T)))==2)+1].'+[-1,0]
% For example we could obtain:
% [5; [4, 5; %We cross the threshold between y(4) and y(5)
% ind = 10; + [-1,0] = 9,10; %We cross the threshold between y(9) and y(10)
% 18] 17,18] %...
xind = x(ind)
yind = y(ind)-T
% Linear interpolation
xint = xind(:,1)-yind(:,1)./(diff(yind,1,2)./diff(xind,1,2))
yint = T
% Plot
plot(x,y);
hold on
plot(xint,yint,'o','markersize',2,'color','r')
然后只需将这些新的插值添加到您的原始向量中:
[x,pos] = sort([x xint]);
y = [y yint];
y = y(pos);
% Now apply the @HansHirse's solution.
% ...