我有两个数组
x = [0 9.8312 77.1256 117.9810 99.9979];
y = [0 2.7545 4.0433 5.3763 5.0504];
figure; plot(x, y)
我想制作更多 x
和 y
样本,然后对这两个数组进行插值。我尝试了这段代码
xi =min(x):.1:max(x);
yi = interp1(x,y,xi);
figure; plot(xi, yi)
但轨迹与之前的图不同。这是因为 xi
的波动与 x
不同。我应该如何以与原始数组相同的轨迹对两个数组进行插值?
最佳答案
这是一个问题,因为插值时,MATLAB 将忽略您输入点的顺序,而只是根据它们的 x 位置对它们进行排序。
您可以使用表示线段的累积弧长的参数,并使用该参数来对 x 和 y 坐标进行插值,而不是在 x/y 坐标中进行插值。这将为您提供一个尊重顺序并保证单调性的插值,即使对于同一 x 坐标处的多个值也是如此。
% Compute the distance between all points.
distances = sqrt(diff(x).^2 + diff(y).^2);
% Compute the cumulative arclength
t = cumsum([0 distances]);
% Determine the arclengths to interpolate at
tt = linspace(t(1), t(end), 1000);
% Now interpolate x and y at these locations
xi = interp1(t, x, tt);
yi = interp1(t, y, tt);
关于matlab - 如何在matlab中对波动向量进行插值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41363651/