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