Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_firdes.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2008 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 3, 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  * \ingroup filter_design
33  */
34
35 class gr_firdes {
36  public:
37
38   enum win_type {
39     WIN_HAMMING = 0,    // max attenuation 53 dB
40     WIN_HANN = 1,       // max attenuation 44 dB
41     WIN_BLACKMAN = 2,   // max attenuation 74 dB
42     WIN_RECTANGULAR = 3,
43     WIN_KAISER = 4,     // max attenuation a function of beta, google it
44     WIN_BLACKMAN_hARRIS = 5,
45   };
46
47
48   // ... class methods ...
49
50   /*!
51    * \brief use "window method" to design a low-pass FIR filter
52    *
53    * \p gain:                   overall gain of filter (typically 1.0)
54    * \p sampling_freq:          sampling freq (Hz)
55    * \p cutoff_freq:            center of transition band (Hz)
56    * \p transition_width:       width of transition band (Hz).
57    *                            The normalized width of the transition
58    *                            band is what sets the number of taps
59    *                            required.  Narrow --> more taps
60    * \p window_type:            What kind of window to use. Determines
61    *                            maximum attenuation and passband ripple.
62    * \p beta:                   parameter for Kaiser window
63    */
64   static std::vector<float>
65   low_pass (double gain,
66             double sampling_freq,
67             double cutoff_freq,         // Hz center of transition band
68             double transition_width,    // Hz width of transition band
69             win_type window = WIN_HAMMING,
70             double beta = 6.76);                // used only with Kaiser
71
72   /*!
73    * \brief use "window method" to design a low-pass FIR filter
74    *
75    * \p gain:                   overall gain of filter (typically 1.0)
76    * \p sampling_freq:          sampling freq (Hz)
77    * \p cutoff_freq:            center of transition band (Hz)
78    * \p transition_width:       width of transition band (Hz).
79    * \p attenuation_dB          required stopband attenuation
80    *                            The normalized width of the transition
81    *                            band and the required stop band
82    *                            attenuation is what sets the number of taps
83    *                            required.  Narrow --> more taps
84    *                            More attenuatin --> more taps
85    * \p window_type:            What kind of window to use. Determines
86    *                            maximum attenuation and passband ripple.
87    * \p beta:                   parameter for Kaiser window
88    */
89
90   static std::vector<float>
91   low_pass_2 (double gain,
92             double sampling_freq,
93             double cutoff_freq,         // Hz beginning transition band
94             double transition_width,    // Hz width of transition band
95             double attenuation_dB,      // out of band attenuation dB
96             win_type window = WIN_HAMMING,
97             double beta = 6.76);                // used only with Kaiser
98
99   /*!
100    * \brief use "window method" to design a high-pass FIR filter
101    *
102    * \p gain:                   overall gain of filter (typically 1.0)
103    * \p sampling_freq:          sampling freq (Hz)
104    * \p cutoff_freq:            center of transition band (Hz)
105    * \p transition_width:       width of transition band (Hz).
106    *                            The normalized width of the transition
107    *                            band is what sets the number of taps
108    *                            required.  Narrow --> more taps
109    * \p window_type:            What kind of window to use. Determines
110    *                            maximum attenuation and passband ripple.
111    * \p beta:                   parameter for Kaiser window
112    */
113
114   static std::vector<float>
115   high_pass (double gain,
116              double sampling_freq,
117              double cutoff_freq,                // Hz center of transition band
118              double transition_width,           // Hz width of transition band
119              win_type window = WIN_HAMMING,
120              double beta = 6.76);               // used only with Kaiser
121
122   /*!
123    * \brief use "window method" to design a high-pass FIR filter
124    *
125    * \p gain:                   overall gain of filter (typically 1.0)
126    * \p sampling_freq:          sampling freq (Hz)
127    * \p cutoff_freq:            center of transition band (Hz)
128    * \p transition_width:       width of transition band (Hz).
129    * \p attenuation_dB          out of band attenuation
130    *                            The normalized width of the transition
131    *                            band and the required stop band
132    *                            attenuation is what sets the number of taps
133    *                            required.  Narrow --> more taps
134    *                            More attenuation --> more taps
135    * \p window_type:            What kind of window to use. Determines
136    *                            maximum attenuation and passband ripple.
137    * \p beta:                   parameter for Kaiser window
138    */
139
140   static std::vector<float>
141   high_pass_2 (double gain,
142              double sampling_freq,
143              double cutoff_freq,                // Hz center of transition band
144              double transition_width,           // Hz width of transition band
145              double attenuation_dB,             // out of band attenuation dB
146              win_type window = WIN_HAMMING,
147              double beta = 6.76);               // used only with Kaiser
148
149   /*!
150    * \brief use "window method" to design a band-pass FIR filter
151    *
152    * \p gain:                   overall gain of filter (typically 1.0)
153    * \p sampling_freq:          sampling freq (Hz)
154    * \p low_cutoff_freq:        center of transition band (Hz)
155    * \p high_cutoff_freq:       center of transition band (Hz)
156    * \p transition_width:       width of transition band (Hz).
157    *                            The normalized width of the transition
158    *                            band is what sets the number of taps
159    *                            required.  Narrow --> more taps
160    * \p window_type:            What kind of window to use. Determines
161    *                            maximum attenuation and passband ripple.
162    * \p beta:                   parameter for Kaiser window
163    */
164   static std::vector<float>
165   band_pass (double gain,
166              double sampling_freq,
167              double low_cutoff_freq,            // Hz center of transition band
168              double high_cutoff_freq,           // Hz center of transition band
169              double transition_width,           // Hz width of transition band
170              win_type window = WIN_HAMMING,
171              double beta = 6.76);               // used only with Kaiser
172
173   /*!
174    * \brief use "window method" to design a band-pass FIR filter
175    *
176    * \p gain:                   overall gain of filter (typically 1.0)
177    * \p sampling_freq:          sampling freq (Hz)
178    * \p low_cutoff_freq:        center of transition band (Hz)
179    * \p high_cutoff_freq:       center of transition band (Hz)
180    * \p transition_width:       width of transition band (Hz).
181    * \p attenuation_dB          out of band attenuation
182    *                            The normalized width of the transition
183    *                            band and the required stop band
184    *                            attenuation is what sets the number of taps
185    *                            required.  Narrow --> more taps
186    *                            More attenuation --> more taps
187    * \p window_type:            What kind of window to use. Determines
188    *                            maximum attenuation and passband ripple.
189    * \p beta:                   parameter for Kaiser window
190    */
191
192   static std::vector<float>
193   band_pass_2 (double gain,
194              double sampling_freq,
195              double low_cutoff_freq,            // Hz beginning transition band
196              double high_cutoff_freq,           // Hz beginning transition band
197              double transition_width,           // Hz width of transition band
198              double attenuation_dB,             // out of band attenuation dB
199              win_type window = WIN_HAMMING,
200              double beta = 6.76);               // used only with Kaiser
201
202   /*!
203    * \brief use "window method" to design a complex band-pass FIR filter
204    *
205    * \p gain:                   overall gain of filter (typically 1.0)
206    * \p sampling_freq:          sampling freq (Hz)
207    * \p low_cutoff_freq:        center of transition band (Hz)
208    * \p high_cutoff_freq:       center of transition band (Hz)
209    * \p transition_width:       width of transition band (Hz).
210    *                            The normalized width of the transition
211    *                            band is what sets the number of taps
212    *                            required.  Narrow --> more taps
213    * \p window_type:            What kind of window to use. Determines
214    *                            maximum attenuation and passband ripple.
215    * \p beta:                   parameter for Kaiser window
216    */
217
218   static std::vector<gr_complex>
219   complex_band_pass (double gain,
220              double sampling_freq,
221              double low_cutoff_freq,            // Hz center of transition band
222              double high_cutoff_freq,           // Hz center of transition band
223              double transition_width,           // Hz width of transition band
224              win_type window = WIN_HAMMING,
225              double beta = 6.76);               // used only with Kaiser
226
227   /*!
228    * \brief use "window method" to design a complex band-pass FIR filter
229    *
230    * \p gain:                   overall gain of filter (typically 1.0)
231    * \p sampling_freq:          sampling freq (Hz)
232    * \p low_cutoff_freq:        center of transition band (Hz)
233    * \p high_cutoff_freq:       center of transition band (Hz)
234    * \p transition_width:       width of transition band (Hz).
235    * \p attenuation_dB          out of band attenuation
236    *                            The normalized width of the transition
237    *                            band and the required stop band
238    *                            attenuation is what sets the number of taps
239    *                            required.  Narrow --> more taps
240    *                            More attenuation --> more taps
241    * \p window_type:            What kind of window to use. Determines
242    *                            maximum attenuation and passband ripple.
243    * \p beta:                   parameter for Kaiser window
244    */
245
246   static std::vector<gr_complex>
247   complex_band_pass_2 (double gain,
248              double sampling_freq,
249              double low_cutoff_freq,            // Hz beginning transition band
250              double high_cutoff_freq,           // Hz beginning transition band
251              double transition_width,           // Hz width of transition band
252              double attenuation_dB,             // out of band attenuation dB
253              win_type window = WIN_HAMMING,
254              double beta = 6.76);               // used only with Kaiser
255
256   /*!
257    * \brief use "window method" to design a band-reject FIR filter
258    *
259    * \p gain:                   overall gain of filter (typically 1.0)
260    * \p sampling_freq:          sampling freq (Hz)
261    * \p low_cutoff_freq:        center of transition band (Hz)
262    * \p high_cutoff_freq:       center of transition band (Hz)
263    * \p transition_width:       width of transition band (Hz).
264    *                            The normalized width of the transition
265    *                            band is what sets the number of taps
266    *                            required.  Narrow --> more taps
267    * \p window_type:            What kind of window to use. Determines
268    *                            maximum attenuation and passband ripple.
269    * \p beta:                   parameter for Kaiser window
270    */
271
272   static std::vector<float>
273   band_reject (double gain,
274                double sampling_freq,
275                double low_cutoff_freq,          // Hz center of transition band
276                double high_cutoff_freq,         // Hz center of transition band
277                double transition_width,         // Hz width of transition band
278                win_type window = WIN_HAMMING,
279                double beta = 6.76);             // used only with Kaiser
280
281   /*!
282    * \brief use "window method" to design a band-reject FIR filter
283    *
284    * \p gain:                   overall gain of filter (typically 1.0)
285    * \p sampling_freq:          sampling freq (Hz)
286    * \p low_cutoff_freq:        center of transition band (Hz)
287    * \p high_cutoff_freq:       center of transition band (Hz)
288    * \p transition_width:       width of transition band (Hz).
289    * \p attenuation_dB          out of band attenuation
290    *                            The normalized width of the transition
291    *                            band and the required stop band
292    *                            attenuation is what sets the number of taps
293    *                            required.  Narrow --> more taps
294    *                            More attenuation --> more taps
295    * \p window_type:            What kind of window to use. Determines
296    *                            maximum attenuation and passband ripple.
297    * \p beta:                   parameter for Kaiser window
298    */
299
300   static std::vector<float>
301   band_reject_2 (double gain,
302                double sampling_freq,
303                double low_cutoff_freq,          // Hz beginning transition band
304                double high_cutoff_freq,         // Hz beginning transition band
305                double transition_width,         // Hz width of transition band
306                double attenuation_dB,           // out of band attenuation dB
307                win_type window = WIN_HAMMING,
308                double beta = 6.76);             // used only with Kaiser
309
310   /*!\brief design a Hilbert Transform Filter
311    *
312    * \p ntaps:                  Number of taps, must be odd
313    * \p window_type:            What kind of window to use
314    * \p beta:                   Only used for Kaiser
315    */
316   static std::vector<float>
317   hilbert (unsigned int ntaps = 19,
318            win_type windowtype = WIN_RECTANGULAR,
319            double beta = 6.76);
320    
321   /*!
322    * \brief design a Root Cosine FIR Filter (do we need a window?)
323    *
324    * \p gain:                   overall gain of filter (typically 1.0)
325    * \p sampling_freq:          sampling freq (Hz)
326    * \p symbol rate:            symbol rate, must be a factor of sample rate
327    * \p alpha:                  excess bandwidth factor
328    * \p ntaps:                  number of taps
329    */
330   static std::vector<float>
331   root_raised_cosine (double gain,
332                       double sampling_freq,
333                       double symbol_rate,       // Symbol rate, NOT bitrate (unless BPSK)
334                       double alpha,             // Excess Bandwidth Factor
335                       int ntaps);
336
337   /*!
338    * \brief design a Gaussian filter
339    *
340    * \p gain:                   overall gain of filter (typically 1.0)
341    * \p symbols per bit:        symbol rate, must be a factor of sample rate
342    * \p ntaps:                  number of taps
343    */
344   static std::vector<float>
345   gaussian (double gain,
346             double spb,       
347             double bt,              // Bandwidth to bitrate ratio
348             int ntaps);
349
350   // window functions ...
351   static std::vector<float> window (win_type type, int ntaps, double beta);
352
353 private:
354   static double bessi0(double x);
355   static void sanity_check_1f (double sampling_freq, double f1,
356                                double transition_width);
357   static void sanity_check_2f (double sampling_freq, double f1, double f2,
358                                double transition_width);
359   static void sanity_check_2f_c (double sampling_freq, double f1, double f2,
360                                double transition_width);
361
362   static int compute_ntaps (double sampling_freq,
363                             double transition_width,
364                             win_type window_type, double beta);
365
366   static int compute_ntaps_windes (double sampling_freq,
367                                    double transition_width,
368                                    double attenuation_dB);
369 };
370
371 #endif