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