From d79054551fee36eda99a142aff2856f20b4787fa Mon Sep 17 00:00:00 2001 From: Tom Rondeau Date: Thu, 26 Apr 2012 20:05:59 -0400 Subject: gr-fft: creates a gr-fft top-level component. --- gr-fft/lib/CMakeLists.txt | 59 +++++++ gr-fft/lib/fft_impl_fft.cc | 330 ++++++++++++++++++++++++++++++++++++++++ gr-fft/lib/fft_impl_goertzel.cc | 78 ++++++++++ gr-fft/lib/fft_vcc_fftw.cc | 147 ++++++++++++++++++ gr-fft/lib/fft_vcc_fftw.h | 60 ++++++++ gr-fft/lib/fft_vfc_fftw.cc | 124 +++++++++++++++ gr-fft/lib/fft_vfc_fftw.h | 59 +++++++ gr-fft/lib/goertzel_fc_impl.cc | 85 +++++++++++ gr-fft/lib/goertzel_fc_impl.h | 60 ++++++++ 9 files changed, 1002 insertions(+) create mode 100644 gr-fft/lib/CMakeLists.txt create mode 100644 gr-fft/lib/fft_impl_fft.cc create mode 100644 gr-fft/lib/fft_impl_goertzel.cc create mode 100644 gr-fft/lib/fft_vcc_fftw.cc create mode 100644 gr-fft/lib/fft_vcc_fftw.h create mode 100644 gr-fft/lib/fft_vfc_fftw.cc create mode 100644 gr-fft/lib/fft_vfc_fftw.h create mode 100644 gr-fft/lib/goertzel_fc_impl.cc create mode 100644 gr-fft/lib/goertzel_fc_impl.h (limited to 'gr-fft/lib') diff --git a/gr-fft/lib/CMakeLists.txt b/gr-fft/lib/CMakeLists.txt new file mode 100644 index 000000000..eb2b9ee07 --- /dev/null +++ b/gr-fft/lib/CMakeLists.txt @@ -0,0 +1,59 @@ +# Copyright 2012 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. + +######################################################################## +# Setup the include and linker paths +######################################################################## +include_directories( + ${GNURADIO_CORE_INCLUDE_DIRS} + ${GR_FFT_INCLUDE_DIRS} + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} +) + +include_directories(${FFT_INCLUDE_DIRS}) +link_directories(${FFT_LIBRARY_DIRS}) + +include_directories(${Boost_INCLUDE_DIRS}) +link_directories(${Boost_LIBRARY_DIRS}) + +include_directories(${FFTW3F_INCLUDE_DIRS}) +link_directories(${FFTW3F_LIBRARY_DIRS}) + +######################################################################## +# Setup library +######################################################################## +list(APPEND fft_sources + fft_vcc_fftw.cc + fft_vfc_fftw.cc + goertzel_fc_impl.cc + fft_impl_fft.cc + fft_impl_goertzel.cc +) + +list(APPEND fft_libs + gnuradio-core + ${Boost_LIBRARIES} + ${FFT_LIBRARIES} + ${FFTW3F_LIBRARIES} +) + +add_library(gnuradio-fft SHARED ${fft_sources}) +target_link_libraries(gnuradio-fft ${fft_libs}) +GR_LIBRARY_FOO(gnuradio-fft RUNTIME_COMPONENT "fft_runtime" DEVEL_COMPONENT "fft_devel") diff --git a/gr-fft/lib/fft_impl_fft.cc b/gr-fft/lib/fft_impl_fft.cc new file mode 100644 index 000000000..42bdf7778 --- /dev/null +++ b/gr-fft/lib/fft_impl_fft.cc @@ -0,0 +1,330 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2008,2011,2012 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. + */ + +#include +#include +#include + +#ifdef _MSC_VER //http://www.fftw.org/install/windows.html#DLLwisdom +static void my_fftw_write_char(char c, void *f) { fputc(c, (FILE *) f); } +#define fftw_export_wisdom_to_file(f) fftw_export_wisdom(my_fftw_write_char, (void*) (f)) +#define fftwf_export_wisdom_to_file(f) fftwf_export_wisdom(my_fftw_write_char, (void*) (f)) +#define fftwl_export_wisdom_to_file(f) fftwl_export_wisdom(my_fftw_write_char, (void*) (f)) + +static int my_fftw_read_char(void *f) { return fgetc((FILE *) f); } +#define fftw_import_wisdom_from_file(f) fftw_import_wisdom(my_fftw_read_char, (void*) (f)) +#define fftwf_import_wisdom_from_file(f) fftwf_import_wisdom(my_fftw_read_char, (void*) (f)) +#define fftwl_import_wisdom_from_file(f) fftwl_import_wisdom(my_fftw_read_char, (void*) (f)) +#endif //_MSC_VER + +#include +#include +#include +#include +#include +#include + +#include +#include +namespace fs = boost::filesystem; + +gr_complex * +fft_impl_fft_malloc_complex(int size) +{ + return (gr_complex*)fftwf_malloc(sizeof(gr_complex)*size); +} + +float * +fft_impl_fft_malloc_float(int size) +{ + return (float*)fftwf_malloc(sizeof(float)*size); +} + +void +fft_impl_fft_free(void *b) +{ + fftwf_free(b); +} + +boost::mutex & +fft_impl_fft_planner::mutex() +{ + static boost::mutex s_planning_mutex; + + return s_planning_mutex; +} + +static const char * +wisdom_filename () +{ + static fs::path path; + path = fs::path(gr_appdata_path()) / ".gr_fftw_wisdom"; + return path.string().c_str(); +} + +static void +fft_impl_fftw_import_wisdom () +{ + const char *filename = wisdom_filename (); + FILE *fp = fopen (filename, "r"); + if (fp != 0){ + int r = fftwf_import_wisdom_from_file (fp); + fclose (fp); + if (!r){ + fprintf (stderr, "fft_impl_fftw: can't import wisdom from %s\n", filename); + } + } +} + +static void +fft_impl_fftw_config_threading (int nthreads) +{ + static int fftw_threads_inited = 0; + +#ifdef FFTW3F_THREADS + if (fftw_threads_inited == 0) + { + fftw_threads_inited = 1; + fftwf_init_threads(); + } + + fftwf_plan_with_nthreads(nthreads); +#endif +} + +static void +fft_impl_fftw_export_wisdom () +{ + const char *filename = wisdom_filename (); + FILE *fp = fopen (filename, "w"); + if (fp != 0){ + fftwf_export_wisdom_to_file (fp); + fclose (fp); + } + else { + fprintf (stderr, "fft_impl_fftw: "); + perror (filename); + } +} + +// ---------------------------------------------------------------- + +fft_impl_fft_complex::fft_impl_fft_complex (int fft_size, bool forward, int nthreads) +{ + // Hold global mutex during plan construction and destruction. + fft_impl_fft_planner::scoped_lock lock(fft_impl_fft_planner::mutex()); + + assert (sizeof (fftwf_complex) == sizeof (gr_complex)); + + if (fft_size <= 0) + throw std::out_of_range ("fft_impl_fftw: invalid fft_size"); + + d_fft_size = fft_size; + d_inbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * inbuf_length ()); + if (d_inbuf == 0) + throw std::runtime_error ("fftwf_malloc"); + + d_outbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * outbuf_length ()); + if (d_outbuf == 0){ + fftwf_free (d_inbuf); + throw std::runtime_error ("fftwf_malloc"); + } + + d_nthreads = nthreads; + fft_impl_fftw_config_threading (nthreads); + fft_impl_fftw_import_wisdom (); // load prior wisdom from disk + + d_plan = fftwf_plan_dft_1d (fft_size, + reinterpret_cast(d_inbuf), + reinterpret_cast(d_outbuf), + forward ? FFTW_FORWARD : FFTW_BACKWARD, + FFTW_MEASURE); + + if (d_plan == NULL) { + fprintf(stderr, "fft_impl_fft_complex: error creating plan\n"); + throw std::runtime_error ("fftwf_plan_dft_1d failed"); + } + fft_impl_fftw_export_wisdom (); // store new wisdom to disk +} + +fft_impl_fft_complex::~fft_impl_fft_complex () +{ + // Hold global mutex during plan construction and destruction. + fft_impl_fft_planner::scoped_lock lock(fft_impl_fft_planner::mutex()); + + fftwf_destroy_plan ((fftwf_plan) d_plan); + fftwf_free (d_inbuf); + fftwf_free (d_outbuf); +} + +void +fft_impl_fft_complex::set_nthreads(int n) +{ + if (n <= 0) + throw std::out_of_range ("fft_impl_fftw: invalid number of threads"); + d_nthreads = n; + +#ifdef FFTW3F_THREADS + fftwf_plan_with_nthreads(d_nthreads); +#endif +} + +void +fft_impl_fft_complex::execute () +{ + fftwf_execute ((fftwf_plan) d_plan); +} + +// ---------------------------------------------------------------- + +fft_impl_fft_real_fwd::fft_impl_fft_real_fwd (int fft_size, int nthreads) +{ + // Hold global mutex during plan construction and destruction. + fft_impl_fft_planner::scoped_lock lock(fft_impl_fft_planner::mutex()); + + assert (sizeof (fftwf_complex) == sizeof (gr_complex)); + + if (fft_size <= 0) + throw std::out_of_range ("fft_impl_fftw: invalid fft_size"); + + d_fft_size = fft_size; + d_inbuf = (float *) fftwf_malloc (sizeof (float) * inbuf_length ()); + if (d_inbuf == 0) + throw std::runtime_error ("fftwf_malloc"); + + d_outbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * outbuf_length ()); + if (d_outbuf == 0){ + fftwf_free (d_inbuf); + throw std::runtime_error ("fftwf_malloc"); + } + + d_nthreads = nthreads; + fft_impl_fftw_config_threading (nthreads); + fft_impl_fftw_import_wisdom (); // load prior wisdom from disk + + d_plan = fftwf_plan_dft_r2c_1d (fft_size, + d_inbuf, + reinterpret_cast(d_outbuf), + FFTW_MEASURE); + + if (d_plan == NULL) { + fprintf(stderr, "fft_impl_fft_real_fwd: error creating plan\n"); + throw std::runtime_error ("fftwf_plan_dft_r2c_1d failed"); + } + fft_impl_fftw_export_wisdom (); // store new wisdom to disk +} + +fft_impl_fft_real_fwd::~fft_impl_fft_real_fwd () +{ + // Hold global mutex during plan construction and destruction. + fft_impl_fft_planner::scoped_lock lock(fft_impl_fft_planner::mutex()); + + fftwf_destroy_plan ((fftwf_plan) d_plan); + fftwf_free (d_inbuf); + fftwf_free (d_outbuf); +} + +void +fft_impl_fft_real_fwd::set_nthreads(int n) +{ + if (n <= 0) + throw std::out_of_range ("fft_impl_fftw: invalid number of threads"); + d_nthreads = n; + +#ifdef FFTW3F_THREADS + fftwf_plan_with_nthreads(d_nthreads); +#endif +} + +void +fft_impl_fft_real_fwd::execute () +{ + fftwf_execute ((fftwf_plan) d_plan); +} + +// ---------------------------------------------------------------- + +fft_impl_fft_real_rev::fft_impl_fft_real_rev (int fft_size, int nthreads) +{ + // Hold global mutex during plan construction and destruction. + fft_impl_fft_planner::scoped_lock lock(fft_impl_fft_planner::mutex()); + + assert (sizeof (fftwf_complex) == sizeof (gr_complex)); + + if (fft_size <= 0) + throw std::out_of_range ("fft_impl_fftw: invalid fft_size"); + + d_fft_size = fft_size; + d_inbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * inbuf_length ()); + if (d_inbuf == 0) + throw std::runtime_error ("fftwf_malloc"); + + d_outbuf = (float *) fftwf_malloc (sizeof (float) * outbuf_length ()); + if (d_outbuf == 0){ + fftwf_free (d_inbuf); + throw std::runtime_error ("fftwf_malloc"); + } + + d_nthreads = nthreads; + fft_impl_fftw_config_threading (nthreads); + fft_impl_fftw_import_wisdom (); // load prior wisdom from disk + + // FIXME If there's ever a chance that the planning functions + // will be called in multiple threads, we've got to ensure single + // threaded access. They are not thread-safe. + d_plan = fftwf_plan_dft_c2r_1d (fft_size, + reinterpret_cast(d_inbuf), + d_outbuf, + FFTW_MEASURE); + + if (d_plan == NULL) { + fprintf(stderr, "fft_impl_fft_real_rev: error creating plan\n"); + throw std::runtime_error ("fftwf_plan_dft_c2r_1d failed"); + } + fft_impl_fftw_export_wisdom (); // store new wisdom to disk +} + +fft_impl_fft_real_rev::~fft_impl_fft_real_rev () +{ + fftwf_destroy_plan ((fftwf_plan) d_plan); + fftwf_free (d_inbuf); + fftwf_free (d_outbuf); +} + +void +fft_impl_fft_real_rev::set_nthreads(int n) +{ + if (n <= 0) + throw std::out_of_range ("fft_impl_fftw: invalid number of threads"); + d_nthreads = n; + +#ifdef FFTW3F_THREADS + fftwf_plan_with_nthreads(d_nthreads); +#endif +} + +void +fft_impl_fft_real_rev::execute () +{ + fftwf_execute ((fftwf_plan) d_plan); +} + diff --git a/gr-fft/lib/fft_impl_goertzel.cc b/gr-fft/lib/fft_impl_goertzel.cc new file mode 100644 index 000000000..b008d0481 --- /dev/null +++ b/gr-fft/lib/fft_impl_goertzel.cc @@ -0,0 +1,78 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002,2011,2012 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 +#endif + +#include +#include + +fft_impl_goertzel::fft_impl_goertzel(int rate, int len, float freq) +{ + set_params(rate, len, freq); +} + +void +fft_impl_goertzel::set_params(int rate, int len, float freq) +{ + d_d1 = 0.0; + d_d2 = 0.0; + + float w = 2.0*M_PI*freq/rate; + d_wr = 2.0*std::cos(w); + d_wi = std::sin(w); + d_len = len; + d_processed = 0; + +} + +gr_complex +fft_impl_goertzel::batch(float *in) +{ + d_d1 = 0.0; + d_d2 = 0.0; + + for(int i = 0; i < d_len; i++) + input(in[i]); + + return output(); +} + +void +fft_impl_goertzel::input(const float &input) +{ + float y = input + d_wr*d_d1 - d_d2; + d_d2 = d_d1; + d_d1 = y; + d_processed++; +} + +gr_complex +fft_impl_goertzel::output() +{ + gr_complex out((0.5*d_wr*d_d1-d_d2)/d_len, (d_wi*d_d1)/d_len); + d_d1 = 0.0; + d_d2 = 0.0; + d_processed = 0; + return out; +} diff --git a/gr-fft/lib/fft_vcc_fftw.cc b/gr-fft/lib/fft_vcc_fftw.cc new file mode 100644 index 000000000..92441f0d3 --- /dev/null +++ b/gr-fft/lib/fft_vcc_fftw.cc @@ -0,0 +1,147 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2007,2008,2010,2012 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 "fft_vcc_fftw.h" +#include +#include +#include + +namespace gr { + namespace fft { + + fft_vcc::sptr fft_vcc::make(int fft_size, bool forward, + const std::vector &window, + bool shift, int nthreads) + { + return gnuradio::get_initial_sptr(new fft_vcc_fftw + (fft_size, forward, window, + shift, nthreads)); + } + + fft_vcc_fftw::fft_vcc_fftw(int fft_size, bool forward, + const std::vector &window, + bool shift, int nthreads) + : gr_sync_block("fft_vcc_fftw", + gr_make_io_signature(1, 1, fft_size * sizeof(gr_complex)), + gr_make_io_signature(1, 1, fft_size * sizeof(gr_complex))), + d_fft_size(fft_size), d_forward(forward), d_shift(shift) + { + d_fft = new fft_impl_fft_complex(d_fft_size, forward, nthreads); + } + + fft_vcc_fftw::~fft_vcc_fftw() + { + delete d_fft; + } + + void + fft_vcc_fftw::set_nthreads(int n) + { + d_fft->set_nthreads(n); + } + + int + fft_vcc_fftw::nthreads() const + { + return d_fft->nthreads(); + } + + bool + fft_vcc_fftw::set_window(const std::vector &window) + { + if(window.size()==0 || window.size()==d_fft_size) { + d_window=window; + return true; + } + else + return false; + } + + int + fft_vcc_fftw::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]; + + unsigned int input_data_size = input_signature()->sizeof_stream_item (0); + unsigned int output_data_size = output_signature()->sizeof_stream_item (0); + + int count = 0; + + while(count++ < noutput_items) { + + // copy input into optimally aligned buffer + if(d_window.size()) { + gr_complex *dst = d_fft->get_inbuf(); + if(!d_forward && d_shift) { + unsigned int offset = (!d_forward && d_shift)?(d_fft_size/2):0; + int fft_m_offset = d_fft_size - offset; + for(unsigned int i = 0; i < offset; i++) // apply window + dst[i+fft_m_offset] = in[i] * d_window[i]; + for(unsigned int i = offset; i < d_fft_size; i++) // apply window + dst[i-offset] = in[i] * d_window[i]; + } + else { + for(unsigned int i = 0; i < d_fft_size; i++) // apply window + dst[i] = in[i] * d_window[i]; + } + } + else { + if(!d_forward && d_shift) { // apply an ifft shift on the data + gr_complex *dst = d_fft->get_inbuf(); + unsigned int len = (unsigned int)(floor(d_fft_size/2.0)); // half length of complex array + memcpy(&dst[0], &in[len], sizeof(gr_complex)*(d_fft_size - len)); + memcpy(&dst[d_fft_size - len], &in[0], sizeof(gr_complex)*len); + } + else { + memcpy(d_fft->get_inbuf(), in, input_data_size); + } + } + + // compute the fft + d_fft->execute(); + + // copy result to our output + if(d_forward && d_shift) { // apply a fft shift on the data + unsigned int len = (unsigned int)(ceil(d_fft_size/2.0)); + memcpy(&out[0], &d_fft->get_outbuf()[len], sizeof(gr_complex)*(d_fft_size - len)); + memcpy(&out[d_fft_size - len], &d_fft->get_outbuf()[0], sizeof(gr_complex)*len); + } + else { + memcpy (out, d_fft->get_outbuf (), output_data_size); + } + + in += d_fft_size; + out += d_fft_size; + } + + return noutput_items; + } + + } /* namespace fft */ +} /* namespace gr */ diff --git a/gr-fft/lib/fft_vcc_fftw.h b/gr-fft/lib/fft_vcc_fftw.h new file mode 100644 index 000000000..fb9312a64 --- /dev/null +++ b/gr-fft/lib/fft_vcc_fftw.h @@ -0,0 +1,60 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2007,2008,2012 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_VCC_FFTW_IMPL_H +#define INCLUDED_FFT_FFT_VCC_FFTW_IMPL_H + +#include +#include + +namespace gr { + namespace fft { + + class FFT_API fft_vcc_fftw : public fft_vcc + { + private: + fft_impl_fft_complex *d_fft; + unsigned int d_fft_size; + std::vector d_window; + bool d_forward; + bool d_shift; + + public: + fft_vcc_fftw(int fft_size, bool forward, + const std::vector &window, + bool shift, int nthreads=1); + + ~fft_vcc_fftw(); + + void set_nthreads(int n); + int nthreads() const; + bool set_window(const std::vector &window); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace fft */ +} /* namespace gr */ + +#endif /* INCLUDED_FFT_FFT_VCC_FFTW_IMPL_H */ diff --git a/gr-fft/lib/fft_vfc_fftw.cc b/gr-fft/lib/fft_vfc_fftw.cc new file mode 100644 index 000000000..0643c180d --- /dev/null +++ b/gr-fft/lib/fft_vfc_fftw.cc @@ -0,0 +1,124 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2007,2008,2010,2012 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 "fft_vfc_fftw.h" +#include +#include +#include + +namespace gr { + namespace fft { + + fft_vfc::sptr fft_vfc::make(int fft_size, bool forward, + const std::vector &window, + int nthreads) + { + return gnuradio::get_initial_sptr(new fft_vfc_fftw + (fft_size, forward, window, + nthreads)); + } + + fft_vfc_fftw::fft_vfc_fftw(int fft_size, bool forward, + const std::vector &window, + int nthreads) + : gr_sync_block("fft_vfc_fftw", + gr_make_io_signature(1, 1, fft_size * sizeof(gr_complex)), + gr_make_io_signature(1, 1, fft_size * sizeof(gr_complex))), + d_fft_size(fft_size), d_forward(forward) + { + d_fft = new fft_impl_fft_complex(d_fft_size, forward, nthreads); + } + + fft_vfc_fftw::~fft_vfc_fftw() + { + delete d_fft; + } + + void + fft_vfc_fftw::set_nthreads(int n) + { + d_fft->set_nthreads(n); + } + + int + fft_vfc_fftw::nthreads() const + { + return d_fft->nthreads(); + } + + bool + fft_vfc_fftw::set_window(const std::vector &window) + { + if(window.size()==0 || window.size()==d_fft_size) { + d_window=window; + return true; + } + else + return false; + } + + int + fft_vfc_fftw::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const float *in = (const float *)input_items[0]; + gr_complex *out = (gr_complex *)output_items[0]; + + unsigned int output_data_size = output_signature()->sizeof_stream_item (0); + + int count = 0; + + while(count++ < noutput_items) { + + // copy input into optimally aligned buffer + if(d_window.size()) { + gr_complex *dst = d_fft->get_inbuf(); + for(unsigned int i = 0; i < d_fft_size; i++) // apply window + dst[i] = in[i] * d_window[i]; + } + else { + gr_complex *dst = d_fft->get_inbuf(); + for(unsigned int i = 0; i < d_fft_size; i++) // float to complex conversion + dst[i] = in[i]; + } + + // compute the fft + d_fft->execute(); + + // copy result to output stream + memcpy(out, d_fft->get_outbuf(), output_data_size); + + in += d_fft_size; + out += d_fft_size; + } + + return noutput_items; + } + + } /* namespace fft */ +} /* namespace gr */ + diff --git a/gr-fft/lib/fft_vfc_fftw.h b/gr-fft/lib/fft_vfc_fftw.h new file mode 100644 index 000000000..54be6c128 --- /dev/null +++ b/gr-fft/lib/fft_vfc_fftw.h @@ -0,0 +1,59 @@ +/* -*- c++ -*- */ +/* + * Copyright 2004,2007,2008,2012 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_VFC_FFTW_IMPL_H +#define INCLUDED_FFT_FFT_VFC_FFTW_IMPL_H + +#include +#include + +namespace gr { + namespace fft { + + class FFT_API fft_vfc_fftw : public fft_vfc + { + private: + fft_impl_fft_complex *d_fft; + unsigned int d_fft_size; + std::vector d_window; + bool d_forward; + + public: + fft_vfc_fftw(int fft_size, bool forward, + const std::vector &window, + int nthreads=1); + + ~fft_vfc_fftw(); + + void set_nthreads(int n); + int nthreads() const; + bool set_window(const std::vector &window); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace fft */ +} /* namespace gr */ + +#endif /* INCLUDED_FFT_FFT_VFC_FFTW_IMPL_H */ diff --git a/gr-fft/lib/goertzel_fc_impl.cc b/gr-fft/lib/goertzel_fc_impl.cc new file mode 100644 index 000000000..ee214f24d --- /dev/null +++ b/gr-fft/lib/goertzel_fc_impl.cc @@ -0,0 +1,85 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2010-2012 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 "goertzel_fc_impl.h" +#include + +namespace gr { + namespace fft { + + goertzel_fc::sptr goertzel_fc::make(int rate, int len, float freq) + { + return gnuradio::get_initial_sptr(new goertzel_fc_impl(rate, len, freq)); + } + + goertzel_fc_impl::goertzel_fc_impl(int rate, int len, float freq) + : gr_sync_decimator("goertzel_fc", + gr_make_io_signature (1, 1, sizeof(float)), + gr_make_io_signature (1, 1, sizeof(gr_complex)), + len), + d_goertzel(rate, len, freq) + { + d_len = len; + d_rate = rate; + d_freq = freq; + } + + goertzel_fc_impl::~goertzel_fc_impl() + { + } + + void + goertzel_fc_impl::set_freq(float freq) + { + d_freq = freq; + d_goertzel.set_params(d_rate, d_len, d_freq); + } + + void + goertzel_fc_impl::set_rate(int rate) + { + d_rate = rate; + d_goertzel.set_params(d_rate, d_len, d_freq); + } + + int + goertzel_fc_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + float *in = (float *)input_items[0]; + gr_complex *out = (gr_complex *)output_items[0]; + + for(int i = 0; i < noutput_items; i++) { + *out++ = d_goertzel.batch(in); + in += d_len; + } + + return noutput_items; + } + + } /* namespace fft */ +} /* namespace gr */ diff --git a/gr-fft/lib/goertzel_fc_impl.h b/gr-fft/lib/goertzel_fc_impl.h new file mode 100644 index 000000000..d7bab1f1d --- /dev/null +++ b/gr-fft/lib/goertzel_fc_impl.h @@ -0,0 +1,60 @@ +/* -*- c++ -*- */ +/* + * Copyright 2006,2011,2012 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_GOERTZEL_FC_IMPL_H +#define INCLUDED_FFT_GOERTZEL_FC_IMPL_H + +#include +#include + +namespace gr { + namespace fft { + + class FFT_API goertzel_fc_impl : public goertzel_fc + { + private: + fft_impl_goertzel d_goertzel; + int d_len; + float d_freq; + int d_rate; + + public: + goertzel_fc_impl(int rate, int len, float freq); + + ~goertzel_fc_impl(); + + void set_freq(float freq); + void set_rate(int rate); + + float freq() { return d_freq; } + int rate() { return d_rate; } + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace fft */ +} /* namespace gr */ + +#endif /* INCLUDED_FFT_GOERTZEL_FC_IMPL_H */ + -- cgit From 399969c8f9ad2ac19acd0f2299b0424dec2390b1 Mon Sep 17 00:00:00 2001 From: Johnathan Corgan Date: Sun, 29 Apr 2012 11:44:27 -0700 Subject: fft: put helper classes in namespace --- gr-fft/lib/CMakeLists.txt | 4 +- gr-fft/lib/fft.cc | 338 ++++++++++++++++++++++++++++++++++++++++ gr-fft/lib/fft_impl_fft.cc | 330 --------------------------------------- gr-fft/lib/fft_impl_goertzel.cc | 78 ---------- gr-fft/lib/fft_vcc_fftw.cc | 2 +- gr-fft/lib/fft_vcc_fftw.h | 4 +- gr-fft/lib/fft_vfc_fftw.cc | 2 +- gr-fft/lib/fft_vfc_fftw.h | 4 +- gr-fft/lib/goertzel.cc | 84 ++++++++++ gr-fft/lib/goertzel_fc_impl.h | 10 +- 10 files changed, 435 insertions(+), 421 deletions(-) create mode 100644 gr-fft/lib/fft.cc delete mode 100644 gr-fft/lib/fft_impl_fft.cc delete mode 100644 gr-fft/lib/fft_impl_goertzel.cc create mode 100644 gr-fft/lib/goertzel.cc (limited to 'gr-fft/lib') diff --git a/gr-fft/lib/CMakeLists.txt b/gr-fft/lib/CMakeLists.txt index eb2b9ee07..e2f17a183 100644 --- a/gr-fft/lib/CMakeLists.txt +++ b/gr-fft/lib/CMakeLists.txt @@ -40,11 +40,11 @@ link_directories(${FFTW3F_LIBRARY_DIRS}) # Setup library ######################################################################## list(APPEND fft_sources + fft.cc fft_vcc_fftw.cc fft_vfc_fftw.cc goertzel_fc_impl.cc - fft_impl_fft.cc - fft_impl_goertzel.cc + goertzel.cc ) list(APPEND fft_libs diff --git a/gr-fft/lib/fft.cc b/gr-fft/lib/fft.cc new file mode 100644 index 000000000..6074236e1 --- /dev/null +++ b/gr-fft/lib/fft.cc @@ -0,0 +1,338 @@ +/* -*- c++ -*- */ +/* + * Copyright 2003,2008,2011,2012 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. + */ + +#include +#include +#include + +#ifdef _MSC_VER //http://www.fftw.org/install/windows.html#DLLwisdom +static void my_fftw_write_char(char c, void *f) { fputc(c, (FILE *) f); } +#define fftw_export_wisdom_to_file(f) fftw_export_wisdom(my_fftw_write_char, (void*) (f)) +#define fftwf_export_wisdom_to_file(f) fftwf_export_wisdom(my_fftw_write_char, (void*) (f)) +#define fftwl_export_wisdom_to_file(f) fftwl_export_wisdom(my_fftw_write_char, (void*) (f)) + +static int my_fftw_read_char(void *f) { return fgetc((FILE *) f); } +#define fftw_import_wisdom_from_file(f) fftw_import_wisdom(my_fftw_read_char, (void*) (f)) +#define fftwf_import_wisdom_from_file(f) fftwf_import_wisdom(my_fftw_read_char, (void*) (f)) +#define fftwl_import_wisdom_from_file(f) fftwl_import_wisdom(my_fftw_read_char, (void*) (f)) +#endif //_MSC_VER + +#include +#include +#include +#include +#include +#include + +#include +#include +namespace fs = boost::filesystem; + +namespace gr { + namespace fft { + + gr_complex * + malloc_complex(int size) + { + return (gr_complex*)fftwf_malloc(sizeof(gr_complex)*size); + } + + float * + malloc_float(int size) + { + return (float*)fftwf_malloc(sizeof(float)*size); + } + + void + free(void *b) + { + fftwf_free(b); + } + + boost::mutex & + planner::mutex() + { + static boost::mutex s_planning_mutex; + + return s_planning_mutex; + } + + static const char * + wisdom_filename() + { + static fs::path path; + path = fs::path(gr_appdata_path()) / ".gr_fftw_wisdom"; + return path.string().c_str(); + } + + static void + import_wisdom() + { + const char *filename = wisdom_filename (); + FILE *fp = fopen (filename, "r"); + if (fp != 0){ + int r = fftwf_import_wisdom_from_file (fp); + fclose (fp); + if (!r){ + fprintf (stderr, "gr::fft: can't import wisdom from %s\n", filename); + } + } + } + + static void + config_threading(int nthreads) + { + static int fftw_threads_inited = 0; + +#ifdef FFTW3F_THREADS + if (fftw_threads_inited == 0) + { + fftw_threads_inited = 1; + fftwf_init_threads(); + } + + fftwf_plan_with_nthreads(nthreads); +#endif + } + + static void + export_wisdom() + { + const char *filename = wisdom_filename (); + FILE *fp = fopen (filename, "w"); + if (fp != 0){ + fftwf_export_wisdom_to_file (fp); + fclose (fp); + } + else { + fprintf (stderr, "fft_impl_fftw: "); + perror (filename); + } + } + +// ---------------------------------------------------------------- + + fft_complex::fft_complex(int fft_size, bool forward, int nthreads) + { + // Hold global mutex during plan construction and destruction. + planner::scoped_lock lock(planner::mutex()); + + assert (sizeof (fftwf_complex) == sizeof (gr_complex)); + + if (fft_size <= 0) + throw std::out_of_range ("fft_impl_fftw: invalid fft_size"); + + d_fft_size = fft_size; + d_inbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * inbuf_length ()); + if (d_inbuf == 0) + throw std::runtime_error ("fftwf_malloc"); + + d_outbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * outbuf_length ()); + if (d_outbuf == 0){ + fftwf_free (d_inbuf); + throw std::runtime_error ("fftwf_malloc"); + } + + d_nthreads = nthreads; + config_threading(nthreads); + import_wisdom(); // load prior wisdom from disk + + d_plan = fftwf_plan_dft_1d (fft_size, + reinterpret_cast(d_inbuf), + reinterpret_cast(d_outbuf), + forward ? FFTW_FORWARD : FFTW_BACKWARD, + FFTW_MEASURE); + + if (d_plan == NULL) { + fprintf(stderr, "gr::fft: error creating plan\n"); + throw std::runtime_error ("fftwf_plan_dft_1d failed"); + } + export_wisdom(); // store new wisdom to disk + } + + fft_complex::~fft_complex() + { + // Hold global mutex during plan construction and destruction. + planner::scoped_lock lock(planner::mutex()); + + fftwf_destroy_plan ((fftwf_plan) d_plan); + fftwf_free (d_inbuf); + fftwf_free (d_outbuf); + } + + void + fft_complex::set_nthreads(int n) + { + if (n <= 0) + throw std::out_of_range ("gr::fft: invalid number of threads"); + d_nthreads = n; + +#ifdef FFTW3F_THREADS + fftwf_plan_with_nthreads(d_nthreads); +#endif + } + + void + fft_complex::execute() + { + fftwf_execute((fftwf_plan) d_plan); + } + +// ---------------------------------------------------------------- + + fft_real_fwd::fft_real_fwd (int fft_size, int nthreads) + { + // Hold global mutex during plan construction and destruction. + planner::scoped_lock lock(planner::mutex()); + + assert (sizeof (fftwf_complex) == sizeof (gr_complex)); + + if (fft_size <= 0) + throw std::out_of_range ("gr::fft: invalid fft_size"); + + d_fft_size = fft_size; + d_inbuf = (float *) fftwf_malloc (sizeof (float) * inbuf_length ()); + if (d_inbuf == 0) + throw std::runtime_error ("fftwf_malloc"); + + d_outbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * outbuf_length ()); + if (d_outbuf == 0){ + fftwf_free (d_inbuf); + throw std::runtime_error ("fftwf_malloc"); + } + + d_nthreads = nthreads; + config_threading(nthreads); + import_wisdom(); // load prior wisdom from disk + + d_plan = fftwf_plan_dft_r2c_1d (fft_size, + d_inbuf, + reinterpret_cast(d_outbuf), + FFTW_MEASURE); + + if (d_plan == NULL) { + fprintf(stderr, "gr::fft::fft_real_fwd: error creating plan\n"); + throw std::runtime_error ("fftwf_plan_dft_r2c_1d failed"); + } + export_wisdom(); // store new wisdom to disk + } + + fft_real_fwd::~fft_real_fwd() + { + // Hold global mutex during plan construction and destruction. + planner::scoped_lock lock(planner::mutex()); + + fftwf_destroy_plan ((fftwf_plan) d_plan); + fftwf_free (d_inbuf); + fftwf_free (d_outbuf); + } + + void + fft_real_fwd::set_nthreads(int n) + { + if (n <= 0) + throw std::out_of_range ("gr::fft::fft_real_fwd::set_nthreads: invalid number of threads"); + d_nthreads = n; + +#ifdef FFTW3F_THREADS + fftwf_plan_with_nthreads(d_nthreads); +#endif + } + + void + fft_real_fwd::execute() + { + fftwf_execute ((fftwf_plan) d_plan); + } + + // ---------------------------------------------------------------- + + fft_real_rev::fft_real_rev(int fft_size, int nthreads) + { + // Hold global mutex during plan construction and destruction. + planner::scoped_lock lock(planner::mutex()); + + assert (sizeof (fftwf_complex) == sizeof (gr_complex)); + + if (fft_size <= 0) + throw std::out_of_range ("gr::fft::fft_real_rev: invalid fft_size"); + + d_fft_size = fft_size; + d_inbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * inbuf_length ()); + if (d_inbuf == 0) + throw std::runtime_error ("fftwf_malloc"); + + d_outbuf = (float *) fftwf_malloc (sizeof (float) * outbuf_length ()); + if (d_outbuf == 0){ + fftwf_free (d_inbuf); + throw std::runtime_error ("fftwf_malloc"); + } + + d_nthreads = nthreads; + config_threading(nthreads); + import_wisdom(); // load prior wisdom from disk + + // FIXME If there's ever a chance that the planning functions + // will be called in multiple threads, we've got to ensure single + // threaded access. They are not thread-safe. + d_plan = fftwf_plan_dft_c2r_1d (fft_size, + reinterpret_cast(d_inbuf), + d_outbuf, + FFTW_MEASURE); + + if (d_plan == NULL) { + fprintf(stderr, "gr::fft::fft_real_rev: error creating plan\n"); + throw std::runtime_error ("fftwf_plan_dft_c2r_1d failed"); + } + export_wisdom (); // store new wisdom to disk + } + + fft_real_rev::~fft_real_rev () + { + // Hold global mutex during plan construction and destruction. + planner::scoped_lock lock(planner::mutex()); + + fftwf_destroy_plan ((fftwf_plan) d_plan); + fftwf_free (d_inbuf); + fftwf_free (d_outbuf); + } + + void + fft_real_rev::set_nthreads(int n) + { + if (n <= 0) + throw std::out_of_range ("gr::fft::fft_real_rev::set_nthreads: invalid number of threads"); + d_nthreads = n; + +#ifdef FFTW3F_THREADS + fftwf_plan_with_nthreads(d_nthreads); +#endif + } + + void + fft_real_rev::execute () + { + fftwf_execute ((fftwf_plan) d_plan); + } + + } /* namespace fft */ +} /* namespace gr */ diff --git a/gr-fft/lib/fft_impl_fft.cc b/gr-fft/lib/fft_impl_fft.cc deleted file mode 100644 index 42bdf7778..000000000 --- a/gr-fft/lib/fft_impl_fft.cc +++ /dev/null @@ -1,330 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2003,2008,2011,2012 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. - */ - -#include -#include -#include - -#ifdef _MSC_VER //http://www.fftw.org/install/windows.html#DLLwisdom -static void my_fftw_write_char(char c, void *f) { fputc(c, (FILE *) f); } -#define fftw_export_wisdom_to_file(f) fftw_export_wisdom(my_fftw_write_char, (void*) (f)) -#define fftwf_export_wisdom_to_file(f) fftwf_export_wisdom(my_fftw_write_char, (void*) (f)) -#define fftwl_export_wisdom_to_file(f) fftwl_export_wisdom(my_fftw_write_char, (void*) (f)) - -static int my_fftw_read_char(void *f) { return fgetc((FILE *) f); } -#define fftw_import_wisdom_from_file(f) fftw_import_wisdom(my_fftw_read_char, (void*) (f)) -#define fftwf_import_wisdom_from_file(f) fftwf_import_wisdom(my_fftw_read_char, (void*) (f)) -#define fftwl_import_wisdom_from_file(f) fftwl_import_wisdom(my_fftw_read_char, (void*) (f)) -#endif //_MSC_VER - -#include -#include -#include -#include -#include -#include - -#include -#include -namespace fs = boost::filesystem; - -gr_complex * -fft_impl_fft_malloc_complex(int size) -{ - return (gr_complex*)fftwf_malloc(sizeof(gr_complex)*size); -} - -float * -fft_impl_fft_malloc_float(int size) -{ - return (float*)fftwf_malloc(sizeof(float)*size); -} - -void -fft_impl_fft_free(void *b) -{ - fftwf_free(b); -} - -boost::mutex & -fft_impl_fft_planner::mutex() -{ - static boost::mutex s_planning_mutex; - - return s_planning_mutex; -} - -static const char * -wisdom_filename () -{ - static fs::path path; - path = fs::path(gr_appdata_path()) / ".gr_fftw_wisdom"; - return path.string().c_str(); -} - -static void -fft_impl_fftw_import_wisdom () -{ - const char *filename = wisdom_filename (); - FILE *fp = fopen (filename, "r"); - if (fp != 0){ - int r = fftwf_import_wisdom_from_file (fp); - fclose (fp); - if (!r){ - fprintf (stderr, "fft_impl_fftw: can't import wisdom from %s\n", filename); - } - } -} - -static void -fft_impl_fftw_config_threading (int nthreads) -{ - static int fftw_threads_inited = 0; - -#ifdef FFTW3F_THREADS - if (fftw_threads_inited == 0) - { - fftw_threads_inited = 1; - fftwf_init_threads(); - } - - fftwf_plan_with_nthreads(nthreads); -#endif -} - -static void -fft_impl_fftw_export_wisdom () -{ - const char *filename = wisdom_filename (); - FILE *fp = fopen (filename, "w"); - if (fp != 0){ - fftwf_export_wisdom_to_file (fp); - fclose (fp); - } - else { - fprintf (stderr, "fft_impl_fftw: "); - perror (filename); - } -} - -// ---------------------------------------------------------------- - -fft_impl_fft_complex::fft_impl_fft_complex (int fft_size, bool forward, int nthreads) -{ - // Hold global mutex during plan construction and destruction. - fft_impl_fft_planner::scoped_lock lock(fft_impl_fft_planner::mutex()); - - assert (sizeof (fftwf_complex) == sizeof (gr_complex)); - - if (fft_size <= 0) - throw std::out_of_range ("fft_impl_fftw: invalid fft_size"); - - d_fft_size = fft_size; - d_inbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * inbuf_length ()); - if (d_inbuf == 0) - throw std::runtime_error ("fftwf_malloc"); - - d_outbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * outbuf_length ()); - if (d_outbuf == 0){ - fftwf_free (d_inbuf); - throw std::runtime_error ("fftwf_malloc"); - } - - d_nthreads = nthreads; - fft_impl_fftw_config_threading (nthreads); - fft_impl_fftw_import_wisdom (); // load prior wisdom from disk - - d_plan = fftwf_plan_dft_1d (fft_size, - reinterpret_cast(d_inbuf), - reinterpret_cast(d_outbuf), - forward ? FFTW_FORWARD : FFTW_BACKWARD, - FFTW_MEASURE); - - if (d_plan == NULL) { - fprintf(stderr, "fft_impl_fft_complex: error creating plan\n"); - throw std::runtime_error ("fftwf_plan_dft_1d failed"); - } - fft_impl_fftw_export_wisdom (); // store new wisdom to disk -} - -fft_impl_fft_complex::~fft_impl_fft_complex () -{ - // Hold global mutex during plan construction and destruction. - fft_impl_fft_planner::scoped_lock lock(fft_impl_fft_planner::mutex()); - - fftwf_destroy_plan ((fftwf_plan) d_plan); - fftwf_free (d_inbuf); - fftwf_free (d_outbuf); -} - -void -fft_impl_fft_complex::set_nthreads(int n) -{ - if (n <= 0) - throw std::out_of_range ("fft_impl_fftw: invalid number of threads"); - d_nthreads = n; - -#ifdef FFTW3F_THREADS - fftwf_plan_with_nthreads(d_nthreads); -#endif -} - -void -fft_impl_fft_complex::execute () -{ - fftwf_execute ((fftwf_plan) d_plan); -} - -// ---------------------------------------------------------------- - -fft_impl_fft_real_fwd::fft_impl_fft_real_fwd (int fft_size, int nthreads) -{ - // Hold global mutex during plan construction and destruction. - fft_impl_fft_planner::scoped_lock lock(fft_impl_fft_planner::mutex()); - - assert (sizeof (fftwf_complex) == sizeof (gr_complex)); - - if (fft_size <= 0) - throw std::out_of_range ("fft_impl_fftw: invalid fft_size"); - - d_fft_size = fft_size; - d_inbuf = (float *) fftwf_malloc (sizeof (float) * inbuf_length ()); - if (d_inbuf == 0) - throw std::runtime_error ("fftwf_malloc"); - - d_outbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * outbuf_length ()); - if (d_outbuf == 0){ - fftwf_free (d_inbuf); - throw std::runtime_error ("fftwf_malloc"); - } - - d_nthreads = nthreads; - fft_impl_fftw_config_threading (nthreads); - fft_impl_fftw_import_wisdom (); // load prior wisdom from disk - - d_plan = fftwf_plan_dft_r2c_1d (fft_size, - d_inbuf, - reinterpret_cast(d_outbuf), - FFTW_MEASURE); - - if (d_plan == NULL) { - fprintf(stderr, "fft_impl_fft_real_fwd: error creating plan\n"); - throw std::runtime_error ("fftwf_plan_dft_r2c_1d failed"); - } - fft_impl_fftw_export_wisdom (); // store new wisdom to disk -} - -fft_impl_fft_real_fwd::~fft_impl_fft_real_fwd () -{ - // Hold global mutex during plan construction and destruction. - fft_impl_fft_planner::scoped_lock lock(fft_impl_fft_planner::mutex()); - - fftwf_destroy_plan ((fftwf_plan) d_plan); - fftwf_free (d_inbuf); - fftwf_free (d_outbuf); -} - -void -fft_impl_fft_real_fwd::set_nthreads(int n) -{ - if (n <= 0) - throw std::out_of_range ("fft_impl_fftw: invalid number of threads"); - d_nthreads = n; - -#ifdef FFTW3F_THREADS - fftwf_plan_with_nthreads(d_nthreads); -#endif -} - -void -fft_impl_fft_real_fwd::execute () -{ - fftwf_execute ((fftwf_plan) d_plan); -} - -// ---------------------------------------------------------------- - -fft_impl_fft_real_rev::fft_impl_fft_real_rev (int fft_size, int nthreads) -{ - // Hold global mutex during plan construction and destruction. - fft_impl_fft_planner::scoped_lock lock(fft_impl_fft_planner::mutex()); - - assert (sizeof (fftwf_complex) == sizeof (gr_complex)); - - if (fft_size <= 0) - throw std::out_of_range ("fft_impl_fftw: invalid fft_size"); - - d_fft_size = fft_size; - d_inbuf = (gr_complex *) fftwf_malloc (sizeof (gr_complex) * inbuf_length ()); - if (d_inbuf == 0) - throw std::runtime_error ("fftwf_malloc"); - - d_outbuf = (float *) fftwf_malloc (sizeof (float) * outbuf_length ()); - if (d_outbuf == 0){ - fftwf_free (d_inbuf); - throw std::runtime_error ("fftwf_malloc"); - } - - d_nthreads = nthreads; - fft_impl_fftw_config_threading (nthreads); - fft_impl_fftw_import_wisdom (); // load prior wisdom from disk - - // FIXME If there's ever a chance that the planning functions - // will be called in multiple threads, we've got to ensure single - // threaded access. They are not thread-safe. - d_plan = fftwf_plan_dft_c2r_1d (fft_size, - reinterpret_cast(d_inbuf), - d_outbuf, - FFTW_MEASURE); - - if (d_plan == NULL) { - fprintf(stderr, "fft_impl_fft_real_rev: error creating plan\n"); - throw std::runtime_error ("fftwf_plan_dft_c2r_1d failed"); - } - fft_impl_fftw_export_wisdom (); // store new wisdom to disk -} - -fft_impl_fft_real_rev::~fft_impl_fft_real_rev () -{ - fftwf_destroy_plan ((fftwf_plan) d_plan); - fftwf_free (d_inbuf); - fftwf_free (d_outbuf); -} - -void -fft_impl_fft_real_rev::set_nthreads(int n) -{ - if (n <= 0) - throw std::out_of_range ("fft_impl_fftw: invalid number of threads"); - d_nthreads = n; - -#ifdef FFTW3F_THREADS - fftwf_plan_with_nthreads(d_nthreads); -#endif -} - -void -fft_impl_fft_real_rev::execute () -{ - fftwf_execute ((fftwf_plan) d_plan); -} - diff --git a/gr-fft/lib/fft_impl_goertzel.cc b/gr-fft/lib/fft_impl_goertzel.cc deleted file mode 100644 index b008d0481..000000000 --- a/gr-fft/lib/fft_impl_goertzel.cc +++ /dev/null @@ -1,78 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2002,2011,2012 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 -#endif - -#include -#include - -fft_impl_goertzel::fft_impl_goertzel(int rate, int len, float freq) -{ - set_params(rate, len, freq); -} - -void -fft_impl_goertzel::set_params(int rate, int len, float freq) -{ - d_d1 = 0.0; - d_d2 = 0.0; - - float w = 2.0*M_PI*freq/rate; - d_wr = 2.0*std::cos(w); - d_wi = std::sin(w); - d_len = len; - d_processed = 0; - -} - -gr_complex -fft_impl_goertzel::batch(float *in) -{ - d_d1 = 0.0; - d_d2 = 0.0; - - for(int i = 0; i < d_len; i++) - input(in[i]); - - return output(); -} - -void -fft_impl_goertzel::input(const float &input) -{ - float y = input + d_wr*d_d1 - d_d2; - d_d2 = d_d1; - d_d1 = y; - d_processed++; -} - -gr_complex -fft_impl_goertzel::output() -{ - gr_complex out((0.5*d_wr*d_d1-d_d2)/d_len, (d_wi*d_d1)/d_len); - d_d1 = 0.0; - d_d2 = 0.0; - d_processed = 0; - return out; -} diff --git a/gr-fft/lib/fft_vcc_fftw.cc b/gr-fft/lib/fft_vcc_fftw.cc index 92441f0d3..ebcd5ec53 100644 --- a/gr-fft/lib/fft_vcc_fftw.cc +++ b/gr-fft/lib/fft_vcc_fftw.cc @@ -49,7 +49,7 @@ namespace gr { gr_make_io_signature(1, 1, fft_size * sizeof(gr_complex))), d_fft_size(fft_size), d_forward(forward), d_shift(shift) { - d_fft = new fft_impl_fft_complex(d_fft_size, forward, nthreads); + d_fft = new fft_complex(d_fft_size, forward, nthreads); } fft_vcc_fftw::~fft_vcc_fftw() diff --git a/gr-fft/lib/fft_vcc_fftw.h b/gr-fft/lib/fft_vcc_fftw.h index fb9312a64..ea15dd07b 100644 --- a/gr-fft/lib/fft_vcc_fftw.h +++ b/gr-fft/lib/fft_vcc_fftw.h @@ -24,7 +24,7 @@ #define INCLUDED_FFT_FFT_VCC_FFTW_IMPL_H #include -#include +#include namespace gr { namespace fft { @@ -32,7 +32,7 @@ namespace gr { class FFT_API fft_vcc_fftw : public fft_vcc { private: - fft_impl_fft_complex *d_fft; + fft_complex *d_fft; unsigned int d_fft_size; std::vector d_window; bool d_forward; diff --git a/gr-fft/lib/fft_vfc_fftw.cc b/gr-fft/lib/fft_vfc_fftw.cc index 0643c180d..8f9b127e5 100644 --- a/gr-fft/lib/fft_vfc_fftw.cc +++ b/gr-fft/lib/fft_vfc_fftw.cc @@ -49,7 +49,7 @@ namespace gr { gr_make_io_signature(1, 1, fft_size * sizeof(gr_complex))), d_fft_size(fft_size), d_forward(forward) { - d_fft = new fft_impl_fft_complex(d_fft_size, forward, nthreads); + d_fft = new fft_complex(d_fft_size, forward, nthreads); } fft_vfc_fftw::~fft_vfc_fftw() diff --git a/gr-fft/lib/fft_vfc_fftw.h b/gr-fft/lib/fft_vfc_fftw.h index 54be6c128..1b6f78ba6 100644 --- a/gr-fft/lib/fft_vfc_fftw.h +++ b/gr-fft/lib/fft_vfc_fftw.h @@ -24,7 +24,7 @@ #define INCLUDED_FFT_FFT_VFC_FFTW_IMPL_H #include -#include +#include namespace gr { namespace fft { @@ -32,7 +32,7 @@ namespace gr { class FFT_API fft_vfc_fftw : public fft_vfc { private: - fft_impl_fft_complex *d_fft; + fft_complex *d_fft; unsigned int d_fft_size; std::vector d_window; bool d_forward; diff --git a/gr-fft/lib/goertzel.cc b/gr-fft/lib/goertzel.cc new file mode 100644 index 000000000..4bcd5ee19 --- /dev/null +++ b/gr-fft/lib/goertzel.cc @@ -0,0 +1,84 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002,2011,2012 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 +#endif + +#include +#include + +namespace gr { + namespace fft { + + goertzel::goertzel(int rate, int len, float freq) + { + set_params(rate, len, freq); + } + + void + goertzel::set_params(int rate, int len, float freq) + { + d_d1 = 0.0; + d_d2 = 0.0; + + float w = 2.0*M_PI*freq/rate; + d_wr = 2.0*std::cos(w); + d_wi = std::sin(w); + d_len = len; + d_processed = 0; + } + + gr_complex + goertzel::batch(float *in) + { + d_d1 = 0.0; + d_d2 = 0.0; + + for(int i = 0; i < d_len; i++) + input(in[i]); + + return output(); + } + + void + goertzel::input(const float &input) + { + float y = input + d_wr*d_d1 - d_d2; + d_d2 = d_d1; + d_d1 = y; + d_processed++; + } + + gr_complex + goertzel::output() + { + gr_complex out((0.5*d_wr*d_d1-d_d2)/d_len, (d_wi*d_d1)/d_len); + d_d1 = 0.0; + d_d2 = 0.0; + d_processed = 0; + return out; + } + + } /* namespace fft */ +}/* namespace gr */ + diff --git a/gr-fft/lib/goertzel_fc_impl.h b/gr-fft/lib/goertzel_fc_impl.h index d7bab1f1d..426bc71f6 100644 --- a/gr-fft/lib/goertzel_fc_impl.h +++ b/gr-fft/lib/goertzel_fc_impl.h @@ -24,7 +24,7 @@ #define INCLUDED_FFT_GOERTZEL_FC_IMPL_H #include -#include +#include namespace gr { namespace fft { @@ -32,10 +32,10 @@ namespace gr { class FFT_API goertzel_fc_impl : public goertzel_fc { private: - fft_impl_goertzel d_goertzel; - int d_len; - float d_freq; - int d_rate; + goertzel d_goertzel; + int d_len; + float d_freq; + int d_rate; public: goertzel_fc_impl(int rate, int len, float freq); -- cgit From 7f8331808f99a9747bed39e55bbee98bc58c6038 Mon Sep 17 00:00:00 2001 From: Tom Rondeau Date: Wed, 2 May 2012 11:45:29 -0400 Subject: build: reworking cmake structure for include and lib directories. I think this is cleaner. We also probably don't need the link_directories (according to the cmake guys, this is no longer necessary). --- gr-fft/lib/CMakeLists.txt | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'gr-fft/lib') diff --git a/gr-fft/lib/CMakeLists.txt b/gr-fft/lib/CMakeLists.txt index e2f17a183..ea1248fba 100644 --- a/gr-fft/lib/CMakeLists.txt +++ b/gr-fft/lib/CMakeLists.txt @@ -21,19 +21,16 @@ # Setup the include and linker paths ######################################################################## include_directories( - ${GNURADIO_CORE_INCLUDE_DIRS} - ${GR_FFT_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} + ${GNURADIO_CORE_INCLUDE_DIRS} + ${GR_FFT_INCLUDE_DIRS} + ${Boost_INCLUDE_DIRS} + ${FFTW3F_INCLUDE_DIRS} ) -include_directories(${FFT_INCLUDE_DIRS}) link_directories(${FFT_LIBRARY_DIRS}) - -include_directories(${Boost_INCLUDE_DIRS}) link_directories(${Boost_LIBRARY_DIRS}) - -include_directories(${FFTW3F_INCLUDE_DIRS}) link_directories(${FFTW3F_LIBRARY_DIRS}) ######################################################################## -- cgit From 291513738c41128bd2ed37b26c53eb7273903dab Mon Sep 17 00:00:00 2001 From: Tom Rondeau Date: Sat, 10 Nov 2012 15:45:06 -0500 Subject: fft: fixing float-in, complex-out fft block to use the right io sig. --- gr-fft/lib/fft_vfc_fftw.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gr-fft/lib') diff --git a/gr-fft/lib/fft_vfc_fftw.cc b/gr-fft/lib/fft_vfc_fftw.cc index 8f9b127e5..890dac7be 100644 --- a/gr-fft/lib/fft_vfc_fftw.cc +++ b/gr-fft/lib/fft_vfc_fftw.cc @@ -45,7 +45,7 @@ namespace gr { const std::vector &window, int nthreads) : gr_sync_block("fft_vfc_fftw", - gr_make_io_signature(1, 1, fft_size * sizeof(gr_complex)), + gr_make_io_signature(1, 1, fft_size * sizeof(float)), gr_make_io_signature(1, 1, fft_size * sizeof(gr_complex))), d_fft_size(fft_size), d_forward(forward) { -- cgit From 83ba8a2dd64c821458e9d7ab89c89a08ef8bc31d Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Tue, 13 Nov 2012 18:07:46 -0800 Subject: fft: removed non-existant FFT_LIBRARY_DIRS FFT_LIBRARIES --- gr-fft/lib/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) (limited to 'gr-fft/lib') diff --git a/gr-fft/lib/CMakeLists.txt b/gr-fft/lib/CMakeLists.txt index ea1248fba..6ca20363b 100644 --- a/gr-fft/lib/CMakeLists.txt +++ b/gr-fft/lib/CMakeLists.txt @@ -29,7 +29,6 @@ include_directories( ${FFTW3F_INCLUDE_DIRS} ) -link_directories(${FFT_LIBRARY_DIRS}) link_directories(${Boost_LIBRARY_DIRS}) link_directories(${FFTW3F_LIBRARY_DIRS}) @@ -47,7 +46,6 @@ list(APPEND fft_sources list(APPEND fft_libs gnuradio-core ${Boost_LIBRARIES} - ${FFT_LIBRARIES} ${FFTW3F_LIBRARIES} ) -- cgit From 02ac01a814a9f856ed1f9a65f6b7fe281cdb13f1 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Tue, 13 Nov 2012 18:12:37 -0800 Subject: fft: restore FFTW3F_THREADS support for fft.cc --- gr-fft/lib/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'gr-fft/lib') diff --git a/gr-fft/lib/CMakeLists.txt b/gr-fft/lib/CMakeLists.txt index 6ca20363b..c16e0d116 100644 --- a/gr-fft/lib/CMakeLists.txt +++ b/gr-fft/lib/CMakeLists.txt @@ -49,6 +49,11 @@ list(APPEND fft_libs ${FFTW3F_LIBRARIES} ) +if(FFTW3F_THREADS_LIBRARIES) + list(APPEND fft_libs ${FFTW3F_THREADS_LIBRARIES}) + add_definitions("-DFFTW3F_THREADS") +endif() + add_library(gnuradio-fft SHARED ${fft_sources}) target_link_libraries(gnuradio-fft ${fft_libs}) GR_LIBRARY_FOO(gnuradio-fft RUNTIME_COMPONENT "fft_runtime" DEVEL_COMPONENT "fft_devel") -- cgit From e788c523d4f8de3efd64a64f148ac1bf25ea032d Mon Sep 17 00:00:00 2001 From: Michael L Dickens Date: Tue, 1 Jan 2013 21:42:23 -0500 Subject: Tweak INCLUDE dirs such that INCLUDE_DIRECTORIES and LINK_DIRECTORIES are ordered as: internal build and source for this component, other components (internal build and source, or already installed), non-project non-system dependencies (e.g., Qt, Boost, Python), system dependencies (e.g., CoreAudio). --- gr-fft/lib/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'gr-fft/lib') diff --git a/gr-fft/lib/CMakeLists.txt b/gr-fft/lib/CMakeLists.txt index c16e0d116..26fcaa7cd 100644 --- a/gr-fft/lib/CMakeLists.txt +++ b/gr-fft/lib/CMakeLists.txt @@ -23,8 +23,9 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} - ${GNURADIO_CORE_INCLUDE_DIRS} ${GR_FFT_INCLUDE_DIRS} + ${GNURADIO_CORE_INCLUDE_DIRS} + ${GRUEL_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${FFTW3F_INCLUDE_DIRS} ) -- cgit