Hi, here's another function which I thought be best contributed to Johnathans stack of reviews :)
What I needed for some spectral estimation was a moving average block which operates on vectors. This is a modified version of gr_moving_average_*. Cheers MB -- Dipl.-Ing. Martin Braun Phone: +49-(0)721-608 3790 Institut fuer Nachrichtentechnik Fax: +49-(0)721-608 6071 Universitaet Karlsruhe (TH) http://www.int.uni-karlsruhe.de/
Index: gnuradio-core/src/python/gnuradio/gr/qa_moving_average_vXX.py =================================================================== --- gnuradio-core/src/python/gnuradio/gr/qa_moving_average_vXX.py (revision 0) +++ gnuradio-core/src/python/gnuradio/gr/qa_moving_average_vXX.py (revision 0) @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# +# Copyright 2007 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. +# + +from gnuradio import gr, gr_unittest + +class test_moving_average_vector (gr_unittest.TestCase): + + def setUp (self): + self.tb = gr.top_block () + + + def tearDown (self): + self.tb = None + + + def test_001(self): + src_data = (1,1,0, 2,2,0, 3,3,0, 4,4,0, 5,5,0) + vlen = 3 + MAlen = 3 + expected_result = (1.0/3,1.0/3,0, 1,1,0, 2,2,0, 3,3,0, 4,4,0) + + src = gr.vector_source_f(src_data, False, vlen) + MA = gr.moving_average_vff(MAlen, vlen, 1.0/3) + dst = gr.vector_sink_f(vlen) + + self.tb.connect(src, MA, dst) + self.tb.run() + result_data = dst.data() + self.assertFloatTuplesAlmostEqual(expected_result, result_data) + + +if __name__ == '__main__': + gr_unittest.main () + Property changes on: gnuradio-core/src/python/gnuradio/gr/qa_moving_average_vXX.py ___________________________________________________________________ Name: svn:executable + * Index: gnuradio-core/src/python/gnuradio/gr/Makefile.am =================================================================== --- gnuradio-core/src/python/gnuradio/gr/Makefile.am (revision 10467) +++ gnuradio-core/src/python/gnuradio/gr/Makefile.am (working copy) @@ -95,4 +95,5 @@ qa_unpack_k_bits.py \ qa_repeat.py \ qa_scrambler.py \ - qa_vector_sink_source.py + qa_vector_sink_source.py \ + qa_moving_average_vXX.py Index: gnuradio-core/src/lib/gengen/gr_moving_average_vXX.i.t =================================================================== --- gnuradio-core/src/lib/gengen/gr_moving_average_vXX.i.t (revision 0) +++ gnuradio-core/src/lib/gengen/gr_moving_average_vXX.i.t (revision 0) @@ -0,0 +1,33 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008 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. + */ + +// @WARNING@ + +GR_SWIG_BLOCK_MAGIC(gr,@BASE_NAME@); + +...@sptr_name@ gr_ma...@base_name@ (int length, int vlen, @O_TYPE@ scale, int max_iter=4096); + +class @NAME@ : public gr_sync_block +{ +private: + @NAME@ (); +}; Index: gnuradio-core/src/lib/gengen/gr_moving_average_vXX.cc.t =================================================================== --- gnuradio-core/src/lib/gengen/gr_moving_average_vXX.cc.t (revision 0) +++ gnuradio-core/src/lib/gengen/gr_moving_average_vXX.cc.t (revision 0) @@ -0,0 +1,85 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008 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. + */ + +// @WARNING@ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <@n...@.h> +#include <gr_io_signature.h> +#include <vector> + +...@sptr_name@ +gr_ma...@base_name@ (int length, int vlen, @O_TYPE@ scale, int max_iter) +{ + return @SPTR_NAME@ (new @NAME@ (length, vlen, scale, max_iter)); +} + +...@name@::@NAME@ (int length, int vlen, @O_TYPE@ scale, int max_iter) + : gr_sync_block ("@BASE_NAME@", + gr_make_io_signature (1, 1, sizeof (@I_TYPE@) * vlen), + gr_make_io_signature (1, 1, sizeof (@O_TYPE@) * vlen)), + d_length(length), + d_vlen(vlen), + d_scale(scale), + d_max_iter(max_iter) +{ + set_history(length); +} + +...@name@::~...@name@ () +{ +} + +int +...@name@::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + const @I_TYPE@ *in = (const @I_TYPE@ *) input_items[0]; + @O_TYPE@ *out = (@O_TYPE@ *) output_items[0]; + + std::vector<@I_TYPE@> sum(d_vlen, 0); + int num_iter = (noutput_items>d_max_iter) ? d_max_iter : noutput_items; + + for (int i = 0; i < d_length-1 ; i++) { + const @I_TYPE@ *in_vec = &in[i * d_vlen]; + for (int k = 0; k < d_vlen; k++) { + sum[k] += in_vec[k]; + } + } + + for (int i = 0; i < num_iter; i++) { + const @I_TYPE@ *in_vec_old = &in[i * d_vlen]; + const @I_TYPE@ *in_vec_new = &in[(i+d_length-1) * d_vlen]; + @O_TYPE@ *out_vec = &out[i * d_vlen]; + for (int k = 0; k < d_vlen; k++) { + sum[k] += in_vec_new[k]; + out_vec[k] = sum[k] * d_scale; + sum[k] -= in_vec_old[k]; + } + } + + return num_iter; +} Index: gnuradio-core/src/lib/gengen/Makefile.gen =================================================================== --- gnuradio-core/src/lib/gengen/Makefile.gen (revision 10467) +++ gnuradio-core/src/lib/gengen/Makefile.gen (working copy) @@ -46,6 +46,10 @@ gr_moving_average_ff.h \ gr_moving_average_ii.h \ gr_moving_average_ss.h \ + gr_moving_average_vcc.h \ + gr_moving_average_vff.h \ + gr_moving_average_vii.h \ + gr_moving_average_vss.h \ gr_multiply_cc.h \ gr_multiply_const_cc.h \ gr_multiply_const_ff.h \ @@ -156,6 +160,10 @@ gr_moving_average_ff.i \ gr_moving_average_ii.i \ gr_moving_average_ss.i \ + gr_moving_average_vcc.i \ + gr_moving_average_vff.i \ + gr_moving_average_vii.i \ + gr_moving_average_vss.i \ gr_multiply_cc.i \ gr_multiply_const_cc.i \ gr_multiply_const_ff.i \ @@ -266,6 +274,10 @@ gr_moving_average_ff.cc \ gr_moving_average_ii.cc \ gr_moving_average_ss.cc \ + gr_moving_average_vcc.cc \ + gr_moving_average_vff.cc \ + gr_moving_average_vii.cc \ + gr_moving_average_vss.cc \ gr_multiply_cc.cc \ gr_multiply_const_cc.cc \ gr_multiply_const_ff.cc \ Index: gnuradio-core/src/lib/gengen/gengen_generated.i =================================================================== --- gnuradio-core/src/lib/gengen/gengen_generated.i (revision 10467) +++ gnuradio-core/src/lib/gengen/gengen_generated.i (working copy) @@ -46,6 +46,10 @@ #include <gr_moving_average_ff.h> #include <gr_moving_average_ii.h> #include <gr_moving_average_ss.h> +#include <gr_moving_average_vcc.h> +#include <gr_moving_average_vff.h> +#include <gr_moving_average_vii.h> +#include <gr_moving_average_vss.h> #include <gr_multiply_cc.h> #include <gr_multiply_const_cc.h> #include <gr_multiply_const_ff.h> @@ -156,6 +160,10 @@ %include <gr_moving_average_ff.i> %include <gr_moving_average_ii.i> %include <gr_moving_average_ss.i> +%include <gr_moving_average_vcc.i> +%include <gr_moving_average_vff.i> +%include <gr_moving_average_vii.i> +%include <gr_moving_average_vss.i> %include <gr_multiply_cc.i> %include <gr_multiply_const_cc.i> %include <gr_multiply_const_ff.i> Index: gnuradio-core/src/lib/gengen/gr_moving_average_vXX.h.t =================================================================== --- gnuradio-core/src/lib/gengen/gr_moving_average_vXX.h.t (revision 0) +++ gnuradio-core/src/lib/gengen/gr_moving_average_vXX.h.t (revision 0) @@ -0,0 +1,66 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008 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. + */ + +// @WARNING@ + +#ifndef @GUARD_NAME@ +#define @GUARD_NAME@ + +#include <gr_sync_block.h> + +class @NAME@; + +typedef boost::shared_ptr<@NAME@> @SPTR_NAME@; + +...@sptr_name@ gr_ma...@base_name@ (int length, int vlen, @O_TYPE@ scale, int max_iter = 4096); + +/*! + * \brief output is the moving sum of the last N samples, scaled by the \p scale factor. + * + * The moving average of the vectors is calculated per element. + * + * \p max_iter limits how long we go without flushing the accumulator. This is necessary + * to avoid numerical instability for float and complex. + * + * \ingroup filter + */ +class @NAME@ : public gr_sync_block +{ +private: + friend @SPTR_NAME@ gr_ma...@base_name@(int length, int vlen, @O_TYPE@ scale, int max_iter); + + @NAME@ (int length, int vlen, @O_TYPE@ scale, int max_iter = 4096); + + int d_length; + int d_vlen; + @O_TYPE@ d_scale; + int d_max_iter; + +public: + ~...@name@ (); + + int work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; + +#endif /* @GUARD_NAME@ */ Index: gnuradio-core/src/lib/gengen/generate_common.py =================================================================== --- gnuradio-core/src/lib/gengen/generate_common.py (revision 10467) +++ gnuradio-core/src/lib/gengen/generate_common.py (working copy) @@ -53,6 +53,7 @@ 'gr_multiply_const_vXX', 'gr_integrate_XX', 'gr_moving_average_XX', + 'gr_moving_average_vXX' ] # other blocks Index: gnuradio-core/src/lib/gengen/Makefile.am =================================================================== --- gnuradio-core/src/lib/gengen/Makefile.am (revision 10467) +++ gnuradio-core/src/lib/gengen/Makefile.am (working copy) @@ -115,7 +115,10 @@ gr_not_XX.i.t \ gr_moving_average_XX.cc.t \ gr_moving_average_XX.h.t \ - gr_moving_average_XX.i.t + gr_moving_average_XX.i.t \ + gr_moving_average_vXX.cc.t \ + gr_moving_average_vXX.h.t \ + gr_moving_average_vXX.i.t include Makefile.gen
pgpezVWYgHoMn.pgp
Description: PGP signature
_______________________________________________ Patch-gnuradio mailing list Patch-gnuradio@gnu.org http://lists.gnu.org/mailman/listinfo/patch-gnuradio