1

I'm trying to calculate the bit error rate in python with numpy. The code looks like this:

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))

But it's giving me the error:

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
4

1 回答 1

2

cmath不支持 numpy 数组:

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

您似乎正在导入很多功能,因为from foo import *这真的会让您感到困惑。此外,您使用的是整数(例如2/5)而不是浮点数,因此上面的等式只返回一个全为零的数组:

>>> 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])
于 2013-07-21T18:23:42.390 回答