Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_firdes.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifndef _GR_FIRDES_H_
24 #define _GR_FIRDES_H_
25
26 #include <vector>
27 #include <cmath>
28 #include <gr_complex.h>
29
30 /*!
31  * \brief Finite Impulse Response (FIR) filter design functions.
32  */
33
34 class gr_firdes {
35  public:
36
37   enum win_type {
38     WIN_HAMMING = 0,    // max attenuation 53 dB
39     WIN_HANN = 1,       // max attenuation 44 dB
40     WIN_BLACKMAN = 2,   // max attenuation 74 dB
41     WIN_RECTANGULAR = 3,
42     WIN_KAISER = 4      // max attenuation a function of beta, google it
43   };
44
45
46   // ... class methods ...
47
48   /*!
49    * \brief use "window method" to design a low-pass FIR filter
50    *
51    * \p gain:                   overall gain of filter (typically 1.0)
52    * \p sampling_freq:          sampling freq (Hz)
53    * \p cutoff_freq:            center of transition band (Hz)
54    * \p transition_width:       width of transition band (Hz).
55    *                            The normalized width of the transition
56    *                            band is what sets the number of taps
57    *                            required.  Narrow --> more taps
58    * \p window_type:            What kind of window to use. Determines
59    *                            maximum attenuation and passband ripple.
60    * \p beta:                   parameter for Kaiser window
61    */
62   static std::vector<float>
63   low_pass (double gain,
64             double sampling_freq,
65             double cutoff_freq,         // Hz center of transition band
66             double transition_width,    // Hz width of transition band
67             win_type window = WIN_HAMMING,
68             double beta = 6.76);                // used only with Kaiser
69
70   /*!
71    * \brief use "window method" to design a high-pass FIR filter
72    *
73    * \p gain:                   overall gain of filter (typically 1.0)
74    * \p sampling_freq:          sampling freq (Hz)
75    * \p cutoff_freq:            center of transition band (Hz)
76    * \p transition_width:       width of transition band (Hz).
77    *                            The normalized width of the transition
78    *                            band is what sets the number of taps
79    *                            required.  Narrow --> more taps
80    * \p window_type:            What kind of window to use. Determines
81    *                            maximum attenuation and passband ripple.
82    * \p beta:                   parameter for Kaiser window
83    */
84   static std::vector<float>
85   high_pass (double gain,
86              double sampling_freq,
87              double cutoff_freq,                // Hz center of transition band
88              double transition_width,           // Hz width of transition band
89              win_type window = WIN_HAMMING,
90              double beta = 6.76);               // used only with Kaiser
91
92   /*!
93    * \brief use "window method" to design a band-pass FIR filter
94    *
95    * \p gain:                   overall gain of filter (typically 1.0)
96    * \p sampling_freq:          sampling freq (Hz)
97    * \p low_cutoff_freq:        center of transition band (Hz)
98    * \p high_cutoff_freq:       center of transition band (Hz)
99    * \p transition_width:       width of transition band (Hz).
100    *                            The normalized width of the transition
101    *                            band is what sets the number of taps
102    *                            required.  Narrow --> more taps
103    * \p window_type:            What kind of window to use. Determines
104    *                            maximum attenuation and passband ripple.
105    * \p beta:                   parameter for Kaiser window
106    */
107   static std::vector<float>
108   band_pass (double gain,
109              double sampling_freq,
110              double low_cutoff_freq,            // Hz center of transition band
111              double high_cutoff_freq,           // Hz center of transition band
112              double transition_width,           // Hz width of transition band
113              win_type window = WIN_HAMMING,
114              double beta = 6.76);               // used only with Kaiser
115
116
117   /*!
118    * \brief use "window method" to design a complex band-pass FIR filter
119    *
120    * \p gain:                   overall gain of filter (typically 1.0)
121    * \p sampling_freq:          sampling freq (Hz)
122    * \p low_cutoff_freq:        center of transition band (Hz)
123    * \p high_cutoff_freq:       center of transition band (Hz)
124    * \p transition_width:       width of transition band (Hz).
125    *                            The normalized width of the transition
126    *                            band is what sets the number of taps
127    *                            required.  Narrow --> more taps
128    * \p window_type:            What kind of window to use. Determines
129    *                            maximum attenuation and passband ripple.
130    * \p beta:                   parameter for Kaiser window
131    */
132
133   static std::vector<gr_complex>
134   complex_band_pass (double gain,
135              double sampling_freq,
136              double low_cutoff_freq,            // Hz center of transition band
137              double high_cutoff_freq,           // Hz center of transition band
138              double transition_width,           // Hz width of transition band
139              win_type window = WIN_HAMMING,
140              double beta = 6.76);               // used only with Kaiser
141
142
143   /*!
144    * \brief use "window method" to design a band-reject FIR filter
145    *
146    * \p gain:                   overall gain of filter (typically 1.0)
147    * \p sampling_freq:          sampling freq (Hz)
148    * \p low_cutoff_freq:        center of transition band (Hz)
149    * \p high_cutoff_freq:       center of transition band (Hz)
150    * \p transition_width:       width of transition band (Hz).
151    *                            The normalized width of the transition
152    *                            band is what sets the number of taps
153    *                            required.  Narrow --> more taps
154    * \p window_type:            What kind of window to use. Determines
155    *                            maximum attenuation and passband ripple.
156    * \p beta:                   parameter for Kaiser window
157    */
158
159   static std::vector<float>
160   band_reject (double gain,
161                double sampling_freq,
162                double low_cutoff_freq,          // Hz center of transition band
163                double high_cutoff_freq,         // Hz center of transition band
164                double transition_width,         // Hz width of transition band
165                win_type window = WIN_HAMMING,
166                double beta = 6.76);             // used only with Kaiser
167
168   /*!\brief design a Hilbert Transform Filter
169    *
170    * \p ntaps:                  Number of taps, must be odd
171    * \p window_type:            What kind of window to use
172    * \p beta:                   Only used for Kaiser
173    */
174   static std::vector<float>
175   hilbert (unsigned int ntaps,
176            win_type windowtype = WIN_RECTANGULAR,
177            double beta = 6.76);
178    
179   /*!
180    * \brief design a Root Cosine FIR Filter (do we need a window?)
181    *
182    * \p gain:                   overall gain of filter (typically 1.0)
183    * \p sampling_freq:          sampling freq (Hz)
184    * \p symbol rate:            symbol rate, must be a factor of sample rate
185    * \p alpha:                  excess bandwidth factor
186    * \p ntaps:                  number of taps
187    */
188   static std::vector<float>
189   root_raised_cosine (double gain,
190                       double sampling_freq,
191                       double symbol_rate,       // Symbol rate, NOT bitrate (unless BPSK)
192                       double alpha,             // Excess Bandwidth Factor
193                       int ntaps);
194
195   /*!
196    * \brief design a Gaussian filter
197    *
198    * \p gain:                   overall gain of filter (typically 1.0)
199    * \p symbols per bit:        symbol rate, must be a factor of sample rate
200    * \p ntaps:                  number of taps
201    */
202   static std::vector<float>
203   gaussian (double gain,
204             double spb,       
205             double bt,              // Bandwidth to bitrate ratio
206             int ntaps);
207
208   // window functions ...
209   static std::vector<float> window (win_type type, int ntaps, double beta);
210
211 private:
212   static double bessi0(double x);
213   static void sanity_check_1f (double sampling_freq, double f1,
214                                double transition_width);
215   static void sanity_check_2f (double sampling_freq, double f1, double f2,
216                                double transition_width);
217   static void sanity_check_2f_c (double sampling_freq, double f1, double f2,
218                                double transition_width);
219
220   static int compute_ntaps (double sampling_freq,
221                             double transition_width,
222                             win_type window_type, double beta);
223 };
224
225 #endif