如何在Matlab中对波动矢量进行插值? [英] how to interpolate a fluctuated vector in matlab?
问题描述
我有两个数组
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
进行更多采样,然后对两个数组进行插值.我尝试了这段代码
I want to make more samples of x
and y
then I interpolated both arrays. I tried this code
xi =min(x):.1:max(x);
yi = interp1(x,y,xi);
figure; plot(xi, yi)
,但轨迹与先前的图不相同.这是因为xi
的波动与x
相同.我应该如何用与原始阵列相同的轨迹对两个阵列进行插值?
but the trajectory is not same as previous plot. Its because the xi
is not fluctuating same as x
. How should I interpolate both arrays with same trajectory as original one?
推荐答案
这是一个问题,因为在插值时,MATLAB将忽略输入点的 order ,而是对其进行排序根据他们的x位置.
This is an issue because when interpolating, MATLAB is going to ignore the order that you feed in the points and instead just sort them based upon their x location.
您可以使用表示线段的累积弧长的参数,而不是在x/y坐标上进行插值,并使用 that 来对x和y坐标进行插值.这将为您提供一个插值法,该插值法应遵守顺序并即使在同一x坐标处的多个值也能保证单调性.
Rather than interpolating in x/y coordinates, you can instead use a parameter which represents the cumulative arc length of the line segments and use that to interpolate both the x and y coordinates. This will provide you with an interpolant that respects the order and guarantees monotonicity even for multiple values at the same x coordinate.
% 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中对波动矢量进行插值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!