2017-03-09 72 views
1

我試圖插入列表中的角度。角度插值

Dir DirOffset 
0 109.6085 
30 77.5099 
60 30.5287 
90 -10.2748 
120 -75.359 
150 -147.6015 
180 -162.7055 
210 21.0103 
240 3.5502 
270 -11.5475 
300 -39.8371 
330 -109.5473 
360 109.6085 

enter image description here

我所編寫的代碼來內插角度(它使上計算平均角度之間達到插補值),其是需要長時間。如果有人擁有更快更短的代碼,請幫助我。

enter image description here

from cmath import rect, phase 
from math import radians, degrees, sqrt 

#Calculate the mean of angles in List 
def mean_angle(degArray): 
    return degrees(phase(sum(rect(1, radians(d)) for d in degArray)/len(degArray))) 

#Calculate Interpolation Angle 
def Interpolate_angle(Dir, DirOffset, ValuetoInterpolate): 
    #Create Lower and Higher bin of ValuetoInterpolate 
    DirLBin = round(float(ValuetoInterpolate)/30,0)*30 
    DirHBin = round(float(ValuetoInterpolate+15)/30,0)*30 

    #Check if the ValuetoInterpolate lies between Lower and Higher bin 
    if DirLBin == DirHBin: 
     DirLBin = DirHBin-30 
     if DirLBin <= ValuetoInterpolate <= DirHBin: 
      DBin = [float(DirLBin), float(DirHBin)] 
      Doff = [DirOffset[Dir.index(DirLBin)], DirOffset[Dir.index(DirHBin)]] 
     else: 
      DirHBin = DirLBin+30 
      DBin = [float(DirLBin), float(DirHBin)] 
      Doff = [DirOffset[Dir.index(DirLBin)], DirOffset[Dir.index(DirHBin)]] 

    else: 
     DBin = [float(DirLBin), float(DirHBin)] 
     Doff = [DirOffset[Dir.index(DirLBin)], DirOffset[Dir.index(DirHBin)]] 

    #Run 50 iterations to calculate the mean of angle and find the ValuetoInterpolate  
    for i in range(51): 
     DMean = mean_angle(DBin) 
     DOMean = mean_angle(Doff) 
     if DMean < 0 : 
      DMean = 360+DMean 

     if DBin[0] <= ValuetoInterpolate <=DMean: 
      DBin = [float(DBin[0]), float(DMean)] 
      Doff = [float(Doff[0]), float(DOMean)] 
     else: 
      DBin = [float(DMean), float(DBin[1])] 
      Doff = [float(DOMean), float(Doff[1])] 

    return DOMean 

Dir = range(0,370,30) 
DirOffset = [109.6085,77.5099,30.5287,-10.2748,-75.359,-147.6015,-162.7055,21.0103,3.5502,-11.5475,-39.8371,-109.5473,109.6085] 
ValuetoInterpolate = 194.4 
print Interpolate_angle(Dir, DirOffset, ValuetoInterpolate) 

回答

2

我得到了上述問題的解決方案,從計算器搜索答案後,然後我修改點點得到解決按我的要求。該解決方案可能對需要它的人有用。

我內插使用下面的功能對於每個定向容器(0,30,60 .... 360),直到360(360和0度將是相同的),並將它們存儲在字典創建一個數據幀(數據幀大熊貓的度)並將其與主DataFrame連接並進一步處理。

enter image description here

DEF InterpolateDegrees(109.6085,77.5099)

將返回DirectionOffset 0的插陣列至30度,(0.0,0.1,0.2,0.3 ...... 28.7 0.1的間隔,29.8,29.9)

import numpy as np 
from math import fabs 

def InterpolateDegrees(start, end, BinSector=12): 
    BinAngle = 360/BinSector 
    amount = np.arange(0,1,(1/(BinAngle/0.1))) 
    dif = fabs(end-start) 
    if dif >180: 
     if end>start: 
      start+=360 
     else: 
      end+=360 

    #Interpolate it 
    value = (start + ((end-start)*amount)) 

    #Wrap it 
    rzero = 360 

    Arr = np.where((value>=0) & (value<=360), (value), (value % rzero)) 
    return Arr