我用Python编程语言开发了双三次插值来演示给一些本科生。
方法如wikipedia所述,
代码运行良好,只是得到的结果与使用scipy库时得到的结果略有不同。
插值代码在下面的函数bicubic_interpolation中显示。import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy import interpolate
import sympy as syp
import pandas as pd
pd.options.display.max_colwidth = 200
%matplotlib inline
def bicubic_interpolation(xi, yi, zi, xnew, ynew):
# check sorting
if np.any(np.diff(xi) < 0) and np.any(np.diff(yi) < 0) and\
np.any(np.diff(xnew) < 0) and np.any(np.diff(ynew) < 0):
raise ValueError('data are not sorted')
if zi.shape != (xi.size, yi.size):
raise ValueError('zi is not set properly use np.meshgrid(xi, yi)')
z = np.zeros((xnew.size, ynew.size))
deltax = xi[1] - xi[0]
deltay = yi[1] - yi[0]
for n, x in enumerate(xnew):
for m, y in enumerate(ynew):
if xi.min() <= x <= xi.max() and yi.min() <= y <= yi.max():
i = np.searchsorted(xi, x) - 1
j = np.searchsorted(yi, y) - 1
x0 = xi[i-1]
x1 = xi[i]
x2 = xi[i+1]
x3 = x1+2*deltax
y0 = yi[j-1]
y1 = yi[j]
y2 = yi[j+1]
y3 = y1+2*deltay
px = (x-x1)/(x2-x1)
py = (y-y1)/(y2-y1)
f00 = zi[i-1, j-1] #row0 col0 >> x0,y0
f01 = zi[i-1, j] #row0 col1 >> x1,y0
f02 = zi[i-1, j+1] #row0 col2 >> x2,y0
f10 = zi[i, j-1] #row1 col0 >> x0,y1
f11 = p00 = zi[i, j] #row1 col1 >> x1,y1
f12 = p01 = zi[i, j&#