GNU Radio 3.7.0 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 <gnuradio/fft/api.h>
00031 #include <gnuradio/gr_complex.h>
00032 #include <boost/thread.hpp>
00033 
00034 namespace gr {
00035   namespace fft {
00036 
00037 
00038     /*! \brief Helper function for allocating complex* buffers
00039      */
00040     FFT_API gr_complex* malloc_complex(int size);
00041 
00042     /*! \brief Helper function for allocating float* buffers
00043      */
00044     FFT_API float* malloc_float(int size);
00045     
00046     /*! \brief Helper function for allocating double* buffers
00047      */
00048     FFT_API double* malloc_double(int size);
00049 
00050     /*! \brief Helper function for freeing fft buffers
00051      */
00052     FFT_API void free(void *b);
00053 
00054     /*!
00055      * \brief Export reference to planner mutex for those apps that
00056      * want to use FFTW w/o using the fft_impl_fftw* classes.
00057      */
00058     class FFT_API planner {
00059     public:
00060       typedef boost::mutex::scoped_lock scoped_lock;
00061       /*!
00062        * Return reference to planner mutex
00063        */
00064       static boost::mutex &mutex();
00065     };
00066 
00067     /*!
00068      * \brief FFT: complex in, complex out
00069      * \ingroup misc
00070      */
00071     class FFT_API fft_complex {
00072       int             d_fft_size;
00073       int         d_nthreads;
00074       gr_complex *d_inbuf;
00075       gr_complex *d_outbuf;
00076       void           *d_plan;
00077 
00078     public:
00079       fft_complex(int fft_size, bool forward = true, int nthreads=1);
00080       virtual ~fft_complex();
00081       
00082       /*
00083        * These return pointers to buffers owned by fft_impl_fft_complex
00084        * into which input and output take place. It's done this way in
00085        * order to ensure optimal alignment for SIMD instructions.
00086        */
00087       gr_complex *get_inbuf()  const { return d_inbuf; }
00088       gr_complex *get_outbuf() const { return d_outbuf; }
00089       
00090       int inbuf_length()  const { return d_fft_size; }
00091       int outbuf_length() const { return d_fft_size; }
00092       
00093       /*!
00094        *  Set the number of threads to use for caclulation.
00095        */
00096       void set_nthreads(int n);
00097       
00098       /*!
00099        *  Get the number of threads being used by FFTW
00100        */
00101       int nthreads() const { return d_nthreads; }
00102       
00103       /*!
00104        * compute FFT. The input comes from inbuf, the output is placed in
00105        * outbuf.
00106        */
00107       void execute();
00108     };
00109 
00110     /*!
00111      * \brief FFT: real in, complex out
00112      * \ingroup misc
00113      */
00114     class FFT_API fft_real_fwd {
00115       int         d_fft_size;
00116       int         d_nthreads;
00117       float      *d_inbuf;
00118       gr_complex *d_outbuf;
00119       void       *d_plan;
00120 
00121     public:
00122       fft_real_fwd (int fft_size, int nthreads=1);
00123       virtual ~fft_real_fwd ();
00124       
00125       /*
00126        * These return pointers to buffers owned by fft_impl_fft_real_fwd
00127        * into which input and output take place. It's done this way in
00128        * order to ensure optimal alignment for SIMD instructions.
00129        */
00130       float *get_inbuf()      const { return d_inbuf; }
00131       gr_complex *get_outbuf() const { return d_outbuf; }
00132       
00133       int inbuf_length()  const { return d_fft_size; }
00134       int outbuf_length() const { return d_fft_size / 2 + 1; }
00135       
00136       /*!
00137        *  Set the number of threads to use for caclulation.
00138        */
00139       void set_nthreads(int n);
00140       
00141       /*!
00142        *  Get the number of threads being used by FFTW
00143        */
00144       int nthreads() const { return d_nthreads; }
00145       
00146       /*!
00147        * compute FFT. The input comes from inbuf, the output is placed in
00148        * outbuf.
00149        */
00150       void execute();
00151     };
00152 
00153     /*!
00154      * \brief FFT: complex in, float out
00155      * \ingroup misc
00156      */
00157     class FFT_API fft_real_rev {
00158       int             d_fft_size;
00159       int         d_nthreads;
00160       gr_complex *d_inbuf;
00161       float          *d_outbuf;
00162       void           *d_plan;
00163       
00164     public:
00165       fft_real_rev(int fft_size, int nthreads=1);
00166       virtual ~fft_real_rev();
00167       
00168       /*
00169        * These return pointers to buffers owned by fft_impl_fft_real_rev
00170        * into which input and output take place. It's done this way in
00171        * order to ensure optimal alignment for SIMD instructions.
00172        */
00173       gr_complex *get_inbuf() const { return d_inbuf; }
00174       float *get_outbuf() const { return d_outbuf; }
00175       
00176       int inbuf_length()  const { return d_fft_size / 2 + 1; }
00177       int outbuf_length() const { return d_fft_size; }
00178       
00179       /*!
00180        *  Set the number of threads to use for caclulation.
00181        */
00182       void set_nthreads(int n);
00183       
00184       /*!
00185        *  Get the number of threads being used by FFTW
00186        */
00187       int nthreads() const { return d_nthreads; }
00188       
00189       /*!
00190        * compute FFT. The input comes from inbuf, the output is placed in
00191        * outbuf.
00192        */
00193       void execute();
00194     };
00195 
00196   } /* namespace fft */
00197 } /*namespace gr */
00198 
00199 #endif /* _FFT_FFT_H_ */