diff options
author | eb | 2006-10-31 16:07:47 +0000 |
---|---|---|
committer | eb | 2006-10-31 16:07:47 +0000 |
commit | 2c1032480d2874f38af9b5b9d18e753e968619e5 (patch) | |
tree | 6f910bda577337475ffc4a53ad94c026f480f026 /gnuradio-core | |
parent | 3695548d8598f524dd203f7dfc4f73f301b7282a (diff) | |
download | gnuradio-2c1032480d2874f38af9b5b9d18e753e968619e5.tar.gz gnuradio-2c1032480d2874f38af9b5b9d18e753e968619e5.tar.bz2 gnuradio-2c1032480d2874f38af9b5b9d18e753e968619e5.zip |
Merged eb/binstats -r3848:3906 into trunk. These changes
(1) fix a problem with gr_feval* where when called from a live
flowgraph, they resulted in the call back into Python occuring without
holding the Global Interpreter Lock causing a SIGSEGV.
(2) add gr_bin_statistics_f which combines statistics gathering
with a control state machine that allows spectrum sensing or related
applications that need to step through the spectrum to be built.
(3) usrp_spectrum_sense.py which ties all this together in an
application which steps through the spectrum a chunk at a time and
gathers statistics. In the current version, the stats are gathered by
nothing is done with them. Think of this as the framework for a real
application. This code may require tuning of the --tune-delay and
--dwell-delay timeouts to ensure that the samples being processed are
associated with the given center frequency.
git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@3907 221aa14e-8319-0410-a670-987f0aec2ac5
Diffstat (limited to 'gnuradio-core')
-rw-r--r-- | gnuradio-core/src/lib/general/Makefile.am | 3 | ||||
-rw-r--r-- | gnuradio-core/src/lib/general/general.i | 2 | ||||
-rw-r--r-- | gnuradio-core/src/lib/general/gr_bin_statistics_f.cc | 173 | ||||
-rw-r--r-- | gnuradio-core/src/lib/general/gr_bin_statistics_f.h | 99 | ||||
-rw-r--r-- | gnuradio-core/src/lib/general/gr_bin_statistics_f.i | 42 | ||||
-rw-r--r-- | gnuradio-core/src/lib/general/gr_feval.cc | 38 | ||||
-rw-r--r-- | gnuradio-core/src/lib/general/gr_feval.h | 60 | ||||
-rw-r--r-- | gnuradio-core/src/lib/general/gr_feval.i | 140 | ||||
-rw-r--r-- | gnuradio-core/src/python/gnuradio/gr/Makefile.am | 1 | ||||
-rwxr-xr-x | gnuradio-core/src/python/gnuradio/gr/qa_bin_statistics.py | 226 |
10 files changed, 752 insertions, 32 deletions
diff --git a/gnuradio-core/src/lib/general/Makefile.am b/gnuradio-core/src/lib/general/Makefile.am index ace399ab3..aa5ec604f 100644 --- a/gnuradio-core/src/lib/general/Makefile.am +++ b/gnuradio-core/src/lib/general/Makefile.am @@ -98,6 +98,7 @@ libgeneral_la_SOURCES = \ gr_agc2_cc.cc \ gr_agc2_ff.cc \ gr_align_on_samplenumbers_ss.cc \ + gr_bin_statistics_f.cc \ gr_binary_slicer_fb.cc \ gr_bytes_to_syms.cc \ gr_char_to_float.cc \ @@ -219,6 +220,7 @@ grinclude_HEADERS = \ gr_agc2_cc.h \ gr_agc2_ff.h \ gr_align_on_samplenumbers_ss.h \ + gr_bin_statistics_f.h \ gr_binary_slicer_fb.h \ gr_bytes_to_syms.h \ gr_char_to_float.h \ @@ -359,6 +361,7 @@ swiginclude_HEADERS = \ gr_agc2_cc.i \ gr_agc2_ff.i \ gr_align_on_samplenumbers_ss.i \ + gr_bin_statistics_f.i \ gr_binary_slicer_fb.i \ gr_bytes_to_syms.i \ gr_char_to_float.i \ diff --git a/gnuradio-core/src/lib/general/general.i b/gnuradio-core/src/lib/general/general.i index 691cd8332..01cadcc0d 100644 --- a/gnuradio-core/src/lib/general/general.i +++ b/gnuradio-core/src/lib/general/general.i @@ -112,6 +112,7 @@ #include <gr_pwr_squelch_ff.h> #include <gr_ctcss_squelch_ff.h> #include <gr_feedforward_agc_cc.h> +#include <gr_bin_statistics_f.h> %} %include "gr_sync_block.i" @@ -205,5 +206,6 @@ %include "gr_pwr_squelch_ff.i" %include "gr_ctcss_squelch_ff.i" %include "gr_feedforward_agc_cc.i" +%include "gr_bin_statistics_f.i" %include "general_generated.i" diff --git a/gnuradio-core/src/lib/general/gr_bin_statistics_f.cc b/gnuradio-core/src/lib/general/gr_bin_statistics_f.cc new file mode 100644 index 000000000..c6153e8ec --- /dev/null +++ b/gnuradio-core/src/lib/general/gr_bin_statistics_f.cc @@ -0,0 +1,173 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006 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 2, 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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gr_bin_statistics_f.h> +#include <gr_io_signature.h> + +gr_bin_statistics_f_sptr +gr_make_bin_statistics_f(unsigned int vlen, + gr_msg_queue_sptr msgq, + gr_feval_dd *tune, + size_t tune_delay, + size_t dwell_delay) +{ + return gr_bin_statistics_f_sptr(new gr_bin_statistics_f(vlen, + msgq, + tune, + tune_delay, + dwell_delay)); +} + +gr_bin_statistics_f::gr_bin_statistics_f(unsigned int vlen, + gr_msg_queue_sptr msgq, + gr_feval_dd *tune, + size_t tune_delay, + size_t dwell_delay) + : gr_sync_block("bin_statistics_f", + gr_make_io_signature(1, 1, sizeof(float) * vlen), + gr_make_io_signature(0, 0, 0)), + d_vlen(vlen), d_msgq(msgq), d_tune(tune), + d_tune_delay(tune_delay), d_dwell_delay(dwell_delay), + d_center_freq(0), d_delay(0), + d_max(vlen) +{ + enter_init(); +} + +gr_bin_statistics_f::~gr_bin_statistics_f() +{ + // NOP +} + +void +gr_bin_statistics_f::enter_init() +{ + d_state = ST_INIT; + d_delay = 0; +} + +void +gr_bin_statistics_f::enter_tune_delay() +{ + d_state = ST_TUNE_DELAY; + d_delay = d_tune_delay; + d_center_freq = d_tune->calleval(0); +} + +void +gr_bin_statistics_f::enter_dwell_delay() +{ + d_state = ST_DWELL_DELAY; + d_delay = d_dwell_delay; + reset_stats(); +} + +void +gr_bin_statistics_f::leave_dwell_delay() +{ + send_stats(); +} + +int +gr_bin_statistics_f::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + const float *input = (const float *) input_items[0]; + size_t vlen = d_max.size(); + + int n = 0; + int t; + + while (n < noutput_items){ + switch (d_state){ + + case ST_INIT: + enter_tune_delay(); + break; + + case ST_TUNE_DELAY: + t = std::min(noutput_items - n, int(d_delay)); + n += t; + d_delay -= t; + assert(d_delay >= 0); + if (d_delay == 0) + enter_dwell_delay(); + break; + + case ST_DWELL_DELAY: + t = std::min(noutput_items - n, int(d_delay)); + for (int i = 0; i < t; i++){ + accrue_stats(&input[n * vlen]); + n++; + } + d_delay -= t; + assert(d_delay >= 0); + if (d_delay == 0){ + leave_dwell_delay(); + enter_tune_delay(); + } + break; + + default: + assert(0); + } + } + + return noutput_items; +} + +////////////////////////////////////////////////////////////////////////// +// virtual methods for gathering stats +////////////////////////////////////////////////////////////////////////// + +void +gr_bin_statistics_f::reset_stats() +{ + for (size_t i = 0; i < vlen(); i++){ + d_max[i] = 0; + } +} + +void +gr_bin_statistics_f::accrue_stats(const float *input) +{ + for (size_t i = 0; i < vlen(); i++){ + d_max[i] = std::max(d_max[i], input[i]); // compute per bin maxima + } +} + +void +gr_bin_statistics_f::send_stats() +{ + if (msgq()->full_p()) // if the queue is full, don't block, drop the data... + return; + + // build & send a message + gr_message_sptr msg = gr_make_message(0, center_freq(), vlen(), vlen() * sizeof(float)); + memcpy(msg->msg(), &d_max[0], vlen() * sizeof(float)); + msgq()->insert_tail(msg); +} diff --git a/gnuradio-core/src/lib/general/gr_bin_statistics_f.h b/gnuradio-core/src/lib/general/gr_bin_statistics_f.h new file mode 100644 index 000000000..48741f598 --- /dev/null +++ b/gnuradio-core/src/lib/general/gr_bin_statistics_f.h @@ -0,0 +1,99 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006 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 2, 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_GR_BIN_STATISTICS_F_H +#define INCLUDED_GR_BIN_STATISTICS_F_H + + +#include <gr_sync_block.h> +#include <gr_feval.h> +#include <gr_message.h> +#include <gr_msg_queue.h> + +class gr_bin_statistics_f; +typedef boost::shared_ptr<gr_bin_statistics_f> gr_bin_statistics_f_sptr; + + +gr_bin_statistics_f_sptr +gr_make_bin_statistics_f(unsigned int vlen, // vector length + gr_msg_queue_sptr msgq, + gr_feval_dd *tune, // callback + size_t tune_delay, // samples + size_t dwell_delay); // samples + +/*! + * \brief control scanning and record frequency domain statistics + * \ingroup sink + */ +class gr_bin_statistics_f : public gr_sync_block +{ + friend gr_bin_statistics_f_sptr + gr_make_bin_statistics_f(unsigned int vlen, // vector length + gr_msg_queue_sptr msgq, + gr_feval_dd *tune, // callback + size_t tune_delay, // samples + size_t dwell_delay); // samples + + enum state_t { ST_INIT, ST_TUNE_DELAY, ST_DWELL_DELAY }; + + size_t d_vlen; + gr_msg_queue_sptr d_msgq; + gr_feval_dd *d_tune; + size_t d_tune_delay; + size_t d_dwell_delay; + double d_center_freq; + + state_t d_state; + size_t d_delay; // nsamples remaining to state transition + + gr_bin_statistics_f(unsigned int vlen, + gr_msg_queue_sptr msgq, + gr_feval_dd *tune, + size_t tune_delay, + size_t dwell_delay); + + void enter_init(); + void enter_tune_delay(); + void enter_dwell_delay(); + void leave_dwell_delay(); + +protected: + std::vector<float> d_max; // per bin maxima + + size_t vlen() const { return d_vlen; } + double center_freq() const { return d_center_freq; } + gr_msg_queue_sptr msgq() const { return d_msgq; } + + virtual void reset_stats(); + virtual void accrue_stats(const float *input); + virtual void send_stats(); + +public: + ~gr_bin_statistics_f(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + +}; + +#endif diff --git a/gnuradio-core/src/lib/general/gr_bin_statistics_f.i b/gnuradio-core/src/lib/general/gr_bin_statistics_f.i new file mode 100644 index 000000000..a0fbb9873 --- /dev/null +++ b/gnuradio-core/src/lib/general/gr_bin_statistics_f.i @@ -0,0 +1,42 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006 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 2, 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +GR_SWIG_BLOCK_MAGIC(gr,bin_statistics_f); + +gr_bin_statistics_f_sptr +gr_make_bin_statistics_f(unsigned int vlen, // vector length + gr_msg_queue_sptr msgq, + gr_feval_dd *tune, // callback + size_t tune_delay, // samples + size_t dwell_delay); // samples + + +class gr_bin_statistics_f : public gr_sync_block +{ +private: + gr_bin_statistics_f(unsigned int vlen, + gr_msg_queue_sptr msgq, + gr_feval_dd *tune, + size_t tune_delay, + size_t dwell_delay); +public: + ~gr_bin_statistics_f(); +}; diff --git a/gnuradio-core/src/lib/general/gr_feval.cc b/gnuradio-core/src/lib/general/gr_feval.cc index 3617620b5..9ac5b1579 100644 --- a/gnuradio-core/src/lib/general/gr_feval.cc +++ b/gnuradio-core/src/lib/general/gr_feval.cc @@ -34,6 +34,14 @@ gr_feval_dd::eval(double x) return 0; } +double +gr_feval_dd::calleval(double x) +{ + return eval(x); +} + +// ---------------------------------------------------------------- + gr_feval_cc::~gr_feval_cc(){} gr_complex @@ -42,6 +50,14 @@ gr_feval_cc::eval(gr_complex x) return 0; } +gr_complex +gr_feval_cc::calleval(gr_complex x) +{ + return eval(x); +} + +// ---------------------------------------------------------------- + gr_feval_ll::~gr_feval_ll(){} long @@ -50,6 +66,14 @@ gr_feval_ll::eval(long x) return 0; } +long +gr_feval_ll::calleval(long x) +{ + return eval(x); +} + +// ---------------------------------------------------------------- + gr_feval::~gr_feval(){} void @@ -58,29 +82,35 @@ gr_feval::eval(void) // nop } +void +gr_feval::calleval(void) +{ + eval(); +} + /* * Trivial examples showing C++ (transparently) calling Python */ double gr_feval_dd_example(gr_feval_dd *f, double x) { - return f->eval(x); + return f->calleval(x); } gr_complex gr_feval_cc_example(gr_feval_cc *f, gr_complex x) { - return f->eval(x); + return f->calleval(x); } long gr_feval_ll_example(gr_feval_ll *f, long x) { - return f->eval(x); + return f->calleval(x); } void gr_feval_example(gr_feval *f) { - f->eval(); + f->calleval(); } diff --git a/gnuradio-core/src/lib/general/gr_feval.h b/gnuradio-core/src/lib/general/gr_feval.h index a2f7020a4..b8a5eec25 100644 --- a/gnuradio-core/src/lib/general/gr_feval.h +++ b/gnuradio-core/src/lib/general/gr_feval.h @@ -31,17 +31,24 @@ * and is callable from both places. It uses SWIG's * "director" feature to implement the magic. * It's slow. Don't use it in a performance critical path. + * + * Override eval to define the behavior. + * Use calleval to invoke eval (this kludge is required to allow a + * python specific "shim" to be inserted. */ class gr_feval_dd { -public: - gr_feval_dd() {} - virtual ~gr_feval_dd(); - +protected: /*! * \brief override this to define the function */ virtual double eval(double x); + +public: + gr_feval_dd() {} + virtual ~gr_feval_dd(); + + virtual double calleval(double x); // invoke "eval" }; /*! @@ -51,17 +58,24 @@ public: * and is callable from both places. It uses SWIG's * "director" feature to implement the magic. * It's slow. Don't use it in a performance critical path. + * + * Override eval to define the behavior. + * Use calleval to invoke eval (this kludge is required to allow a + * python specific "shim" to be inserted. */ class gr_feval_cc { -public: - gr_feval_cc() {} - virtual ~gr_feval_cc(); - +protected: /*! * \brief override this to define the function */ virtual gr_complex eval(gr_complex x); + +public: + gr_feval_cc() {} + virtual ~gr_feval_cc(); + + virtual gr_complex calleval(gr_complex x); // invoke "eval" }; /*! @@ -71,17 +85,24 @@ public: * and is callable from both places. It uses SWIG's * "director" feature to implement the magic. * It's slow. Don't use it in a performance critical path. + * + * Override eval to define the behavior. + * Use calleval to invoke eval (this kludge is required to allow a + * python specific "shim" to be inserted. */ class gr_feval_ll { -public: - gr_feval_ll() {} - virtual ~gr_feval_ll(); - +protected: /*! * \brief override this to define the function */ virtual long eval(long x); + +public: + gr_feval_ll() {} + virtual ~gr_feval_ll(); + + virtual long calleval(long x); // invoke "eval" }; /*! @@ -91,17 +112,24 @@ public: * and is callable from both places. It uses SWIG's * "director" feature to implement the magic. * It's slow. Don't use it in a performance critical path. + * + * Override eval to define the behavior. + * Use calleval to invoke eval (this kludge is required to allow a + * python specific "shim" to be inserted. */ class gr_feval { -public: - gr_feval() {} - virtual ~gr_feval(); - +protected: /*! * \brief override this to define the function */ virtual void eval(); + +public: + gr_feval() {} + virtual ~gr_feval(); + + virtual void calleval(); // invoke "eval" }; /*! diff --git a/gnuradio-core/src/lib/general/gr_feval.i b/gnuradio-core/src/lib/general/gr_feval.i index 09d98b648..9df276f66 100644 --- a/gnuradio-core/src/lib/general/gr_feval.i +++ b/gnuradio-core/src/lib/general/gr_feval.i @@ -20,53 +20,169 @@ * Boston, MA 02110-1301, USA. */ + +/* + * N.B., this is a _very_ non-standard SWIG .i file + * + * It contains a bunch of magic that is required to ensure that when + * these classes are used as base classes for python code, + * everything works when calling back from C++ into Python. + * + * The gist of the problem is that our C++ code is usually not holding + * the Python Global Interpreter Lock (GIL). Thus if we invoke a + * "director" method from C++, we'll end up in Python not holding the + * GIL. Disaster (SIGSEGV) will result. To avoid this we insert a + * "shim" that grabs and releases the GIL. + * + * If you don't understand SWIG "directors" or the Python GIL, + * don't bother trying to understand what's going on in here. + * + * [We could eliminate a bunch of this hair by requiring SWIG 1.3.29 + * or later and some additional magic declarations, but many systems + * aren't shipping that version yet. Thus we kludge...] + */ + + // Enable SWIG directors for these classes -%feature("director") gr_feval_dd; -%feature("director") gr_feval_cc; -%feature("director") gr_feval_ll; -%feature("director") gr_feval; +%feature("director") gr_py_feval_dd; +%feature("director") gr_py_feval_cc; +%feature("director") gr_py_feval_ll; +%feature("director") gr_py_feval; + +%feature("nodirector") gr_py_feval_dd::calleval; +%feature("nodirector") gr_py_feval_cc::calleval; +%feature("nodirector") gr_py_feval_ll::calleval; +%feature("nodirector") gr_py_feval::calleval; + + +%rename(feval_dd) gr_py_feval_dd; +%rename(feval_cc) gr_py_feval_cc; +%rename(feval_ll) gr_py_feval_ll; +%rename(feval) gr_py_feval; + +//%exception { +// try { $action } +// catch (Swig::DirectorException &e) { std::cerr << e.getMessage(); SWIG_fail; } +//} + +%{ + +// class that ensures we acquire and release the Python GIL +class ensure_py_gil_state { + PyGILState_STATE d_gstate; +public: + ensure_py_gil_state() { d_gstate = PyGILState_Ensure(); } + ~ensure_py_gil_state() { PyGILState_Release(d_gstate); } +}; + +%} -%rename(feval_dd) gr_feval_dd; +/* + * These are the real C++ base classes, however we don't want these exposed. + */ +%ignore gr_feval_dd; class gr_feval_dd { +protected: + virtual double eval(double x); + public: gr_feval_dd() {} virtual ~gr_feval_dd(); - virtual double eval(double x); + virtual double calleval(double x); }; -%rename(feval_cc) gr_feval_cc; +%ignore gr_feval_cc; class gr_feval_cc { +protected: + virtual gr_complex eval(gr_complex x); + public: gr_feval_cc() {} virtual ~gr_feval_cc(); - virtual gr_complex eval(gr_complex x); + virtual gr_complex calleval(gr_complex x); }; -%rename(feval_ll) gr_feval_ll; +%ignore gr_feval_ll; class gr_feval_ll { +protected: + virtual long eval(long x); + public: gr_feval_ll() {} virtual ~gr_feval_ll(); - virtual long eval(long x); + virtual long calleval(long x); }; -%rename(feval) gr_feval; +%ignore gr_feval; class gr_feval { +protected: + virtual void eval(); + public: gr_feval() {} virtual ~gr_feval(); - virtual void eval(); + virtual void calleval(); +}; + +/* + * These are the ones to derive from in Python. They have the magic shim + * that ensures that we're holding the Python GIL when we enter Python land... + */ + +%inline %{ + +class gr_py_feval_dd : public gr_feval_dd +{ + public: + double calleval(double x) + { + ensure_py_gil_state _lock; + return eval(x); + } }; +class gr_py_feval_cc : public gr_feval_cc +{ + public: + gr_complex calleval(gr_complex x) + { + ensure_py_gil_state _lock; + return eval(x); + } +}; + +class gr_py_feval_ll : public gr_feval_ll +{ + public: + long calleval(long x) + { + ensure_py_gil_state _lock; + return eval(x); + } +}; + +class gr_py_feval : public gr_feval +{ + public: + void calleval() + { + ensure_py_gil_state _lock; + eval(); + } +}; + +%} + + // examples / test cases diff --git a/gnuradio-core/src/python/gnuradio/gr/Makefile.am b/gnuradio-core/src/python/gnuradio/gr/Makefile.am index ec91917b2..fcf026146 100644 --- a/gnuradio-core/src/python/gnuradio/gr/Makefile.am +++ b/gnuradio-core/src/python/gnuradio/gr/Makefile.am @@ -48,6 +48,7 @@ noinst_PYTHON = \ qa_add_v_and_friends.py \ qa_agc.py \ qa_basic_flow_graph.py \ + qa_bin_statistics.py \ qa_cma_equalizer.py \ qa_complex_to_xxx.py \ qa_constellation_decoder_cb.py \ diff --git a/gnuradio-core/src/python/gnuradio/gr/qa_bin_statistics.py b/gnuradio-core/src/python/gnuradio/gr/qa_bin_statistics.py new file mode 100755 index 000000000..75cfc8308 --- /dev/null +++ b/gnuradio-core/src/python/gnuradio/gr/qa_bin_statistics.py @@ -0,0 +1,226 @@ +#!/usr/bin/env python +# +# Copyright 2006 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 2, 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. +# + +from gnuradio import gr, gr_unittest +import random +import struct + +#import os +#print "pid =", os.getpid() +#raw_input("Attach gdb and press return...") + + +class counter(gr.feval_dd): + def __init__(self, step_size=1): + gr.feval_dd.__init__(self) + self.step_size = step_size + self.count = 0 + + def eval(self, input): + #print "eval: self.count =", self.count + t = self.count + self.count = self.count + self.step_size + return t + + +class counter3(gr.feval_dd): + def __init__(self, f, step_size): + gr.feval_dd.__init__(self) + self.f = f + self.step_size = step_size + self.count = 0 + + def eval(self, input): + try: + #print "eval: self.count =", self.count + t = self.count + self.count = self.count + self.step_size + self.f(self.count) + except Exception, e: + print "Exception: ", e + return t + +def foobar3(new_t): + #print "foobar3: new_t =", new_t + pass + + +class counter4(gr.feval_dd): + def __init__(self, obj_instance, step_size): + gr.feval_dd.__init__(self) + self.obj_instance = obj_instance + self.step_size = step_size + self.count = 0 + + def eval(self, input): + try: + #print "eval: self.count =", self.count + t = self.count + self.count = self.count + self.step_size + self.obj_instance.foobar4(self.count) + except Exception, e: + print "Exception: ", e + return t + + +class parse_msg(object): + def __init__(self, msg): + self.center_freq = msg.arg1() + self.vlen = int(msg.arg2()) + assert(msg.length() == self.vlen * gr.sizeof_float) + self.data = struct.unpack('%df' % (self.vlen,), msg.to_string()) + + +class test_bin_statistics(gr_unittest.TestCase): + + def setUp(self): + self.fg = gr.flow_graph () + + def tearDown(self): + self.fg = None + + def test_001(self): + vlen = 4 + tune = counter(1) + tune_delay = 0 + dwell_delay = 1 + msgq = gr.msg_queue() + + src_data = tuple([float(x) for x in + ( 1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10, 11, 12, + 13, 14, 15, 16 + )]) + + expected_results = tuple([float(x) for x in + ( 1, 2, 3, 4, + 5, 6, 7, 8, + 9, 10, 11, 12, + 13, 14, 15, 16 + )]) + + src = gr.vector_source_f(src_data, False) + s2v = gr.stream_to_vector(gr.sizeof_float, vlen) + stats = gr.bin_statistics_f(vlen, msgq, tune, tune_delay, dwell_delay) + self.fg.connect(src, s2v, stats) + self.fg.run() + self.assertEqual(4, msgq.count()) + for i in range(4): + m = parse_msg(msgq.delete_head()) + #print "m =", m.center_freq, m.data + self.assertEqual(expected_results[vlen*i:vlen*i + vlen], m.data) + + def test_002(self): + vlen = 4 + tune = counter(1) + tune_delay = 1 + dwell_delay = 2 + msgq = gr.msg_queue() + + src_data = tuple([float(x) for x in + ( 1, 2, 3, 4, + 9, 6, 11, 8, + 5, 10, 7, 12, + 13, 14, 15, 16 + )]) + + expected_results = tuple([float(x) for x in + ( 9, 10, 11, 12)]) + + src = gr.vector_source_f(src_data, False) + s2v = gr.stream_to_vector(gr.sizeof_float, vlen) + stats = gr.bin_statistics_f(vlen, msgq, tune, tune_delay, dwell_delay) + self.fg.connect(src, s2v, stats) + self.fg.run() + self.assertEqual(1, msgq.count()) + for i in range(1): + m = parse_msg(msgq.delete_head()) + #print "m =", m.center_freq, m.data + self.assertEqual(expected_results[vlen*i:vlen*i + vlen], m.data) + + + + def test_003(self): + vlen = 4 + tune = counter3(foobar3, 1) + tune_delay = 1 + dwell_delay = 2 + msgq = gr.msg_queue() + + src_data = tuple([float(x) for x in + ( 1, 2, 3, 4, + 9, 6, 11, 8, + 5, 10, 7, 12, + 13, 14, 15, 16 + )]) + + expected_results = tuple([float(x) for x in + ( 9, 10, 11, 12)]) + + src = gr.vector_source_f(src_data, False) + s2v = gr.stream_to_vector(gr.sizeof_float, vlen) + stats = gr.bin_statistics_f(vlen, msgq, tune, tune_delay, dwell_delay) + self.fg.connect(src, s2v, stats) + self.fg.run() + self.assertEqual(1, msgq.count()) + for i in range(1): + m = parse_msg(msgq.delete_head()) + #print "m =", m.center_freq, m.data + self.assertEqual(expected_results[vlen*i:vlen*i + vlen], m.data) + + + def foobar4(self, new_t): + #print "foobar4: new_t =", new_t + pass + + def test_004(self): + vlen = 4 + tune = counter4(self, 1) + tune_delay = 1 + dwell_delay = 2 + msgq = gr.msg_queue() + + src_data = tuple([float(x) for x in + ( 1, 2, 3, 4, + 9, 6, 11, 8, + 5, 10, 7, 12, + 13, 14, 15, 16 + )]) + + expected_results = tuple([float(x) for x in + ( 9, 10, 11, 12)]) + + src = gr.vector_source_f(src_data, False) + s2v = gr.stream_to_vector(gr.sizeof_float, vlen) + stats = gr.bin_statistics_f(vlen, msgq, tune, tune_delay, dwell_delay) + self.fg.connect(src, s2v, stats) + self.fg.run() + self.assertEqual(1, msgq.count()) + for i in range(1): + m = parse_msg(msgq.delete_head()) + #print "m =", m.center_freq, m.data + self.assertEqual(expected_results[vlen*i:vlen*i + vlen], m.data) + + +if __name__ == '__main__': + gr_unittest.main () |