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