Home > other >  Two consecutive error while drawing a bit pattern
Two consecutive error while drawing a bit pattern

Time:01-02

I am trying to draw a bit stream with the code block below:

But unfortunately Python throws two errors which are:

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'

and

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

How can I get rid of the problem? Can someone help me please? Thank you for your time..

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

CodePudding user response:

Your code results in jagged arrays, because you keep assigning signal = [signal, sig] and carrier = [carrier, c], which in both cases does not append to the existing list, but makes a level deeper list of two elements.

For example, after your code, carrier looks like:

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

Here is some code to produce two (20, 10000) arrays signal and carrier. I am not sure this is the shape you intend to obtain and what exactly you are trying to plot. Will adjust this answer if more details percolate through...

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, :]

Now:

>>> carrier.shape
(20, 10000)

>>> signal.shape
(20, 10000)

CodePudding user response:

Firstly, -np.ones() wasn't working so I replaced it with -1*np.ones(). Secondly, by not using append() or a similar method you were saving a copy of the array into itself as a new element, like this:

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

This is what I did to get it working:

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()
  • Related