GNU Radio 3.6.5 C++ API

firdes.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2002,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 _FILTER_FIRDES_H_
00024 #define _FILTER_FIRDES_H_
00025 
00026 #include <filter/api.h>
00027 #include <vector>
00028 #include <cmath>
00029 #include <gr_complex.h>
00030 
00031 namespace gr {
00032   namespace filter {
00033 
00034     /*!
00035      * \brief Finite Impulse Response (FIR) filter design functions.
00036      * \ingroup filter_design
00037      */
00038     
00039     class FILTER_API firdes {
00040     public:
00041             
00042       enum win_type {
00043         WIN_HAMMING = 0,        // max attenuation 53 dB
00044         WIN_HANN = 1,   // max attenuation 44 dB
00045         WIN_BLACKMAN = 2,       // max attenuation 74 dB
00046         WIN_RECTANGULAR = 3,
00047         WIN_KAISER = 4,     // max attenuation a function of beta, google it
00048         WIN_BLACKMAN_hARRIS = 5,
00049         WIN_BLACKMAN_HARRIS = 5, // alias for capitalization consistency
00050       };
00051 
00052 
00053       // ... class methods ...
00054       
00055       /*!
00056        * \brief use "window method" to design a low-pass FIR filter
00057        *
00058        * \p gain:               overall gain of filter (typically 1.0)
00059        * \p sampling_freq:      sampling freq (Hz)
00060        * \p cutoff_freq:        center of transition band (Hz)
00061        * \p transition_width:   width of transition band (Hz).
00062        *                        The normalized width of the transition
00063        *                        band is what sets the number of taps
00064        *                        required.  Narrow --> more taps
00065        * \p window_type:        What kind of window to use. Determines
00066        *                        maximum attenuation and passband ripple.
00067        * \p beta:               parameter for Kaiser window
00068        */
00069       static std::vector<float>
00070         low_pass(double gain,
00071                  double sampling_freq,
00072                  double cutoff_freq,            // Hz center of transition band
00073                  double transition_width,       // Hz width of transition band
00074                  win_type window = WIN_HAMMING,
00075                  double beta = 6.76);           // used only with Kaiser
00076       
00077       /*!
00078        * \brief use "window method" to design a low-pass FIR filter
00079        *
00080        * \p gain:               overall gain of filter (typically 1.0)
00081        * \p sampling_freq:      sampling freq (Hz)
00082        * \p cutoff_freq:        center of transition band (Hz)
00083        * \p transition_width:   width of transition band (Hz).
00084        * \p attenuation_dB      required stopband attenuation
00085        *                        The normalized width of the transition
00086        *                        band and the required stop band
00087        *                        attenuation is what sets the number of taps
00088        *                        required.  Narrow --> more taps
00089        *                        More attenuatin --> more taps
00090        * \p window_type:        What kind of window to use. Determines
00091        *                        maximum attenuation and passband ripple.
00092        * \p beta:               parameter for Kaiser window
00093        */
00094 
00095       static std::vector<float>
00096         low_pass_2(double gain,
00097                    double sampling_freq,
00098                    double cutoff_freq,          // Hz beginning transition band
00099                    double transition_width,     // Hz width of transition band
00100                    double attenuation_dB,       // out of band attenuation dB
00101                    win_type window = WIN_HAMMING,
00102                    double beta = 6.76);         // used only with Kaiser
00103 
00104       /*!
00105        * \brief use "window method" to design a high-pass FIR filter
00106        *
00107        * \p gain:               overall gain of filter (typically 1.0)
00108        * \p sampling_freq:      sampling freq (Hz)
00109        * \p cutoff_freq:        center of transition band (Hz)
00110        * \p transition_width:   width of transition band (Hz).
00111        *                        The normalized width of the transition
00112        *                        band is what sets the number of taps
00113        *                        required.  Narrow --> more taps
00114        * \p window_type:        What kind of window to use. Determines
00115        *                        maximum attenuation and passband ripple.
00116        * \p beta:               parameter for Kaiser window
00117        */
00118       
00119       static std::vector<float>
00120         high_pass(double gain,
00121                   double sampling_freq,
00122                   double cutoff_freq,           // Hz center of transition band
00123                   double transition_width,      // Hz width of transition band
00124                   win_type window = WIN_HAMMING,
00125                   double beta = 6.76);          // used only with Kaiser
00126 
00127       /*!
00128        * \brief use "window method" to design a high-pass FIR filter
00129        *
00130        * \p gain:               overall gain of filter (typically 1.0)
00131        * \p sampling_freq:      sampling freq (Hz)
00132        * \p cutoff_freq:        center of transition band (Hz)
00133        * \p transition_width:   width of transition band (Hz).
00134        * \p attenuation_dB      out of band attenuation
00135        *                        The normalized width of the transition
00136        *                        band and the required stop band
00137        *                        attenuation is what sets the number of taps
00138        *                        required.  Narrow --> more taps
00139        *                        More attenuation --> more taps
00140        * \p window_type:        What kind of window to use. Determines
00141        *                        maximum attenuation and passband ripple.
00142        * \p beta:               parameter for Kaiser window
00143        */
00144       
00145       static std::vector<float>
00146         high_pass_2(double gain,
00147                     double sampling_freq,
00148                     double cutoff_freq,         // Hz center of transition band
00149                     double transition_width,    // Hz width of transition band
00150                     double attenuation_dB,      // out of band attenuation dB
00151                     win_type window = WIN_HAMMING,
00152                     double beta = 6.76);        // used only with Kaiser
00153       
00154       /*!
00155        * \brief use "window method" to design a band-pass FIR filter
00156        *
00157        * \p gain:               overall gain of filter (typically 1.0)
00158        * \p sampling_freq:      sampling freq (Hz)
00159        * \p low_cutoff_freq:    center of transition band (Hz)
00160        * \p high_cutoff_freq:   center of transition band (Hz)
00161        * \p transition_width:   width of transition band (Hz).
00162        *                        The normalized width of the transition
00163        *                        band is what sets the number of taps
00164        *                        required.  Narrow --> more taps
00165        * \p window_type:        What kind of window to use. Determines
00166        *                        maximum attenuation and passband ripple.
00167        * \p beta:               parameter for Kaiser window
00168        */
00169       static std::vector<float>
00170         band_pass(double gain,
00171                   double sampling_freq,
00172                   double low_cutoff_freq,       // Hz center of transition band
00173                   double high_cutoff_freq,      // Hz center of transition band
00174                   double transition_width,      // Hz width of transition band
00175                   win_type window = WIN_HAMMING,
00176                   double beta = 6.76);          // used only with Kaiser
00177       
00178       /*!
00179        * \brief use "window method" to design a band-pass FIR filter
00180        *
00181        * \p gain:               overall gain of filter (typically 1.0)
00182        * \p sampling_freq:      sampling freq (Hz)
00183        * \p low_cutoff_freq:    center of transition band (Hz)
00184        * \p high_cutoff_freq:   center of transition band (Hz)
00185        * \p transition_width:   width of transition band (Hz).
00186        * \p attenuation_dB      out of band attenuation
00187        *                        The normalized width of the transition
00188        *                        band and the required stop band
00189        *                        attenuation is what sets the number of taps
00190        *                        required.  Narrow --> more taps
00191        *                        More attenuation --> more taps
00192        * \p window_type:        What kind of window to use. Determines
00193        *                        maximum attenuation and passband ripple.
00194        * \p beta:               parameter for Kaiser window
00195        */
00196       
00197       static std::vector<float>
00198         band_pass_2(double gain,
00199                     double sampling_freq,
00200                     double low_cutoff_freq,     // Hz beginning transition band
00201                     double high_cutoff_freq,    // Hz beginning transition band
00202                     double transition_width,    // Hz width of transition band
00203                     double attenuation_dB,      // out of band attenuation dB
00204                     win_type window = WIN_HAMMING,
00205                     double beta = 6.76);        // used only with Kaiser
00206       
00207       /*!
00208        * \brief use "window method" to design a complex band-pass FIR filter
00209        *
00210        * \p gain:               overall gain of filter (typically 1.0)
00211        * \p sampling_freq:      sampling freq (Hz)
00212        * \p low_cutoff_freq:    center of transition band (Hz)
00213        * \p high_cutoff_freq:   center of transition band (Hz)
00214        * \p transition_width:   width of transition band (Hz).
00215        *                        The normalized width of the transition
00216        *                        band is what sets the number of taps
00217        *                        required.  Narrow --> more taps
00218        * \p window_type:        What kind of window to use. Determines
00219        *                        maximum attenuation and passband ripple.
00220        * \p beta:               parameter for Kaiser window
00221        */
00222       static std::vector<gr_complex>
00223         complex_band_pass(double gain,
00224                           double sampling_freq,
00225                           double low_cutoff_freq,       // Hz center of transition band
00226                           double high_cutoff_freq,      // Hz center of transition band
00227                           double transition_width,      // Hz width of transition band
00228                           win_type window = WIN_HAMMING,
00229                           double beta = 6.76);          // used only with Kaiser
00230       
00231       /*!
00232        * \brief use "window method" to design a complex band-pass FIR filter
00233        *
00234        * \p gain:               overall gain of filter (typically 1.0)
00235        * \p sampling_freq:      sampling freq (Hz)
00236        * \p low_cutoff_freq:    center of transition band (Hz)
00237        * \p high_cutoff_freq:   center of transition band (Hz)
00238        * \p transition_width:   width of transition band (Hz).
00239        * \p attenuation_dB      out of band attenuation
00240        *                        The normalized width of the transition
00241        *                        band and the required stop band
00242        *                        attenuation is what sets the number of taps
00243        *                        required.  Narrow --> more taps
00244        *                        More attenuation --> more taps
00245        * \p window_type:        What kind of window to use. Determines
00246        *                        maximum attenuation and passband ripple.
00247        * \p beta:               parameter for Kaiser window
00248        */
00249       
00250       static std::vector<gr_complex>
00251         complex_band_pass_2(double gain,
00252                             double sampling_freq,
00253                             double low_cutoff_freq,     // Hz beginning transition band
00254                             double high_cutoff_freq,    // Hz beginning transition band
00255                             double transition_width,    // Hz width of transition band
00256                             double attenuation_dB,      // out of band attenuation dB
00257                             win_type window = WIN_HAMMING,
00258                             double beta = 6.76);        // used only with Kaiser
00259       
00260       /*!
00261        * \brief use "window method" to design a band-reject FIR filter
00262        *
00263        * \p gain:               overall gain of filter (typically 1.0)
00264        * \p sampling_freq:      sampling freq (Hz)
00265        * \p low_cutoff_freq:    center of transition band (Hz)
00266        * \p high_cutoff_freq:   center of transition band (Hz)
00267        * \p transition_width:   width of transition band (Hz).
00268        *                        The normalized width of the transition
00269        *                        band is what sets the number of taps
00270        *                        required.  Narrow --> more taps
00271        * \p window_type:        What kind of window to use. Determines
00272        *                        maximum attenuation and passband ripple.
00273        * \p beta:               parameter for Kaiser window
00274        */
00275       
00276       static std::vector<float>
00277         band_reject(double gain,
00278                     double sampling_freq,
00279                     double low_cutoff_freq,     // Hz center of transition band
00280                     double high_cutoff_freq,    // Hz center of transition band
00281                     double transition_width,    // Hz width of transition band
00282                     win_type window = WIN_HAMMING,
00283                     double beta = 6.76);        // used only with Kaiser
00284       
00285       /*!
00286        * \brief use "window method" to design a band-reject FIR filter
00287        *
00288        * \p gain:               overall gain of filter (typically 1.0)
00289        * \p sampling_freq:      sampling freq (Hz)
00290        * \p low_cutoff_freq:    center of transition band (Hz)
00291        * \p high_cutoff_freq:   center of transition band (Hz)
00292        * \p transition_width:   width of transition band (Hz).
00293        * \p attenuation_dB      out of band attenuation
00294        *                        The normalized width of the transition
00295        *                        band and the required stop band
00296        *                        attenuation is what sets the number of taps
00297        *                        required.  Narrow --> more taps
00298        *                        More attenuation --> more taps
00299        * \p window_type:        What kind of window to use. Determines
00300        *                        maximum attenuation and passband ripple.
00301        * \p beta:               parameter for Kaiser window
00302        */
00303       
00304       static std::vector<float>
00305         band_reject_2(double gain,
00306                       double sampling_freq,
00307                       double low_cutoff_freq,   // Hz beginning transition band
00308                       double high_cutoff_freq,  // Hz beginning transition band
00309                       double transition_width,  // Hz width of transition band
00310                       double attenuation_dB,    // out of band attenuation dB
00311                       win_type window = WIN_HAMMING,
00312                       double beta = 6.76);      // used only with Kaiser
00313       
00314       /*!\brief design a Hilbert Transform Filter
00315        *
00316        * \p ntaps:              Number of taps, must be odd
00317        * \p window_type:        What kind of window to use
00318        * \p beta:               Only used for Kaiser
00319        */
00320       static std::vector<float>
00321         hilbert(unsigned int ntaps = 19,
00322                 win_type windowtype = WIN_RECTANGULAR,
00323                 double beta = 6.76);
00324       
00325       /*!
00326        * \brief design a Root Cosine FIR Filter (do we need a window?)
00327        *
00328        * \p gain:               overall gain of filter (typically 1.0)
00329        * \p sampling_freq:      sampling freq (Hz)
00330        * \p symbol rate:        symbol rate, must be a factor of sample rate
00331        * \p alpha:              excess bandwidth factor
00332        * \p ntaps:              number of taps
00333        */
00334       static std::vector<float>
00335         root_raised_cosine(double gain,
00336                            double sampling_freq,
00337                            double symbol_rate, // Symbol rate, NOT bitrate (unless BPSK)
00338                            double alpha,       // Excess Bandwidth Factor
00339                            int ntaps);
00340       
00341       /*!
00342        * \brief design a Gaussian filter
00343        *
00344        * \p gain:               overall gain of filter (typically 1.0)
00345        * \p symbols per bit:    symbol rate, must be a factor of sample rate
00346        * \p ntaps:              number of taps
00347        */
00348       static std::vector<float>
00349         gaussian(double gain,
00350                  double spb,
00351                  double bt,     // Bandwidth to bitrate ratio
00352                  int ntaps);
00353       
00354       // window functions ...
00355       static std::vector<float> window (win_type type, int ntaps, double beta);
00356       
00357     private:
00358       static double bessi0(double x);
00359       static void sanity_check_1f(double sampling_freq, double f1,
00360                                   double transition_width);
00361       static void sanity_check_2f(double sampling_freq, double f1, double f2,
00362                                   double transition_width);
00363       static void sanity_check_2f_c(double sampling_freq, double f1, double f2,
00364                                     double transition_width);
00365       
00366       static int compute_ntaps(double sampling_freq,
00367                                double transition_width,
00368                                win_type window_type, double beta);
00369       
00370       static int compute_ntaps_windes(double sampling_freq,
00371                                       double transition_width,
00372                                       double attenuation_dB);
00373     };
00374     
00375   } /* namespace filter */
00376 } /* namespace gr */
00377 
00378 #endif /* _FILTER_FIRDES_H_ */