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