diff options
author | Tom Rondeau <tom@trondeau.com> | 2013-08-22 17:39:47 -0400 |
---|---|---|
committer | Tom Rondeau <tom@trondeau.com> | 2013-08-26 17:07:10 -0400 |
commit | 52cffbabaef816ba7e723cf53c69a8cfa2a39849 (patch) | |
tree | 95e7fadecd1362b307d7cd2f62f6ba8bfd073e40 | |
parent | 6bf21a5a3371939b8e3e79ac57b33deb333eea8a (diff) |
blocks: adding a complex VCO block.
-rw-r--r-- | gnuradio-runtime/include/gnuradio/fxpt_vco.h | 11 | ||||
-rw-r--r-- | gnuradio-runtime/lib/math/qa_fxpt_vco.cc | 20 | ||||
-rw-r--r-- | gnuradio-runtime/lib/math/vco.h | 14 | ||||
-rw-r--r-- | gr-blocks/grc/blocks_vco_c.xml | 35 | ||||
-rw-r--r-- | gr-blocks/include/gnuradio/blocks/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-blocks/include/gnuradio/blocks/vco_c.h | 59 | ||||
-rw-r--r-- | gr-blocks/lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-blocks/lib/vco_c_impl.cc | 68 | ||||
-rw-r--r-- | gr-blocks/lib/vco_c_impl.h | 53 | ||||
-rw-r--r-- | gr-blocks/python/blocks/qa_vco.py | 23 | ||||
-rw-r--r-- | gr-blocks/swig/blocks_swig5.i | 3 |
11 files changed, 288 insertions, 0 deletions
diff --git a/gnuradio-runtime/include/gnuradio/fxpt_vco.h b/gnuradio-runtime/include/gnuradio/fxpt_vco.h index 77d58677ce..cbc204c1b8 100644 --- a/gnuradio-runtime/include/gnuradio/fxpt_vco.h +++ b/gnuradio-runtime/include/gnuradio/fxpt_vco.h @@ -61,6 +61,17 @@ namespace gr { *cosx = fxpt::cos(d_phase); } + // compute complex sine a block at a time + void sincos(gr_complex *output, const float *input, int noutput_items, + float k, float ampl = 1.0) + { + for(int i = 0; i < noutput_items; i++) { + output[i] = gr_complex((float)(fxpt::cos(d_phase) * ampl), + (float)(fxpt::sin(d_phase) * ampl)); + adjust_phase(input[i] * k); + } + } + // compute a block at a time void cos(float *output, const float *input, int noutput_items, float k, float ampl = 1.0) { diff --git a/gnuradio-runtime/lib/math/qa_fxpt_vco.cc b/gnuradio-runtime/lib/math/qa_fxpt_vco.cc index ee9865e926..8ee402fdc1 100644 --- a/gnuradio-runtime/lib/math/qa_fxpt_vco.cc +++ b/gnuradio-runtime/lib/math/qa_fxpt_vco.cc @@ -102,6 +102,26 @@ qa_fxpt_vco::t1() void qa_fxpt_vco::t2() { + gr::vco<gr_complex,float> ref_vco; + gr::fxpt_vco new_vco; + gr_complex ref_block[SIN_COS_BLOCK_SIZE]; + gr_complex new_block[SIN_COS_BLOCK_SIZE]; + float input[SIN_COS_BLOCK_SIZE]; + double max_error = 0; + + for(int i = 0; i < SIN_COS_BLOCK_SIZE; i++) { + input[i] = sin(double(i)); + } + + ref_vco.sincos(ref_block, input, SIN_COS_BLOCK_SIZE, SIN_COS_K, SIN_COS_AMPL); + new_vco.sincos(new_block, input, SIN_COS_BLOCK_SIZE, SIN_COS_K, SIN_COS_AMPL); + + for(int i = 0; i < SIN_COS_BLOCK_SIZE; i++) { + CPPUNIT_ASSERT_COMPLEXES_EQUAL(ref_block[i], new_block[i], SIN_COS_TOLERANCE); + max_error = max_d(max_error, abs(ref_block[i]-new_block[i])); + } + CPPUNIT_ASSERT_DOUBLES_EQUAL(ref_vco.get_phase(), new_vco.get_phase(), SIN_COS_TOLERANCE); + // printf ("Fxpt max error %.9f, max phase error %.9f\n", max_error, ref_vco.get_phase()-new_vco.get_phase()); } void diff --git a/gnuradio-runtime/lib/math/vco.h b/gnuradio-runtime/lib/math/vco.h index fa11732c1f..d8a6fbb415 100644 --- a/gnuradio-runtime/lib/math/vco.h +++ b/gnuradio-runtime/lib/math/vco.h @@ -64,6 +64,9 @@ namespace gr { // compute sin and cos for current phase angle void sincos(float *sinx, float *cosx) const; + void sincos(gr_complex *output, const float *input, + int noutput_items, double k, double ampl = 1.0); + // compute cos or sin for current phase angle float cos() const { return std::cos(d_phase); } float sin() const { return std::sin(d_phase); } @@ -85,6 +88,17 @@ namespace gr { template<class o_type, class i_type> void + vco<o_type,i_type>::sincos(gr_complex *output, const float *input, + int noutput_items, double k, double ampl) + { + for(int i = 0; i < noutput_items; i++) { + output[i] = gr_complex(cos() * ampl, sin() * ampl); + adjust_phase(input[i] * k); + } + } + + template<class o_type, class i_type> + void vco<o_type,i_type>::cos(float *output, const float *input, int noutput_items, double k, double ampl) { diff --git a/gr-blocks/grc/blocks_vco_c.xml b/gr-blocks/grc/blocks_vco_c.xml new file mode 100644 index 0000000000..f6246441b9 --- /dev/null +++ b/gr-blocks/grc/blocks_vco_c.xml @@ -0,0 +1,35 @@ +<?xml version="1.0"?> +<!-- +################################################### +##VCO +################################################### + --> +<block> + <name>VCO (complex)</name> + <key>blocks_vco_c</key> + <import>from gnuradio import blocks</import> + <make>blocks.vco_c($samp_rate, $sensitivity, $amplitude)</make> + <param> + <name>Sample Rate</name> + <key>samp_rate</key> + <type>real</type> + </param> + <param> + <name>Sensitivity</name> + <key>sensitivity</key> + <type>real</type> + </param> + <param> + <name>Amplitude</name> + <key>amplitude</key> + <type>real</type> + </param> + <sink> + <name>in</name> + <type>float</type> + </sink> + <source> + <name>out</name> + <type>complex</type> + </source> +</block> diff --git a/gr-blocks/include/gnuradio/blocks/CMakeLists.txt b/gr-blocks/include/gnuradio/blocks/CMakeLists.txt index 88ee184179..de52e3e3e7 100644 --- a/gr-blocks/include/gnuradio/blocks/CMakeLists.txt +++ b/gr-blocks/include/gnuradio/blocks/CMakeLists.txt @@ -202,6 +202,7 @@ install(FILES udp_source.h unpack_k_bits_bb.h vco_f.h + vco_c.h vector_map.h vector_to_stream.h vector_to_streams.h diff --git a/gr-blocks/include/gnuradio/blocks/vco_c.h b/gr-blocks/include/gnuradio/blocks/vco_c.h new file mode 100644 index 0000000000..ab9723af5b --- /dev/null +++ b/gr-blocks/include/gnuradio/blocks/vco_c.h @@ -0,0 +1,59 @@ +/* -*- c++ -*- */ +/* + * Copyright 2013 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_GR_VCO_C_H +#define INCLUDED_GR_VCO_C_H + +#include <gnuradio/blocks/api.h> +#include <gnuradio/sync_block.h> + +namespace gr { + namespace blocks { + + /*! + * \brief VCO - Voltage controlled oscillator + * \ingroup modulators_blk + * \ingroup waveform_generators_blk + * + * \details + * input: float stream of control voltages; output: complex oscillator output + */ + class BLOCKS_API vco_c : virtual public sync_block + { + public: + // gr::blocks::vco_c::sptr + typedef boost::shared_ptr<vco_c> sptr; + + /*! + * \brief VCO - Voltage controlled oscillator + * + * \param sampling_rate sampling rate (Hz) + * \param sensitivity units are radians/sec/volt + * \param amplitude output amplitude + */ + static sptr make(double sampling_rate, double sensitivity, double amplitude); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_VCO_C_H */ diff --git a/gr-blocks/lib/CMakeLists.txt b/gr-blocks/lib/CMakeLists.txt index 987164fdf7..1c7bd80ae0 100644 --- a/gr-blocks/lib/CMakeLists.txt +++ b/gr-blocks/lib/CMakeLists.txt @@ -244,6 +244,7 @@ list(APPEND gr_blocks_sources udp_source_impl.cc unpack_k_bits_bb_impl.cc vco_f_impl.cc + vco_c_impl.cc vector_map_impl.cc vector_to_stream_impl.cc vector_to_streams_impl.cc diff --git a/gr-blocks/lib/vco_c_impl.cc b/gr-blocks/lib/vco_c_impl.cc new file mode 100644 index 0000000000..5103ce1851 --- /dev/null +++ b/gr-blocks/lib/vco_c_impl.cc @@ -0,0 +1,68 @@ +/* -*- c++ -*- */ +/* + * Copyright 2013 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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "vco_c_impl.h" +#include <gnuradio/io_signature.h> +#include <math.h> + +namespace gr { + namespace blocks { + + vco_c::sptr + vco_c::make(double sampling_rate, double sensitivity, double amplitude) + { + return gnuradio::get_initial_sptr + (new vco_c_impl(sampling_rate, sensitivity, amplitude)); + } + + vco_c_impl::vco_c_impl(double sampling_rate, double sensitivity, double amplitude) + : sync_block("vco_c", + io_signature::make(1, 1, sizeof(float)), + io_signature::make(1, 1, sizeof(gr_complex))), + d_sampling_rate(sampling_rate), d_sensitivity(sensitivity), + d_amplitude(amplitude), d_k(d_sensitivity/d_sampling_rate) + { + } + + vco_c_impl::~vco_c_impl() + { + } + + int + vco_c_impl::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]; + gr_complex *output = (gr_complex*)output_items[0]; + + d_vco.sincos(output, input, noutput_items, d_k, d_amplitude); + + return noutput_items; + } + + } /* namespace blocks */ +} /* namespace gr */ diff --git a/gr-blocks/lib/vco_c_impl.h b/gr-blocks/lib/vco_c_impl.h new file mode 100644 index 0000000000..53def4f1da --- /dev/null +++ b/gr-blocks/lib/vco_c_impl.h @@ -0,0 +1,53 @@ +/* -*- c++ -*- */ +/* + * Copyright 2013 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_GR_VCO_F_IMPL_H +#define INCLUDED_GR_VCO_F_IMPL_H + +#include <gnuradio/blocks/vco_c.h> +#include <gnuradio/fxpt_vco.h> + +namespace gr { + namespace blocks { + + class vco_c_impl : public vco_c + { + private: + double d_sampling_rate; + double d_sensitivity; + double d_amplitude; + double d_k; + gr::fxpt_vco d_vco; + + public: + vco_c_impl(double sampling_rate, double sensitivity, double amplitude); + ~vco_c_impl(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_VCO_C_H */ diff --git a/gr-blocks/python/blocks/qa_vco.py b/gr-blocks/python/blocks/qa_vco.py index 4ed2a71c04..fdd1eb1001 100644 --- a/gr-blocks/python/blocks/qa_vco.py +++ b/gr-blocks/python/blocks/qa_vco.py @@ -28,6 +28,12 @@ def sig_source_f(samp_rate, freq, amp, N): y = map(lambda x: amp*math.cos(2.*math.pi*freq*x), t) return y +def sig_source_c(samp_rate, freq, amp, N): + t = map(lambda x: float(x)/samp_rate, xrange(N)) + y = map(lambda x: math.cos(2.*math.pi*freq*x) + \ + 1j*math.sin(2.*math.pi*freq*x), t) + return y + class test_vco(gr_unittest.TestCase): def setUp (self): @@ -53,6 +59,23 @@ class test_vco(gr_unittest.TestCase): self.assertFloatTuplesAlmostEqual(expected_result, result_data, 5) + def test_002(self): + src_data = 200*[0,] + 200*[0.5,] + 200*[1,] + expected_result = 200*[1,] + \ + sig_source_c(1, 0.125, 1, 200) + \ + sig_source_c(1, 0.25, 1, 200) + + src = blocks.vector_source_f(src_data) + op = blocks.vco_c(1, math.pi/2.0, 1) + dst = blocks.vector_sink_c() + + self.tb.connect(src, op, dst) + self.tb.run() + + result_data = dst.data() + self.assertComplexTuplesAlmostEqual(expected_result, result_data, 5) + + if __name__ == '__main__': gr_unittest.run(test_vco, "test_vco.xml") diff --git a/gr-blocks/swig/blocks_swig5.i b/gr-blocks/swig/blocks_swig5.i index 2b19d8ec29..09679531d9 100644 --- a/gr-blocks/swig/blocks_swig5.i +++ b/gr-blocks/swig/blocks_swig5.i @@ -64,6 +64,7 @@ #include "gnuradio/blocks/unpacked_to_packed_ss.h" #include "gnuradio/blocks/unpacked_to_packed_ii.h" #include "gnuradio/blocks/vco_f.h" +#include "gnuradio/blocks/vco_c.h" #include "gnuradio/blocks/xor_bb.h" #include "gnuradio/blocks/xor_ss.h" #include "gnuradio/blocks/xor_ii.h" @@ -102,6 +103,7 @@ %include "gnuradio/blocks/unpacked_to_packed_ss.h" %include "gnuradio/blocks/unpacked_to_packed_ii.h" %include "gnuradio/blocks/vco_f.h" +%include "gnuradio/blocks/vco_c.h" %include "gnuradio/blocks/xor_bb.h" %include "gnuradio/blocks/xor_ss.h" %include "gnuradio/blocks/xor_ii.h" @@ -139,6 +141,7 @@ GR_SWIG_BLOCK_MAGIC2(blocks, unpacked_to_packed_bb); GR_SWIG_BLOCK_MAGIC2(blocks, unpacked_to_packed_ss); GR_SWIG_BLOCK_MAGIC2(blocks, unpacked_to_packed_ii); GR_SWIG_BLOCK_MAGIC2(blocks, vco_f); +GR_SWIG_BLOCK_MAGIC2(blocks, vco_c); GR_SWIG_BLOCK_MAGIC2(blocks, xor_bb); GR_SWIG_BLOCK_MAGIC2(blocks, xor_ss); GR_SWIG_BLOCK_MAGIC2(blocks, xor_ii); |