我正在嘗試使用以下代碼塊繪制位流:
但不幸的是,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')
uj5u.com熱心網友回復:
您的代碼會產生鋸齒狀陣列,因為您一直在分配signal = [signal, sig]and 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)陣列signal和carrier. 我不確定這是您打算獲得的形狀以及您要繪制的確切內容。如果更多細節滲透到...將調整此答案
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)
uj5u.com熱心網友回復:
首先,-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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/406028.html
標籤:
上一篇:創建一系列數字序列
下一篇:期限結構的間隔
