diff options
author | ghostop14 <ghostop14@gmail.com> | 2020-03-02 17:48:46 -0500 |
---|---|---|
committer | mormj <34754695+mormj@users.noreply.github.com> | 2020-03-30 09:57:31 -0400 |
commit | 6f18944f258d30a0038ab377532562f319d687cf (patch) | |
tree | b9d6e1daed61c820d880a078f9f5a9363c36c40d /gr-blocks/lib | |
parent | 553b51e295e6b9e03108fdae26a17679f71992d2 (diff) |
gr-blocks: Add Phase Shift Block with Msg Capabilities
This new block provides a native ability to phase shift signals
for solutions such as DoA and array-based projects. The block
supports both variable and message based updates such that
other blocks can provide the appropriate calculations for shifting
and only send message-based updates when necessary. Based on a
block configuration parameter, shift can be specified either
in radians or degrees. his block functions like a multiply const,
but with the const limited to abs() == 1 to provide a constant
phase shift.
Diffstat (limited to 'gr-blocks/lib')
-rw-r--r-- | gr-blocks/lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-blocks/lib/phase_shift_impl.cc | 97 | ||||
-rw-r--r-- | gr-blocks/lib/phase_shift_impl.h | 43 |
3 files changed, 141 insertions, 0 deletions
diff --git a/gr-blocks/lib/CMakeLists.txt b/gr-blocks/lib/CMakeLists.txt index 2f27e22f5e..8c9dd9aa6a 100644 --- a/gr-blocks/lib/CMakeLists.txt +++ b/gr-blocks/lib/CMakeLists.txt @@ -35,6 +35,7 @@ add_library(gnuradio-blocks not_blk_impl.cc packed_to_unpacked_impl.cc peak_detector_impl.cc + phase_shift_impl.cc probe_signal_impl.cc probe_signal_v_impl.cc sample_and_hold_impl.cc diff --git a/gr-blocks/lib/phase_shift_impl.cc b/gr-blocks/lib/phase_shift_impl.cc new file mode 100644 index 0000000000..b81b78d6c4 --- /dev/null +++ b/gr-blocks/lib/phase_shift_impl.cc @@ -0,0 +1,97 @@ +/* -*- c++ -*- */ +/* + * Copyright 2020 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "phase_shift_impl.h" +#include <gnuradio/io_signature.h> + +#include <gnuradio/math.h> +#include <volk/volk.h> + +namespace gr { +namespace blocks { + +phase_shift::sptr phase_shift::make(float shift, bool is_radians) +{ + return gnuradio::get_initial_sptr(new phase_shift_impl(shift, is_radians)); +} + +/* + * The private constructor + */ +phase_shift_impl::phase_shift_impl(float shift, bool is_radians) + : gr::sync_block("phase_shift", + gr::io_signature::make(1, 1, sizeof(gr_complex)), + gr::io_signature::make(1, 1, sizeof(gr_complex))), + d_is_radians(is_radians) +{ + set_shift(shift); + + message_port_register_in(pmt::mp("shift")); + set_msg_handler(pmt::mp("shift"), + [this](pmt::pmt_t msg) { this->handle_msg_in(msg); }); +} + +/* + * Our virtual destructor. + */ +phase_shift_impl::~phase_shift_impl() {} + +void phase_shift_impl::handle_msg_in(pmt::pmt_t msg) +{ + if (pmt::is_number(msg)) { + set_shift(pmt::to_float(msg)); + } else { + if (pmt::is_pair(msg)) { + pmt::pmt_t data = pmt::cdr(msg); + if (pmt::is_number(data)) { + set_shift(pmt::to_float(data)); + } else + GR_LOG_WARN( + d_logger, + "Phase message must be a number or a number pair. Ignoring value."); + } + } +} + +void phase_shift_impl::set_shift(float new_value) +{ + gr::thread::scoped_lock guard(d_setlock); + if (d_is_radians) + d_shift = new_value; + else + d_shift = new_value * GR_M_PI / 180.0; // convert to radians + + d_shift_cc = gr_complex(cos(d_shift), sin(d_shift)); +} + +int phase_shift_impl::work(int noutput_items, + gr_vector_const_void_star& input_items, + gr_vector_void_star& output_items) +{ + const gr_complex* in = (const gr_complex*)input_items[0]; + gr_complex* out = (gr_complex*)output_items[0]; + + gr::thread::scoped_lock guard(d_setlock); + + if (d_shift != 0.0f) { + volk_32fc_s32fc_multiply_32fc(out, in, d_shift_cc, noutput_items); + } else { + memcpy(out, in, sizeof(gr_complex) * noutput_items); + } + + return noutput_items; +} + +} /* namespace blocks */ +} /* namespace gr */ diff --git a/gr-blocks/lib/phase_shift_impl.h b/gr-blocks/lib/phase_shift_impl.h new file mode 100644 index 0000000000..f0d33a87d9 --- /dev/null +++ b/gr-blocks/lib/phase_shift_impl.h @@ -0,0 +1,43 @@ +/* -*- c++ -*- */ +/* + * Copyright 2020 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifndef INCLUDED_PHASE_SHIFT_IMPL_H +#define INCLUDED_PHASE_SHIFT_IMPL_H + +#include <gnuradio/blocks/phase_shift.h> + +namespace gr { +namespace blocks { + +class phase_shift_impl : public phase_shift +{ +private: + bool d_is_radians; + float d_shift; + gr_complex d_shift_cc; + +public: + phase_shift_impl(float shift, bool is_radians); + ~phase_shift_impl(); + + virtual float get_shift() const { return d_shift; } + virtual void set_shift(float new_value); + + void handle_msg_in(pmt::pmt_t msg); + + int work(int noutput_items, + gr_vector_const_void_star& input_items, + gr_vector_void_star& output_items); +}; + +} // namespace blocks +} // namespace gr + +#endif /* INCLUDED_PHASE_SHIFT_IMPL_H */ |