Matlab中的双重插值

3
我想问一下Matlab中插值的问题。我想知道是否可能在同一行中进行两种不同的插值。我的意思是,例如,在开始时进行线性插值,在中间的位置,大约需要另一种插值方法,如样条插值。
主要问题是,我已经进行了线性插值,并且在开始时效果很好,但是在某些点之后,我认为使用另一种类型的插值会更好。如果这种做法可行,那么我该如何编写代码来实现修改?我尝试查看了关于Matlab的文档,但没有找到有关修改插值的任何信息。
提前致谢,问候。

1
给定一个数组 x,你难道不能只是这样做吗? y(1:threshold)=interpA(1:threshold); y(1+threshold:end)=interpB(1+threshold:end); ?? - learnvst
哎呀,这里有一个小错误。请参见下面的完整答案。 - learnvst
1个回答

3

让我详细说明一下我在你的帖子上发表的评论。

如果您想使用2个不同的函数和一个拆分从输入数组创建输出数组,可以使用类似以下代码示例中的数组索引范围。

x = randn(20,1); %//your input data - 20 random numbers for demonstration
threshold = 5; %//index where you want the change of algorithm
y = zeros(size(x)); %//output array of zeros the same size as input

y(1:threshold)     = fun1(x(1:threshold));
y(1+threshold:end) = fun2(x(1+threshold:end));

如果您愿意,可以跳过对 y 的预分配,并将附加数据直接连接到输出的末尾。如果函数返回的输出元素数量与输入元素数量不同,这将特别有用。以下是其语法示例。
y = fun1(x(1:threshold));
y = [y; fun2(x(1+threshold:end))];

编辑:

回应下面的帖子,这里有一个完整的例子...


(该文涉及IT技术)
clc; close all

x = -5:5; %//your x-range
y = [1 1 0 -1 -1 0 1 1 1 1 1]; %//the function to interpolate
t = -5:.01:5; %//sampling interval for output

xIdx = 5; %//the index on the x-axis where you want the split to occur
tIdx = floor(numel(t)/numel(x)*xIdx);%//need to calculate as it is at a different sample rate

out = pchip(x(1:xIdx),y(1:xIdx),t(1:tIdx));
out = [out spline(x((1+xIdx):end),y((1+xIdx):end),t((1+tIdx):end))];

%//PLOTTING
plot(x,y,'o',t,out,'-',[x(xIdx) x(xIdx)], [-1.5 1.5], '-')
legend('data','output','split',4);
ylim ([-1.5 1.5])

Which will give . . .

enter image description here


嗯,我已经检查过了,问题是我不知道如何准确地编写插值函数的类型(比如'spline'、'linear'...),但我正在尝试寻找和测试它!:) 再次感谢! - user1578688
嗨,learnvst!我不知道发生了什么,但我做不到。我已经尝试了以下代码:code x = randn(20,1); %//your input data - 20 random numbers for demonstration threshold = 5; %//index where you want the change of algorithm y = zeros(size(x)); %//output array of zeros the same size as input y(1:threshold)= pchip(x(1:threshold)); y(1+threshold:end)=spline(x(1+threshold:end));code - user1578688

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接