用Pandas插值极/圆数据

2024-09-30 20:32:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个稀疏的数据集,f44,由度数方向和ssi(dbm)值组成:

       ssi
deg       
4.0    -69
59.0   -73
162.0  -73
267.0  -61
319.0  -75

我重新索引了f44,以包含0-359中所有缺失的索引:

^{pr2}$

当我插值(使用二次曲线)并绘制图形时,很明显,结果不会在第一个/最低方位和最高方位之间插值:

f44i = f44i.interpolate(method='quadratic')
f44i.plot()

naive interpolation

如何以0到360度的方式插值此数据?^{}文档似乎没有任何内置内容,^{}文档也没有。在


Tags: 数据文档图形绘制方向插值dbm度数
2条回答

我有另一种方法来解决这个问题,使用scipy在闭合曲线上插值。首先,我将数据从(deg,ssi)转换为psuedo笛卡尔坐标(x,y),假设deg是极角,ssi是径向距离的(负)。然后可以使用定义的方法here将闭合曲线插值到一组(x,y)点上。在

import numpy as np
import pandas
import matplotlib.pyplot as plt
from scipy import interpolate

dic = {'deg': [4.0, 59.0, 162.0, 267.0, 319.0],
       'ssi': [-69, -73, -73, -61, -75]}

f44  = pandas.DataFrame(data=dic)

'''
Now, lets do the following. Convert your data from (deg, ssi) to (x,y)
where x = ssi* cosd(deg), y=ssi*sind(deg). Now we need to interpolate a closed
curve onto these set of cartesian points.  
'''

f44['x'] = -f44['ssi']*np.cos( np.deg2rad(f44['deg']))
f44['y'] = -f44['ssi']*np.sin( np.deg2rad(f44['deg']))

x = f44.as_matrix(columns=[f44.columns[2]])[:,0]
y = f44.as_matrix(columns=[f44.columns[3]])[:,0]
x = np.r_[x, x[0]]
y = np.r_[y, y[0]]

tck, u = interpolate.splprep([x, y], s=0, per=True)
xi, yi = interpolate.splev(np.linspace(0, 1, 1000), tck)

# Save interpolated data to new dataframe. 
f44i = pandas.DataFrame(data = {'x':xi, 'y':yi})

f44i['deg'] = np.rad2deg( np.arctan2(f44i['y'],f44i['x']))
f44i['ssi'] =-np.sqrt( f44i['x']**2 + f44i['y']**2)

for i,l in enumerate(f44i['deg']):
    if l < 0:
        f44i['deg'][i] = 360 + f44i['deg'][i]

fig, ax = plt.subplots(1, 1)
ax.plot(f44i['deg'], f44i['ssi'], '.', markersize=0.5)
ax.plot(f44['deg'], f44['ssi'], '.', color='red')
plt.show()

现在我们得到一条曲线,看起来像下面的曲线。(从psuedo笛卡尔坐标重新转换为首选(deg,ssi)坐标)。另外,我还创建了一个新的数据帧来替换您创建的f44i。您可以使此代码更适合您的特定应用程序。在

Interpolation using a closed curve.

我不相信这是最好的办法,但它似乎有效。如果有更好的方法,请添加一些内容。在

由于我只对0-360度感兴趣,我可以从-360-0和360-720复制数据集,将原始数据集向左和向右展开,如下所示:

import numpy as  np    

# Create left side
f44il = f44i.copy()
f44il.index = np.arange(-360,0)

# Create right side
f44ir = f44i.copy()
f44ir.index = np.arange(360,720)

插值和绘制结果看起来很有希望(第三个命令以不同的颜色显示0-360):

^{pr2}$

repeating data

然后,我可以从插值数据中创建一个新的序列,索引范围为0-360,这看起来正好满足我的需要:

f44final = f44expanded[np.arange(0,360)]
f44final.plot()

final plot

我怀疑有更好的方法可以做到这一点,所以如果你知道答案的话,可以随意添加一个答案。在

相关问题 更多 >