【问题标题】:Two consecutive error while drawing a bit pattern绘制位模式时连续两个错误
【发布时间】:2022-01-02 14:09:38
【问题描述】:

我正在尝试用下面的代码块绘制一个比特流:

但不幸的是,Python 抛出了两个错误:

C:\Users\bahadir.yalin\Anaconda3\lib\site-packages\numpy\core\_asarray.py:171: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  return array(a, dtype, copy=False, order=order, subok=True)
TypeError: float() argument must be a string or a number, not 'list'

return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

我怎样才能摆脱这个问题?有人能帮助我吗? 谢谢你的时间..

import random as rand
import numpy as np
import math as m
import matplotlib.pyplot as plt

a = [round(rand.randint(0,1)) for x in range(20)]
#list object is not callable

pi = m.pi
signal = []
carrier = []

##t = list(range(1,10000))
t = np.arange(10000)
fc = 0.01

for i in range(20):
    if a[i] == 0:
        sig =-np.ones(10001)
    else:
        sig = np.ones(10001)
    c = np.cos(2 *pi *fc *t)
    carrier = [carrier, c]
    signal = [signal, sig]

plt.plot(signal)
plt.title('Org. Bit Sequence')

【问题讨论】:

  • 你没有声明也没有导入rand。无论如何,最好是a = np.random.randint(0, 2, 20)。此外,sig = -np.ones(10001) 会太长。也许sig = -np.ones_like(t) 会使用正确的大小。 carrier = [carrier, c] 创建一个列表列表的列表...。最好附加列表,例如carrier = np.hstack(carrier, c)?
  • signalcarrier 的预期形状是什么?
  • @JohanC 先生,实际上我将随机声明为 rand。但是,无意中我没有将它添加到我的问题中。通过尝试您的解决方案,它会引发错误:TypeError: _vhstack_dispatcher() 需要 1 个位置参数,但给出了 2 个
  • 你试过carrier = np.hstack([carrier, c]) 吗?另外,请编辑您的帖子并添加缺少的rand。您是否有理由创建递归 carrier 列表而不在图中使用它?
  • @JohanC 首先感谢您对编辑我的问题的建议。仅仅因为我对 stackoverflow 很陌生,所以我不完整地分享了我的问题。在我尝试carrier = np.hstack([carrier, c]) 之后,代码既不抛出错误也不绘制任何东西..

标签: python numpy matplotlib math random


【解决方案1】:

您的代码会产生锯齿状数组,因为您一直在分配 signal = [signal, sig]carrier = [carrier, c],这两种情况下都不会附加到现有列表中,而是会生成包含两个元素的更深层次的列表。

例如,在您的代码之后,carrier 看起来像:

>>> carrier
[[[[[[[[[[[[[[[[[[[[[],
                    array([1.        , 0.99802673, 0.9921147 , ..., 0.98228725, 0.9921147, 0.99802673])],
...
]

>>> len(carrier)
2

>>> len(carrier[0])
2

>>> len(carrier[0][0])
2

# etc.

这里有一些代码可以生成两个(20, 10000) 数组signalcarrier。我不确定这是您打算获得的形状以及您要绘制的确切内容。如果有更多细节渗透到...

n, m = 10_000, 20
fc = 0.01

sign = 2 * np.random.randint(0, 2, size=m) - 1
t = np.arange(n)
signal = sign[:, None] @ np.ones(n)[None, :]
carrier = np.ones((m, 1)) @ np.cos(2 * np.pi * fc * t)[None, :]

现在:

>>> carrier.shape
(20, 10000)

>>> signal.shape
(20, 10000)

【讨论】:

    【解决方案2】:

    首先,-np.ones() 不起作用,因此我将其替换为 -1*np.ones()。其次,通过不使用append() 或类似方法,您将数组的副本作为新元素保存到自身中,如下所示:

    >>> [[],np.ones()] #something like this first loop
    >>> [[[],np.ones()],np.ones()] #second loop
    >>> [[[[],np.ones()],np.ones()], np.ones()] #etc for twenty loops
    

    这是我为使其正常工作所做的:

    import numpy as np
    
    import math as m
    
    import matplotlib.pyplot as plt
    
    import random as rand
    
    a = [rand.randint(0,1) for x in range(20)]
    #list object is not callable
    
    pi = m.pi
    signal = []
    carrier = []
    
    ##t = list(range(1,10000))
    t = np.arange(10000)
    fc = 0.01
    
    for i in range(20):
        if a[i] == 0:
            sig = -1*np.ones(10001)
        else:
            sig = np.ones(10001)
        c = np.cos(2 * pi * fc * t)
        carrier.append(c)
        signal.append(sig)
    
    
    plt.plot(signal)
    plt.title('Org. Bit Sequence')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2018-11-19
      • 1970-01-01
      • 2012-06-30
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多