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