GNU Radio 3.6.5 C++ API

fft.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2003,2008,2012 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef _FFT_FFT_H_
00024 #define _FFT_FFT_H_
00025 
00026 /*
00027  * Wrappers for FFTW single precision 1d dft
00028  */
00029 
00030 #include <fft/api.h>
00031 #include <gr_complex.h>
00032 #include <boost/thread.hpp>
00033 
00034 namespace gr {
00035   namespace fft {
00036 
00037 
00038     /*! \brief Helper function for allocating complex fft buffers
00039      */
00040     FFT_API gr_complex* malloc_complex(int size);
00041 
00042     /*! \brief Helper function for allocating float fft buffers
00043      */
00044     FFT_API float* malloc_float(int size);
00045     
00046     /*! \brief Helper function for freeing fft buffers
00047      */
00048     FFT_API void free(void *b);
00049 
00050     /*!
00051      * \brief Export reference to planner mutex for those apps that
00052      * want to use FFTW w/o using the fft_impl_fftw* classes.
00053      */
00054     class FFT_API planner {
00055     public:
00056       typedef boost::mutex::scoped_lock scoped_lock;
00057       /*!
00058        * Return reference to planner mutex
00059        */
00060       static boost::mutex &mutex();
00061     };
00062 
00063     /*!
00064      * \brief FFT: complex in, complex out
00065      * \ingroup misc
00066      */
00067     class FFT_API fft_complex {
00068       int             d_fft_size;
00069       int         d_nthreads;
00070       gr_complex *d_inbuf;
00071       gr_complex *d_outbuf;
00072       void           *d_plan;
00073 
00074     public:
00075       fft_complex(int fft_size, bool forward = true, int nthreads=1);
00076       virtual ~fft_complex();
00077       
00078       /*
00079        * These return pointers to buffers owned by fft_impl_fft_complex
00080        * into which input and output take place. It's done this way in
00081        * order to ensure optimal alignment for SIMD instructions.
00082        */
00083       gr_complex *get_inbuf()  const { return d_inbuf; }
00084       gr_complex *get_outbuf() const { return d_outbuf; }
00085       
00086       int inbuf_length()  const { return d_fft_size; }
00087       int outbuf_length() const { return d_fft_size; }
00088       
00089       /*!
00090        *  Set the number of threads to use for caclulation.
00091        */
00092       void set_nthreads(int n);
00093       
00094       /*!
00095        *  Get the number of threads being used by FFTW
00096        */
00097       int nthreads() const { return d_nthreads; }
00098       
00099       /*!
00100        * compute FFT. The input comes from inbuf, the output is placed in
00101        * outbuf.
00102        */
00103       void execute();
00104     };
00105 
00106     /*!
00107      * \brief FFT: real in, complex out
00108      * \ingroup misc
00109      */
00110     class FFT_API fft_real_fwd {
00111       int         d_fft_size;
00112       int         d_nthreads;
00113       float      *d_inbuf;
00114       gr_complex *d_outbuf;
00115       void       *d_plan;
00116 
00117     public:
00118       fft_real_fwd (int fft_size, int nthreads=1);
00119       virtual ~fft_real_fwd ();
00120       
00121       /*
00122        * These return pointers to buffers owned by fft_impl_fft_real_fwd
00123        * into which input and output take place. It's done this way in
00124        * order to ensure optimal alignment for SIMD instructions.
00125        */
00126       float *get_inbuf()      const { return d_inbuf; }
00127       gr_complex *get_outbuf() const { return d_outbuf; }
00128       
00129       int inbuf_length()  const { return d_fft_size; }
00130       int outbuf_length() const { return d_fft_size / 2 + 1; }
00131       
00132       /*!
00133        *  Set the number of threads to use for caclulation.
00134        */
00135       void set_nthreads(int n);
00136       
00137       /*!
00138        *  Get the number of threads being used by FFTW
00139        */
00140       int nthreads() const { return d_nthreads; }
00141       
00142       /*!
00143        * compute FFT. The input comes from inbuf, the output is placed in
00144        * outbuf.
00145        */
00146       void execute();
00147     };
00148 
00149     /*!
00150      * \brief FFT: complex in, float out
00151      * \ingroup misc
00152      */
00153     class FFT_API fft_real_rev {
00154       int             d_fft_size;
00155       int         d_nthreads;
00156       gr_complex *d_inbuf;
00157       float          *d_outbuf;
00158       void           *d_plan;
00159       
00160     public:
00161       fft_real_rev(int fft_size, int nthreads=1);
00162       virtual ~fft_real_rev();
00163       
00164       /*
00165        * These return pointers to buffers owned by fft_impl_fft_real_rev
00166        * into which input and output take place. It's done this way in
00167        * order to ensure optimal alignment for SIMD instructions.
00168        */
00169       gr_complex *get_inbuf() const { return d_inbuf; }
00170       float *get_outbuf() const { return d_outbuf; }
00171       
00172       int inbuf_length()  const { return d_fft_size / 2 + 1; }
00173       int outbuf_length() const { return d_fft_size; }
00174       
00175       /*!
00176        *  Set the number of threads to use for caclulation.
00177        */
00178       void set_nthreads(int n);
00179       
00180       /*!
00181        *  Get the number of threads being used by FFTW
00182        */
00183       int nthreads() const { return d_nthreads; }
00184       
00185       /*!
00186        * compute FFT. The input comes from inbuf, the output is placed in
00187        * outbuf.
00188        */
00189       void execute();
00190     };
00191 
00192   } /* namespace fft */
00193 } /*namespace gr */
00194 
00195 #endif /* _FFT_FFT_H_ */