插值函數 interp1 interp2

1、一維插值 interp1

clc;
clear;
close all;

x0=0:1:2*pi;
y0=sin(x0);

x=0:0.5:2*pi;

y = interp1(x0,y0,x,'spline');

figure(1);
plot(x0,y0);hold on;plot(x,y);
grid on;
xlabel('x0');ylabel('y0');title("һά²喵");legend('y0','y');

在這裏插入圖片描述

2、二維插值 interp2

   搜索域:

   橫座標範圍col (-5) ~ (+5)
   縱座標範圍row (-3) ~ (+3)

   值域:

   與上述搜索範圍一一對應

   搜索座標:

   ( x, y)

   返回值:

   搜索座標在搜索域內取得的值

clc;
clear;
close all;

col = -5:5;
row = -3:3;
[map_x, map_y] = meshgrid(col,row);

for i=1:length(row)
    for j=1:length(col)
        region(i,j) = ((i-1)*length(col)+j)*10;
    end
end

region

%% x:(-5 5)   y:(-3 3)
x=2.4;
y=2.5;
if x>5
    x=5;
elseif x<-5
    x=-5;
end

if y>3
    y=3;
elseif y<-3
    y=-3;
end
    
value = interp2(map_x, map_y, region, x, y);

fprintf("search (%d, %d) : %d\n", x, y, value);


simulink中使用的插值模塊: Lookup Tables

發佈了32 篇原創文章 · 獲贊 8 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章