日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python2d 平滑插值处理_python中平滑的、通用的2D线性插值

發(fā)布時間:2024/7/5 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python2d 平滑插值处理_python中平滑的、通用的2D线性插值 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我已經(jīng)設(shè)法寫了一個符合我的目的的函數(shù)。它通過沿網(wǎng)格線插值,然后在x和y方向插值平面,并取兩者的平均值,從坐標(biāo)網(wǎng)格中插值(填充)平面。在

通過將坐標(biāo)重塑為一維矢量,一次性插值平面,然后再重新塑造為二維,應(yīng)該可以稍微加快這一速度。但是,對于合理的平面尺寸來說,這個代碼已經(jīng)足夠快了。在

如果坐標(biāo)也在平面外,似乎也可以工作。

如果網(wǎng)格近似規(guī)則,則外推法也有效。不管怎樣,它都會外推,但是隨著柵格不規(guī)則度的增加,你會看到一些尖銳的折痕遠(yuǎn)離邊緣。在

這是密碼。docstring中提供了一個示例。在def interlin2d(x,y,z,fsize):

"""

Linear 2D interpolation of a plane from arbitrary gridded points.

:param x: 2D array of x coordinates

:param y: 2D array of y coordinates

:param z: 2D array of z coordinates

:param fsize: Tuple of x and y dimensions of plane to be interpolated.

:return: 2D array with interpolated plane.

This function works by interpolating lines along the grid point in both dimensions,

then interpolating the plane area in both the x and y directions, and taking the

average of the two. Result looks like a series of approximately curvilinear quadrilaterals.

Note, the structure of the x,y,z coordinate arrays are such that the index of the coordinates

indicates the relative physical position of the point with respect to the plane to be interpoalted.

Plane is allowed to be a subset of the range of grid coordinates provided.

Extrapolation is accounted for, however sharp creases will start to appear

in the extrapolated region as the grid of coordinates becomes increasingly irregular.

Scipy's interpolation function is used for the grid lines as it allows for proper linear extrapolation.

However Numpy's interpolation function is used for the plane itself as it is robust against gridlines

that overlap (divide by zero distance).

Example:

#set up number of grid lines and size of field to interpolate

nlines=[3,3]

fsize=(100,100,100)

#initialize the coordinate arrays

x=np.empty((nlines[0],nlines[1]))

y=np.empty((nlines[0],nlines[1]))

z=np.random.uniform(0.25*fsize[2],0.75*fsize[2],(nlines[0],nlines[1]))

#set random ordered locations for the interior points

spacings=(fsize[0]/(nlines[0]-2),fsize[1]/(nlines[1]-2))

for k in range(0, nlines[0]):

for l in range(0, nlines[1]):

x[k, l] = round(random.uniform(0, 1) * (spacings[0] - 1) + spacings[0] * (k - 1) + 1)

y[k, l] = round(random.uniform(0, 1) * (spacings[1] - 1) + spacings[1] * (l - 1) + 1)

#fix the edge points to the edge

x[0, :] = 0

x[-1, :] = fsize[1]-1

y[:, 0] = 0

y[:, -1] = fsize[0]-1

field = interlin2d(x,y,z,fsize)

"""

from scipy.interpolate import interp1d

import numpy as np

#number of lines in grid in x and y directions

nsegx=x.shape[0]

nsegy=x.shape[1]

#lines along the grid points to be interpolated, x and y directions

#0 indicates own axis, 1 is height (z axis)

intlinesx=np.empty((2,nsegy,fsize[0]))

intlinesy=np.empty((2,nsegx,fsize[1]))

#account for the first and last points being fixed to the edges

intlinesx[0,0,:]=0

intlinesx[0,-1,:]=fsize[1]-1

intlinesy[0,0,:]=0

intlinesy[0,-1,:]=fsize[0]-1

#temp fields for interpolation in x and y directions

tempx=np.empty((fsize[0],fsize[1]))

tempy=np.empty((fsize[0],fsize[1]))

#interpolate grid lines in the x direction

for k in range(nsegy):

interp = interp1d(x[:,k], y[:,k], kind='linear', copy=False, fill_value='extrapolate')

intlinesx[0,k,:] = np.round(interp(range(fsize[0])))

interp = interp1d(x[:, k], z[:, k], kind='linear', copy=False, fill_value='extrapolate')

intlinesx[1, k, :] = interp(range(fsize[0]))

intlinesx[0,:,:].sort(0)

# interpolate grid lines in the y direction

for k in range(nsegx):

interp = interp1d(y[k, :], x[k, :], kind='linear', copy=False, fill_value='extrapolate')

intlinesy[0, k, :] = np.round(interp(range(fsize[1])))

interp = interp1d(y[k, :], z[k, :], kind='linear', copy=False, fill_value='extrapolate')

intlinesy[1, k, :] = interp(range(fsize[1]))

intlinesy[0,:,:].sort(0)

#interpolate plane in x direction

for k in range(fsize[1]):

tempx[k, :] = np.interp(range(fsize[1]),intlinesx[0,:,k], intlinesx[1,:,k])

#interpolate plane in y direction

for k in range(fsize[1]):

tempy[:, k] = np.interp(range(fsize[0]), intlinesy[0, :, k], intlinesy[1, :, k])

return (tempx+tempy)/2

總結(jié)

以上是生活随笔為你收集整理的python2d 平滑插值处理_python中平滑的、通用的2D线性插值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。