如何修复"list assignment index is out of range"?



我正在实现IEEE 802.16d模型,用于预测Python 中的路径损耗

我创建了两个独立的文件:一个是包含IEEE模型函数的库,另一个是主要代码,其中函数将被称为

lib文件中的代码:

def PL_IEEE_80216d(fc,d,Type,htx,hrx,corr_fact,mod=False):
'''
IEEE 802.16d model
Inputs:
fc:        Carrier frequency [Hz]
d:         Distance between transmitter and receiver station [m]
htx:       Height of transmitter [m]
hrx:       Height of receiver [m]
corr_fact: Correlation coefficient
mod:       Modified [True/False]
'''
PL=[]
lambda = 3.10e8/fc
d0=100
if(Type == 'A'):
a=4.6;b=0.0075;c=12.6
elif(Type == 'B'):
a=4;b=0.0065;c=17.1
elif(Type == 'C'):
a=3.6;b=0.005;c=20
else:
a=0;b=0;c=0
if(corr_fact == 'AT&T'):
if( Type == 'A' or Type == 'B'):
C_rx = -10.8 * log10(hrx/2)
else:
C_rx = -20 * log10(hrx/2)
elif(corr_fact == 'OKUMURA'):
if(hrx <= 3):
C_rx = -10 * log10(hrx/3)
else:
C_rx = -20 * log10(hrx/3)
else:
C_rx = 0
gamma=a-b*htx+c/htx
Cf=6*log10(fc/2000)
if(mod == True):
d0_pr = d0 * 10 **(-(Cf+C_rx)/(10*gamma))
else:
d0_pr = d0
A = 20 * log10(4*pi*d0_pr/lambda)+Cf+C_rx
for i in range(0,len(d)):
print('i=%d'%i)
if(d[i] > d0_pr):
PL[i]=A+10*gamma*log10(d[i]/d0)
else:
PL[i]=20*log10(4*pi*d[i]/lambda)
PL = PL.append(P[i])
return PL

我的主要代码:

from model_lib import *
import matplotlib.pyplot as plt
fc=2e9
htx=[30,30]
hrx=[2,10]
distance=arrange(1,3)  #2 distance=[1,2] => d[0]=1,d[1]=2
print("length of distance is %d"%len(distance))
for k in range(0,2):
print('k=%d'%k)
y_IEEE16d[k]=PL_IEEE_80216d(fc,distance,'A',htx[k],hrx[k],'AT&T')

然而,当我运行代码时,我得到了一个错误:

列表分配索引超出范围

我不知道它在哪里超出了范围,尽管我已经试过很多次了。

由于已将PL初始化为空列表,因此无法使用PL[i]。请改用append函数。PL.append(...)

相关内容

最新更新