discuss-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Discuss-gnuradio] Digital Upsampler Code


From: Ed Criscuolo
Subject: [Discuss-gnuradio] Digital Upsampler Code
Date: Thu, 18 Sep 2008 15:01:47 -0400
User-agent: Thunderbird 2.0.0.9 (Macintosh/20071031)

On Wednesday 17 September 2008 08:05:59 you wrote:
>
> I wrote a simple "digital upsampler" block to do exactly this. It takes
> in a binary stream at the lower data rate and upsamples it to match the
> PN "chip" rate.  It differs from the interpolator blocks in that it
> inserts the nearest correct value (1 or 0) instead of simply inserting
> zeros.
>
>
> I'd be happy to share the code.  What's the preferred way
> on this list?
>
>   a) In-line with message
>   b) Message attachment
>   c) Other
>

If it's reasonably small, please post it the the list. Either (a) or (b) is fine. Otherwise post a link.

Thanks!
Eric


I wrote this for use in constructing a DSSS receiver for NASA's
Telemetry Data Relay Satellite System (TDRSS), thus the TDRSS
prefix on everything.

It basically works by stepping thru the output bits and
accumulating bit times.  It replicates the same input bit
into each output bit until the accumulated time exceeds
an input bit time, at which point it steps to the next input
bit. Any remainder over one output bit time is remembered
and carried forward to the next loop iteration or the
next call to general_work.

@(^.^)@  Ed


==========tdrss_digital_upsampler_bb.h===============

/* -*- c++ -*- */
/*
 * Copyright 2004 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */
#ifndef INCLUDED_TDRSS_DIGITAL_UPSAMPLER_BB_H
#define INCLUDED_TDRSS_DIGITAL_UPSAMPLER_BB_H

#include <gr_sync_block.h>

class tdrss_digital_upsampler_bb;

/*
 * We use boost::shared_ptr's instead of raw pointers for all access
 * to gr_blocks (and many other data structures).  The shared_ptr gets
 * us transparent reference counting, which greatly simplifies storage
 * management issues.  This is especially helpful in our hybrid
 * C++ / Python system.
 *
 * See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
 *
 * As a convention, the _sptr suffix indicates a boost::shared_ptr
 */
typedef boost::shared_ptr<tdrss_digital_upsampler_bb> tdrss_digital_upsampler_bb_sptr;

/*!
* \brief Return a shared_ptr to a new instance of tdrss_digital_upsampler_bb.
 *
 * To avoid accidental use of raw pointers, tdrss_digital_upsampler_bb's
 * constructor is private.  tdrss_make_digital_upsampler_bb is the public
 * interface for creating new instances.
 */
tdrss_digital_upsampler_bb_sptr tdrss_make_digital_upsampler_bb (unsigned int data_rate,

unsigned int sample_rate);

/*!
 * \brief Upsamples a digital stream of bits carried in the lsb of a byte.
 * \ingroup block
 *
 */
class tdrss_digital_upsampler_bb : public gr_block
{
private:
  // The friend declaration allows tdrss_make_digital_upsampler_bb to
  // access the private constructor.

friend tdrss_digital_upsampler_bb_sptr tdrss_make_digital_upsampler_bb (unsigned int data_rate,

   unsigned int sample_rate);

  unsigned int d_data_rate;
  unsigned int d_sample_rate;
  const float  d_bit_time;
  const float  d_sample_time;
  float        d_remainder_time;

  tdrss_digital_upsampler_bb (unsigned int data_rate,
unsigned int sample_rate); // private constructor

 public:
  ~tdrss_digital_upsampler_bb ();       // public destructor

  void forecast ( int             noutput_items,
                  gr_vector_int   &ninput_items_required);

  // Where all the action really happens

  int general_work (int noutput_items,
            gr_vector_int &ninput_items,
            gr_vector_const_void_star &input_items,
            gr_vector_void_star &output_items);
};

#endif /* INCLUDED_TDRSS_DIGITAL_UPSAMPLER_BB_H */


==========tdrss_digital_upsampler_bb.i===============

/* -*- c++ -*- */

%feature("autodoc", "1");           // generate python docstrings

%include "exception.i"
%import "gnuradio.i"                  // the common stuff

%{
#include "gnuradio_swig_bug_workaround.h"     // mandatory bug fix
#include "tdrss_digital_upsampler_bb.h"
#include <stdexcept>
%}

// ----------------------------------------------------------------

/*
 * First arg is the package prefix.
 * Second arg is the name of the class minus the prefix.
 *
 * This does some behind-the-scenes magic so we can
 * access tdrss_xor_bb from python as tdrss.xor_bb
 */

GR_SWIG_BLOCK_MAGIC(tdrss,digital_upsampler_bb);

tdrss_digital_upsampler_bb_sptr tdrss_make_digital_upsampler_bb (unsigned int data_rate,

unsigned int sample_rate);

class tdrss_digital_upsampler_bb : public gr_block
{
private:
  tdrss_digital_upsampler_bb (unsigned int data_rate,
                              unsigned int sample_rate);
};



==========tdrss_digital_upsampler_bb.cpp===============

/* -*- c++ -*- */
/*
 * Copyright 2004 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

/*
 * config.h is generated by configure.  It contains the results
 * of probing for features, options etc.  It should be the first
 * file included in your .cc file.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tdrss_digital_upsampler_bb.h>
#include <gr_io_signature.h>

/*
 * Create a new instance of tdrss_digital_upsampler_bb and return
 * a boost shared_ptr.  This is effectively the public constructor.
 */
tdrss_digital_upsampler_bb_sptr
tdrss_make_digital_upsampler_bb (unsigned int data_rate, unsigned int sample_rate)
{
return tdrss_digital_upsampler_bb_sptr (new tdrss_digital_upsampler_bb (data_rate, sample_rate));
}

/*
 * Specify constraints on number of input and output streams.
 * This info is used to construct the input and output signatures
 * (2nd & 3rd args to gr_block's constructor).  The input and
 * output signatures are used by the runtime system to
 * check that a valid number and type of inputs and outputs
 * are connected to this block.  In this case, we accept
 * only 1 input and 1 output.
 */
static const int MIN_IN = 1;    // mininum number of input streams
static const int MAX_IN = 1;    // maximum number of input streams
static const int MIN_OUT = 1;   // minimum number of output streams
static const int MAX_OUT = 1;   // maximum number of output streams

/*
 * The private constructor
 */
tdrss_digital_upsampler_bb::tdrss_digital_upsampler_bb (unsigned int data_rate, unsigned int sample_rate)
  : gr_block ("digital_upsampler_bb",
                      gr_make_io_signature (MIN_IN, MAX_IN, sizeof (unsigned 
char)),
                      gr_make_io_signature (MIN_OUT, MAX_OUT, sizeof (unsigned 
char))),
        d_data_rate(data_rate),
        d_sample_rate(sample_rate),
    d_bit_time(1.0/data_rate),
    d_sample_time(1.0/sample_rate),
    d_remainder_time(0.0)
{
    set_relative_rate(d_sample_rate/d_data_rate);
}

/*
 * Our virtual destructor.
 */
tdrss_digital_upsampler_bb::~tdrss_digital_upsampler_bb ()
{
  // nothing else required
}


void tdrss_digital_upsampler_bb::forecast ( int             noutput_items,
gr_vector_int &ninput_items_required)
{
    int n = noutput_items * d_data_rate / d_sample_rate;
    ninput_items_required[0] = (n==0 ? 1 : n);
}


int
tdrss_digital_upsampler_bb::general_work (int noutput_items,
                        gr_vector_int &ninput_items,
                        gr_vector_const_void_star &input_items,
                        gr_vector_void_star &output_items)
{
  const unsigned char *in  = (const unsigned char *) input_items[0];
  unsigned char       *out = (unsigned char *)       output_items[0];
  float                elapsed_time;
  int                  n_in_items = ninput_items[0];
  int                  i; // input item counter
  int                  o; // output item counter

  elapsed_time = d_remainder_time;  // get any leftover time from last call

  // Loop thru all output items 'till we run out of output or input
  i = 0;
  for (o = 0; (o < noutput_items) && (i < n_in_items); o++)
  {
    if(elapsed_time >= d_bit_time)  // see if we've started a new bit
    {
      elapsed_time -= d_bit_time;   // adjust by one bit time
      i++;                          // move to next input bit
    }
    out[o] = in[i];                 // copy input bit value to output
    elapsed_time += d_sample_time;  // one sample time elapsed
  }

d_remainder_time = elapsed_time; // save any remainder time for next call

  // Tell runtime system how many input items we consumed on
  // each input stream.
  if((d_remainder_time >= d_bit_time) && (i < n_in_items))
  {
    // we completed the "i"th input bit
    consume_each (i+1);
  }
  else
  {
    // we're still working on the 'i"th input bit
    consume_each (i);
  }

  // Tell runtime system how many output items we produced.
  return o;
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]