Forum: GNU Radio generate 0s and 1s

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5d49c921fbbc3c4c04315826d9fde205?d=identicon&s=25 yufeng wang (Guest)
on 2009-02-13 14:43
(Received via mailing list)
Hi, all,

Can anyone tell me which function in Python could I use to generate a
fixed length of 0s and 1s? I know in matlab we could just use
randsrc(x, y, 0:1), while I've no idea how to implement it in Python.
Thanks for ur help!
28a4af15b4d496d7900593255ea9c8a3?d=identicon&s=25 Ed Criscuolo (Guest)
on 2009-02-13 15:13
(Received via mailing list)
yufeng wang wrote:
> Can anyone tell me which function in Python could I use to generate a
> fixed length of 0s and 1s? I know in matlab we could just use
> randsrc(x, y, 0:1), while I've no idea how to implement it in Python.
> Thanks for ur help!

Look at the linear shift feedback register (gr_lfsr_32k_source_s) block.
It should do what you want.

@(^.^)@  Ed
3e5d1707dd5f4807ed9938d5c37b2649?d=identicon&s=25 Patrick Strasser (Guest)
on 2009-02-13 16:54
(Received via mailing list)
yufeng wang wrote am 2009-02-13 14:34:
> Hi, all,
>
> Can anyone tell me which function in Python could I use to generate a
> fixed length of 0s and 1s? I know in matlab we could just use
> randsrc(x, y, 0:1), while I've no idea how to implement it in Python.
> Thanks for ur help!

If you want to have random sources in GNU Radio, you can use various
sources, like a noise source or a LSFR source[2].

One method for a fixed length:

import random
length = 10
rand_sequence = random.sample(length/2*[1]+length/2*[0],length)

This first generates a list of 1s and 0s and permutates them. You can
vary the length/2-part to get a not-even-distribution.
Or:
rand_sequence = [x%2 for x in random.sample(xrange(10000000), length)]

These may not be the most efficient or random one, but it should do for
a first shot. For really long sequences yu should construct a sequence
from a random source with multiple invocations, like multiple
random.choice([0,1]) if you need reproducible numbers, or
random.SystemRandom([0,1]) to get good non-reproducible numbers. Read
all about random numbers and Python in [0]

If you are unfamiliar with Python, just read the Python Tutorial[1],
it's really great.

Patrick

[0] http://docs.python.org/library/random.html
[1] http://docs.python.org/tutorial/
[2] http://www.gnuradio.org/doc/doxygen/group__source.html

--
Engineers motto: cheap, good, fast: choose any two
Patrick Strasser <patrick dot strasser at student dot tugraz dot at>
Student of Telematik, Techn. University Graz, Austria
This topic is locked and can not be replied to.