GNU Radio 3.6.5 C++ API

gr_firdes.h

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