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