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/phase_shift_impl.cc | |
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/phase_shift_impl.cc')
-rw-r--r-- | gr-blocks/lib/phase_shift_impl.cc | 97 |
1 files changed, 97 insertions, 0 deletions
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 */ |