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