【发布时间】: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)? -
signal和carrier的预期形状是什么? -
@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