diff options
author | Christoph Mayer <hcab14@gmail.com> | 2019-05-23 10:58:51 +0000 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2019-06-15 17:17:02 -0700 |
commit | 6996d1b7ee20fb7611068906bb64886eb5b514ef (patch) | |
tree | d121f1ba55aaea456906aca4f46c66b0be83bf6e | |
parent | 565d133ff028b316a9d920e631df0bd38cd7bd1e (diff) |
fft: Add fft_shift class
This is a generic FFT shift object. Includes C++ unit tests.
The class is templated and thus works with FFTs of any type.
-rw-r--r-- | gr-fft/include/gnuradio/fft/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-fft/include/gnuradio/fft/fft_shift.h | 82 | ||||
-rw-r--r-- | gr-fft/lib/CMakeLists.txt | 15 | ||||
-rw-r--r-- | gr-fft/lib/qa_fft_shift.cc | 62 | ||||
-rw-r--r-- | gr-qtgui/lib/freq_sink_c_impl.cc | 28 | ||||
-rw-r--r-- | gr-qtgui/lib/freq_sink_c_impl.h | 6 | ||||
-rw-r--r-- | gr-qtgui/lib/freq_sink_f_impl.cc | 24 | ||||
-rw-r--r-- | gr-qtgui/lib/freq_sink_f_impl.h | 6 | ||||
-rw-r--r-- | gr-qtgui/lib/waterfall_sink_c_impl.cc | 20 | ||||
-rw-r--r-- | gr-qtgui/lib/waterfall_sink_c_impl.h | 4 | ||||
-rw-r--r-- | gr-qtgui/lib/waterfall_sink_f_impl.cc | 20 | ||||
-rw-r--r-- | gr-qtgui/lib/waterfall_sink_f_impl.h | 4 |
12 files changed, 199 insertions, 73 deletions
diff --git a/gr-fft/include/gnuradio/fft/CMakeLists.txt b/gr-fft/include/gnuradio/fft/CMakeLists.txt index 0f52b0aa96..2bd77b8d3c 100644 --- a/gr-fft/include/gnuradio/fft/CMakeLists.txt +++ b/gr-fft/include/gnuradio/fft/CMakeLists.txt @@ -23,6 +23,7 @@ install(FILES api.h fft.h + fft_shift.h fft_vcc.h fft_vfc.h goertzel.h diff --git a/gr-fft/include/gnuradio/fft/fft_shift.h b/gr-fft/include/gnuradio/fft/fft_shift.h new file mode 100644 index 0000000000..1ee08915b9 --- /dev/null +++ b/gr-fft/include/gnuradio/fft/fft_shift.h @@ -0,0 +1,82 @@ +/* -*- c++ -*- */ +/* + * Copyright 2019 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_FFT_FFT_SHIFT_H +#define INCLUDED_FFT_FFT_SHIFT_H + +#include <algorithm> +#include <cassert> +#include <vector> + +namespace gr { +namespace fft { + + /*! \brief reorder FFT results which are ordered from 0 to 1 in normalized frequency + * to -0.5 to 0.5 by cyclic shift + */ + template<typename T> + class fft_shift { + public: + fft_shift(size_t fft_length) + : d_fftlen(fft_length) + , d_lenpos(fft_length/2 + (fft_length%2)) + , d_lenneg(fft_length/2) + , d_buf(fft_length) {} + + /*! performs the cyclic shift on a vector v + */ + void shift(std::vector<T>& v) { + shift(&v.front(), v.size()); + } + + /*! performs the cyclic shift on an array + */ + void shift(T* data, size_t fft_len) { + resize(fft_len); + std::copy_n(data, d_lenpos, d_buf.begin()); + std::copy_n(data+d_lenpos, d_lenneg, data); + std::copy_n(d_buf.begin(), d_lenpos, data+d_lenneg); + } + + /*! if needed adjusts the buffer size to a new fft length + */ + void resize(size_t fft_length) { + if (d_fftlen == fft_length) + return; + d_fftlen = fft_length; + d_lenpos = d_fftlen/2 + (d_fftlen%2); + d_lenneg = d_fftlen/2; + assert(d_lenpos + d_lenneg == d_fftlen); + d_buf.resize(d_lenpos); + } + protected: + + private: + size_t d_fftlen; // FFT length + size_t d_lenpos; // number of FFT bins with positive frequencies + size_t d_lenneg; // number of FFT bins with negative frequencies + std::vector<T> d_buf; // buffer used for cyclic shift + } ; + +} // namespace fft +} // namespace gr +#endif // INCLUDED_FFT_FFT_SHIFT_H diff --git a/gr-fft/lib/CMakeLists.txt b/gr-fft/lib/CMakeLists.txt index 7b8207665e..14541fcffc 100644 --- a/gr-fft/lib/CMakeLists.txt +++ b/gr-fft/lib/CMakeLists.txt @@ -65,3 +65,18 @@ endif(MSVC) if(BUILD_SHARED_LIBS) GR_LIBRARY_FOO(gnuradio-fft FFTW3f) endif() + +if(ENABLE_TESTING) + include(GrTest) + + list(APPEND test_gr_fft_sources + qa_fft_shift + ) + list(APPEND GR_TEST_TARGET_DEPS gnuradio-fft) + + foreach(qa_file ${test_gr_fft_sources}) + GR_ADD_CPP_TEST("fft_${qa_file}" + ${CMAKE_CURRENT_SOURCE_DIR}/${qa_file} + ) + endforeach(qa_file) +endif(ENABLE_TESTING) diff --git a/gr-fft/lib/qa_fft_shift.cc b/gr-fft/lib/qa_fft_shift.cc new file mode 100644 index 0000000000..e924af8629 --- /dev/null +++ b/gr-fft/lib/qa_fft_shift.cc @@ -0,0 +1,62 @@ +/* -*- c++ -*- */ +/* + * Copyright 2019 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 <gnuradio/fft/fft_shift.h> +#include <boost/test/unit_test.hpp> +#include <vector> + +namespace gr { +namespace fft { + +BOOST_AUTO_TEST_CASE(t1) +{ + fft::fft_shift<int> s(1023); + + std::vector<int> x_even{ 0, 1, 2, 3, -4, -3, -2, -1 }; + std::vector<int> y_even{ -4, -3, -2, -1, 0, 1, 2, 3 }; // expected result + + s.shift(x_even); + BOOST_TEST(x_even == y_even, boost::test_tools::per_element()); + + // two shifts should not change the result + s.shift(x_even); + s.shift(x_even); + BOOST_TEST(x_even == y_even, boost::test_tools::per_element()); +} + +BOOST_AUTO_TEST_CASE(t2) +{ + fft::fft_shift<int> s(7); + + std::vector<int> x_odd{ 0, 1, 2, 3, -3, -2, -1 }; + std::vector<int> y_odd{ -3, -2, -1, 0, 1, 2, 3 }; // expected result + + s.shift(x_odd); + BOOST_TEST(x_odd == y_odd, boost::test_tools::per_element()); +} + +} /* namespace fft */ +} /* namespace gr */ diff --git a/gr-qtgui/lib/freq_sink_c_impl.cc b/gr-qtgui/lib/freq_sink_c_impl.cc index e94dae6074..3f31c90975 100644 --- a/gr-qtgui/lib/freq_sink_c_impl.cc +++ b/gr-qtgui/lib/freq_sink_c_impl.cc @@ -59,7 +59,9 @@ namespace gr { : sync_block("freq_sink_c", io_signature::make(0, nconnections, sizeof(gr_complex)), io_signature::make(0, 0, 0)), - d_fftsize(fftsize), d_fftavg(1.0), + d_fftsize(fftsize), + d_fft_shift(fftsize), + d_fftavg(1.0), d_wintype((filter::firdes::win_type)(wintype)), d_center_freq(fc), d_bandwidth(bw), d_name(name), d_nconnections(nconnections), @@ -103,11 +105,6 @@ namespace gr { volk_get_alignment()); memset(d_fbuf, 0, d_fftsize*sizeof(float)); - d_tmpbuflen = (unsigned int)(floor(d_fftsize/2.0)); - d_tmpbuf = (float*)volk_malloc(sizeof(float)*(d_tmpbuflen + 1), - volk_get_alignment()); - - d_index = 0; // save the last "connection" for the PDU memory for(int i = 0; i < d_nconnections; i++) { @@ -147,7 +144,6 @@ namespace gr { } delete d_fft; volk_free(d_fbuf); - volk_free(d_tmpbuf); delete d_argv; } @@ -503,18 +499,7 @@ namespace gr { volk_32fc_s32f_x2_power_spectral_density_32f(data_out, d_fft->get_outbuf(), size, 1.0, size); - // Perform shift operation - // size=8, d_tmpbuflen=4: - // freqs=[0.00000 0.12500 0.25000 0.37500 -0.50000 -0.37500 -0.25000 -0.12500] - // size=7, d_tmpbuflen=3: - // freqa=[0.00000 0.14286 0.28571 0.42857 -0.42857 -0.28571 -0.14286] - const int off = size%2; - const int len = d_tmpbuflen; - assert(len+off + len == size); - // | pos.| |neg.| frequencies - std::copy_n(data_out, len+off, d_tmpbuf); - std::copy_n(data_out+len+off, len, data_out); - std::copy_n(d_tmpbuf, len+off, data_out+len); + d_fft_shift.shift(data_out, size); } bool @@ -585,10 +570,7 @@ namespace gr { volk_get_alignment()); memset(d_fbuf, 0, d_fftsize*sizeof(float)); - volk_free(d_tmpbuf); - d_tmpbuflen = (unsigned int)(floor(d_fftsize/2.0)); - d_tmpbuf = (float*)volk_malloc(sizeof(float)*(d_tmpbuflen + 1), - volk_get_alignment()); + d_fft_shift.resize(d_fftsize); d_last_time = 0; diff --git a/gr-qtgui/lib/freq_sink_c_impl.h b/gr-qtgui/lib/freq_sink_c_impl.h index 842b525193..09f8eaf388 100644 --- a/gr-qtgui/lib/freq_sink_c_impl.h +++ b/gr-qtgui/lib/freq_sink_c_impl.h @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2012,2015 Free Software Foundation, Inc. + * Copyright 2012,2015,2019 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -27,6 +27,7 @@ #include <gnuradio/filter/firdes.h> #include <gnuradio/fft/fft.h> +#include <gnuradio/fft/fft_shift.h> #include <gnuradio/high_res_timer.h> #include <gnuradio/qtgui/freqdisplayform.h> @@ -39,7 +40,7 @@ namespace gr { void initialize(); int d_fftsize; - int d_tmpbuflen; + fft::fft_shift<float> d_fft_shift; float d_fftavg; filter::firdes::win_type d_wintype; std::vector<float> d_window; @@ -59,7 +60,6 @@ namespace gr { std::vector<double*> d_magbufs; double* d_pdu_magbuf; float *d_fbuf; - float *d_tmpbuf; int d_argc; char *d_argv; diff --git a/gr-qtgui/lib/freq_sink_f_impl.cc b/gr-qtgui/lib/freq_sink_f_impl.cc index 86ce4b2c4e..efc03f5f8e 100644 --- a/gr-qtgui/lib/freq_sink_f_impl.cc +++ b/gr-qtgui/lib/freq_sink_f_impl.cc @@ -37,6 +37,7 @@ namespace gr { namespace qtgui { + freq_sink_f::sptr freq_sink_f::make(int fftsize, int wintype, double fc, double bw, @@ -59,7 +60,9 @@ namespace gr { : sync_block("freq_sink_f", io_signature::make(0, nconnections, sizeof(float)), io_signature::make(0, 0, 0)), - d_fftsize(fftsize), d_fftavg(1.0), + d_fftsize(fftsize), + d_fft_shift(fftsize), + d_fftavg(1.0), d_wintype((filter::firdes::win_type)(wintype)), d_center_freq(fc), d_bandwidth(bw), d_name(name), d_nconnections(nconnections), @@ -103,10 +106,6 @@ namespace gr { volk_get_alignment()); memset(d_fbuf, 0, d_fftsize*sizeof(float)); - d_tmpbuflen = (unsigned int)(floor(d_fftsize/2.0)); - d_tmpbuf = (float*)volk_malloc(sizeof(float)*(d_tmpbuflen + 1), - volk_get_alignment()); - d_index = 0; // save the last "connection" for the PDU memory for(int i = 0; i < d_nconnections; i++) { @@ -146,7 +145,6 @@ namespace gr { } delete d_fft; volk_free(d_fbuf); - volk_free(d_tmpbuf); delete d_argv; } @@ -508,14 +506,7 @@ namespace gr { volk_32fc_s32f_x2_power_spectral_density_32f(data_out, d_fft->get_outbuf(), size, 1.0, size); - // Perform shift operation - const int off = size%2; - const int len = d_tmpbuflen; - assert(len+off + len == size); - // | pos.| |neg.| frequencies - std::copy_n(data_out, len+off, d_tmpbuf); - std::copy_n(data_out+len+off, len, data_out); - std::copy_n(d_tmpbuf, len+off, data_out+len); + d_fft_shift.shift(data_out, size); } bool @@ -586,10 +577,7 @@ namespace gr { volk_get_alignment()); memset(d_fbuf, 0, d_fftsize*sizeof(float)); - volk_free(d_tmpbuf); - d_tmpbuflen = (unsigned int)(floor(d_fftsize/2.0)); - d_tmpbuf = (float*)volk_malloc(sizeof(float)*(d_tmpbuflen + 1), - volk_get_alignment()); + d_fft_shift.resize(d_fftsize); d_last_time = 0; diff --git a/gr-qtgui/lib/freq_sink_f_impl.h b/gr-qtgui/lib/freq_sink_f_impl.h index 9b656f6e57..4214943c73 100644 --- a/gr-qtgui/lib/freq_sink_f_impl.h +++ b/gr-qtgui/lib/freq_sink_f_impl.h @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2012,2014-2015 Free Software Foundation, Inc. + * Copyright 2012,2014-2015,2019 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -27,6 +27,7 @@ #include <gnuradio/filter/firdes.h> #include <gnuradio/fft/fft.h> +#include <gnuradio/fft/fft_shift.h> #include <gnuradio/high_res_timer.h> #include <gnuradio/qtgui/freqdisplayform.h> @@ -39,7 +40,7 @@ namespace gr { void initialize(); int d_fftsize; - int d_tmpbuflen; + fft::fft_shift<float> d_fft_shift; float d_fftavg; filter::firdes::win_type d_wintype; std::vector<float> d_window; @@ -59,7 +60,6 @@ namespace gr { std::vector<double*> d_magbufs; double* d_pdu_magbuf; float *d_fbuf; - float *d_tmpbuf; int d_argc; char *d_argv; diff --git a/gr-qtgui/lib/waterfall_sink_c_impl.cc b/gr-qtgui/lib/waterfall_sink_c_impl.cc index cf178eadb2..eac4badce5 100644 --- a/gr-qtgui/lib/waterfall_sink_c_impl.cc +++ b/gr-qtgui/lib/waterfall_sink_c_impl.cc @@ -60,7 +60,9 @@ namespace gr { : sync_block("waterfall_sink_c", io_signature::make(0, nconnections, sizeof(gr_complex)), io_signature::make(0, 0, 0)), - d_fftsize(fftsize), d_fftavg(1.0), + d_fftsize(fftsize), + d_fft_shift(fftsize), + d_fftavg(1.0), d_wintype((filter::firdes::win_type)(wintype)), d_center_freq(fc), d_bandwidth(bw), @@ -116,7 +118,7 @@ namespace gr { // setup bw input port message_port_register_in(d_port_bw); set_msg_handler(d_port_bw, - boost::bind(&waterfall_sink_c_impl::handle_set_bw, this, _1)); + boost::bind(&waterfall_sink_c_impl::handle_set_bw, this, _1)); // setup output message port to post frequency when display is // double-clicked @@ -405,15 +407,7 @@ namespace gr { volk_32fc_s32f_x2_power_spectral_density_32f(data_out, d_fft->get_outbuf(), size, 1.0, size); - // Perform shift operation - const int len = (unsigned int)(floor(size/2.0)); - const int off = size%2; - assert(len+off + len == size); - // | pos.| |neg.| frequencies - const std::vector<float> tmp(data_out, data_out+len+off); - // std::copy_n(data_out, len+off, tmp.begin()); - std::copy_n(data_out+len+off, len, data_out); - std::copy_n(tmp.begin(), len+off, data_out+len); + d_fft_shift.shift(data_out, size); } void @@ -487,6 +481,8 @@ namespace gr { delete d_fft; d_fft = new fft::fft_complex(d_fftsize, true); + d_fft_shift.resize(d_fftsize); + volk_free(d_fbuf); d_fbuf = (float*)volk_malloc(d_fftsize*sizeof(float), volk_get_alignment()); @@ -531,7 +527,7 @@ namespace gr { new SetFreqEvent(d_center_freq, d_bandwidth)); } } - } + } void waterfall_sink_c_impl::set_time_per_fft(double t) diff --git a/gr-qtgui/lib/waterfall_sink_c_impl.h b/gr-qtgui/lib/waterfall_sink_c_impl.h index e0d421e36b..6eba6f8ede 100644 --- a/gr-qtgui/lib/waterfall_sink_c_impl.h +++ b/gr-qtgui/lib/waterfall_sink_c_impl.h @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2012,2015 Free Software Foundation, Inc. + * Copyright 2012,2015,2019 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -27,6 +27,7 @@ #include <gnuradio/filter/firdes.h> #include <gnuradio/fft/fft.h> +#include <gnuradio/fft/fft_shift.h> #include <gnuradio/high_res_timer.h> #include <gnuradio/qtgui/waterfalldisplayform.h> @@ -41,6 +42,7 @@ namespace gr { void initialize(); int d_fftsize; + fft::fft_shift<float> d_fft_shift; float d_fftavg; filter::firdes::win_type d_wintype; std::vector<float> d_window; diff --git a/gr-qtgui/lib/waterfall_sink_f_impl.cc b/gr-qtgui/lib/waterfall_sink_f_impl.cc index b93a5663aa..ad466b7c17 100644 --- a/gr-qtgui/lib/waterfall_sink_f_impl.cc +++ b/gr-qtgui/lib/waterfall_sink_f_impl.cc @@ -59,7 +59,9 @@ namespace gr { : sync_block("waterfall_sink_f", io_signature::make(0, nconnections, sizeof(float)), io_signature::make(0, 0, 0)), - d_fftsize(fftsize), d_fftavg(1.0), + d_fftsize(fftsize), + d_fft_shift(fftsize), + d_fftavg(1.0), d_wintype((filter::firdes::win_type)(wintype)), d_center_freq(fc), d_bandwidth(bw), d_name(name), d_nconnections(nconnections), d_nrows(200), @@ -112,7 +114,7 @@ namespace gr { // setup bw input port message_port_register_in(d_port_bw); set_msg_handler(d_port_bw, - boost::bind(&waterfall_sink_f_impl::handle_set_bw, this, _1)); + boost::bind(&waterfall_sink_f_impl::handle_set_bw, this, _1)); // setup output message port to post frequency when display is // double-clicked @@ -409,15 +411,7 @@ namespace gr { volk_32fc_s32f_x2_power_spectral_density_32f(data_out, d_fft->get_outbuf(), size, 1.0, size); - // Perform shift operation - const int len = (int)(floor(size/2.0)); - const int off = size%2; - assert(len+off + len == size); - // | pos.| |neg.| frequencies - const std::vector<float> tmp(data_out, data_out+len+off); - // std::copy_n(data_out, len+off, tmp.begin()); - std::copy_n(data_out+len+off, len, data_out); - std::copy_n(tmp.begin(), len+off, data_out+len); + d_fft_shift.shift(data_out, size); } void @@ -491,6 +485,8 @@ namespace gr { delete d_fft; d_fft = new fft::fft_complex(d_fftsize, true); + d_fft_shift.resize(d_fftsize); + volk_free(d_fbuf); d_fbuf = (float*)volk_malloc(d_fftsize*sizeof(float), volk_get_alignment()); @@ -535,7 +531,7 @@ namespace gr { new SetFreqEvent(d_center_freq, d_bandwidth)); } } - } + } void waterfall_sink_f_impl::set_time_per_fft(double t) diff --git a/gr-qtgui/lib/waterfall_sink_f_impl.h b/gr-qtgui/lib/waterfall_sink_f_impl.h index aca87b187a..d474a4f079 100644 --- a/gr-qtgui/lib/waterfall_sink_f_impl.h +++ b/gr-qtgui/lib/waterfall_sink_f_impl.h @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2012,2015 Free Software Foundation, Inc. + * Copyright 2012,2015,2019 Free Software Foundation, Inc. * * This file is part of GNU Radio * @@ -27,6 +27,7 @@ #include <gnuradio/filter/firdes.h> #include <gnuradio/fft/fft.h> +#include <gnuradio/fft/fft_shift.h> #include <gnuradio/high_res_timer.h> #include <gnuradio/qtgui/waterfalldisplayform.h> @@ -41,6 +42,7 @@ namespace gr { void initialize(); int d_fftsize; + fft::fft_shift<float> d_fft_shift; float d_fftavg; filter::firdes::win_type d_wintype; std::vector<float> d_window; |