gpt4 book ai didi

python - efrc 给出错误 length-1 数组可以转换为 python

转载 作者:行者123 更新时间:2023-12-01 05:40:06 24 4
gpt4 key购买 nike

我正在尝试使用 numpy 计算 python 中的误码率。代码如下所示:

EbbyNo = arange(0,16,1)
ebno = 10**(EbbyNo/10)
BER2 = (3/8)*erfc(cmath.sqrt(ebno*(2/5)))+(1/4)*erfc(3*sqrt(2/5*ebno))

但是它给了我错误:

BER2 = (3/8)*erfc(cmath.sqrt(ebno*(2/5)))+(1/4)*erfc(3*sqrt(2/5*ebno))
TypeError: only length-1 arrays can be converted to Python scalars

最佳答案

cmath 不支持 numpy 数组:

BER2=(3/8)*erfc(sqrt(ebno*(2/5)))+(1/4)*erfc(3*sqrt(2/5*ebno))

您似乎正在导入很多函数,例如 from foo import * 这确实会让您陷入困境。此外,您还使用整数(例如 2/5)而不是 float ,因此上面的等式仅返回全零的数组:

>>> 2/5
0
>>> 2./5
0.4

我建议:

>>> import numpy as np
>>> import scipy.special as sp
>>> EbbyNo=np.arange(0.,16.,1)
>>> ebno=10**(EbbyNo/10)
>>> BER2=(3./8)*sp.erfc(np.sqrt(ebno*(2./5)))+(1./4)*sp.erfc(3*np.sqrt(2./5*ebno))
>>> BER2
array([ 1.40982603e-01, 1.18997473e-01, 9.77418560e-02,
7.74530603e-02, 5.86237373e-02, 4.18927600e-02,
2.78713278e-02, 1.69667344e-02, 9.24721374e-03,
4.39033609e-03, 1.75415062e-03, 5.64706106e-04,
1.38658689e-04, 2.42337855e-05, 2.76320800e-06,
1.84185551e-07])

关于python - efrc 给出错误 length-1 数组可以转换为 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17775397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com