Splitting bits

Hey folks-

Let’s say I have a stream of shorts whose values are one of [0,1,2,3]
(in
other words, the bottom 2 bits are active). I want to split these bits,
so
that:
[2,3,3,1,0] -> [1,0,1,1,1,1,0,1,0,0], etc.

What block(s) can help me achieve this?

On Thu, Jan 10, 2008 at 04:43:17PM -0500, Steven C. wrote:

Hey folks-

Let’s say I have a stream of shorts whose values are one of [0,1,2,3] (in
other words, the bottom 2 bits are active). I want to split these bits, so
that:
[2,3,3,1,0] -> [1,0,1,1,1,1,0,1,0,0], etc.

What block(s) can help me achieve this?

gr_packed_to_unpacked or gr_chunks_to_symbols are probably close to
what you’re looking for.

Eric

/*!

  • \brief Convert a stream of packed bytes or shorts to stream of
    unpacked bytes or shorts.
  • \ingroup block
  • input: stream of @I_TYPE@; output: stream of @O_TYPE@
  • This is the inverse of gr_unpacked_to_packed_XX.
  • The bits in the bytes or shorts input stream are grouped into chunks
    of
  • \p bits_per_chunk bits and each resulting chunk is written right-
  • justified to the output stream of bytes or shorts.
  • All b or 16 bits of the each input bytes or short are processed.
  • The right thing is done if bits_per_chunk is not a power of two.
  • The combination of gr_packed_to_unpacked_XX_ followed by
  • gr_chunks_to_symbols_Xf or gr_chunks_to_symbols_Xc handles the
  • general case of mapping from a stream of bytes or shorts into
  • arbitrary float or complex symbols.
  • \sa gr_packed_to_unpacked_bb, gr_unpacked_to_packed_bb,
  • \sa gr_packed_to_unpacked_ss, gr_unpacked_to_packed_ss,
  • \sa gr_chunks_to_symbols_bf, gr_chunks_to_symbols_bc.
  • \sa gr_chunks_to_symbols_sf, gr_chunks_to_symbols_sc.
    */

I had looked at those blocks, but can’t figure out a way to get them to
do
what I want…
(2,3,1) into gr.packed_to_unpacked_ss(1,gr.GR_MSB_FIRST) gives:
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
rather than:
(1,0,1,1,0,1)

bang head on keyboard in search of enlightenment

You might be able to run unpacked_to_packed followed by
packed_to_unpacked to get what you want. It’s ugly, but you don’t
have to write any code :wink:

pack = gr.unpacked_to_packed_ss(2, gr.GR_MSB_FIRST)
unpack = gr.packed_to_unpacked_ss(1, gr.GR_MSB_FIRST)

Eric

Aha! Shame on me for not thinking of that. If the number of input
shorts is not a multiple of 8, some will be lost from the end, but I
can live with that.

Thanks Eric.

On Thu, Jan 10, 2008 at 05:24:06PM -0500, Steven C. wrote:

I had looked at those blocks, but can’t figure out a way to get them to do
what I want…
(2,3,1) into gr.packed_to_unpacked_ss(1,gr.GR_MSB_FIRST) gives:
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
rather than:
(1,0,1,1,0,1)

bang head on keyboard in search of enlightenment

You might be able to run unpacked_to_packed followed by
packed_to_unpacked to get what you want. It’s ugly, but you don’t
have to write any code :wink:

pack = gr.unpacked_to_packed_ss(2, gr.GR_MSB_FIRST)
unpack = gr.packed_to_unpacked_ss(1, gr.GR_MSB_FIRST)

Eric

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs