summaryrefslogtreecommitdiff
path: root/gnuradio-core/src/lib/general/gr_firdes.h
blob: 8d98ebe0a1f1a0e5d51734857b4bd6ae4cd3f45b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* -*- c++ -*- */
/*
 * Copyright 2002,2008 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifndef _GR_FIRDES_H_
#define _GR_FIRDES_H_

#include <gr_core_api.h>
#include <vector>
#include <cmath>
#include <gr_complex.h>

/*!
 * \brief Finite Impulse Response (FIR) filter design functions.
 * \ingroup filter_design
 */

class GR_CORE_API gr_firdes {
 public:

  enum win_type {
    WIN_HAMMING = 0,	// max attenuation 53 dB
    WIN_HANN = 1,	// max attenuation 44 dB
    WIN_BLACKMAN = 2,	// max attenuation 74 dB
    WIN_RECTANGULAR = 3,
    WIN_KAISER = 4,     // max attenuation a function of beta, google it
    WIN_BLACKMAN_hARRIS = 5,
    WIN_BLACKMAN_HARRIS = 5, // alias for capitalization consistency
  };


  // ... class methods ...

  /*!
   * \brief use "window method" to design a low-pass FIR filter
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p sampling_freq:		sampling freq (Hz)
   * \p cutoff_freq:		center of transition band (Hz)
   * \p transition_width:	width of transition band (Hz).
   *				The normalized width of the transition
   *				band is what sets the number of taps
   *				required.  Narrow --> more taps
   * \p window_type: 		What kind of window to use. Determines
   *				maximum attenuation and passband ripple.
   * \p beta:			parameter for Kaiser window
   */
  static std::vector<float>
  low_pass (double gain,
	    double sampling_freq,
	    double cutoff_freq,		// Hz center of transition band
	    double transition_width,	// Hz width of transition band
	    win_type window = WIN_HAMMING,
	    double beta = 6.76);		// used only with Kaiser

  /*!
   * \brief use "window method" to design a low-pass FIR filter
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p sampling_freq:		sampling freq (Hz)
   * \p cutoff_freq:		center of transition band (Hz)
   * \p transition_width:	width of transition band (Hz).
   * \p attenuation_dB          required stopband attenuation
   *				The normalized width of the transition
   *				band and the required stop band
   *                            attenuation is what sets the number of taps
   *				required.  Narrow --> more taps
   *                            More attenuatin --> more taps
   * \p window_type: 		What kind of window to use. Determines
   *				maximum attenuation and passband ripple.
   * \p beta:			parameter for Kaiser window
   */

  static std::vector<float>
  low_pass_2 (double gain,
	    double sampling_freq,
	    double cutoff_freq,		// Hz beginning transition band
	    double transition_width,	// Hz width of transition band
	    double attenuation_dB,      // out of band attenuation dB
	    win_type window = WIN_HAMMING,
	    double beta = 6.76);		// used only with Kaiser

  /*!
   * \brief use "window method" to design a high-pass FIR filter
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p sampling_freq:		sampling freq (Hz)
   * \p cutoff_freq:		center of transition band (Hz)
   * \p transition_width:	width of transition band (Hz).
   *				The normalized width of the transition
   *				band is what sets the number of taps
   *				required.  Narrow --> more taps
   * \p window_type: 		What kind of window to use. Determines
   *				maximum attenuation and passband ripple.
   * \p beta:			parameter for Kaiser window
   */

  static std::vector<float>
  high_pass (double gain,
	     double sampling_freq,
	     double cutoff_freq,		// Hz center of transition band
	     double transition_width,		// Hz width of transition band
	     win_type window = WIN_HAMMING,
	     double beta = 6.76);		// used only with Kaiser

  /*!
   * \brief use "window method" to design a high-pass FIR filter
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p sampling_freq:		sampling freq (Hz)
   * \p cutoff_freq:		center of transition band (Hz)
   * \p transition_width:	width of transition band (Hz).
   * \p attenuation_dB          out of band attenuation
   *				The normalized width of the transition
   *				band and the required stop band
   *                            attenuation is what sets the number of taps
   *				required.  Narrow --> more taps
   *                            More attenuation --> more taps
   * \p window_type: 		What kind of window to use. Determines
   *				maximum attenuation and passband ripple.
   * \p beta:			parameter for Kaiser window
   */

  static std::vector<float>
  high_pass_2 (double gain,
	     double sampling_freq,
	     double cutoff_freq,		// Hz center of transition band
	     double transition_width,		// Hz width of transition band
	     double attenuation_dB,             // out of band attenuation dB
	     win_type window = WIN_HAMMING,
	     double beta = 6.76);		// used only with Kaiser

  /*!
   * \brief use "window method" to design a band-pass FIR filter
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p sampling_freq:		sampling freq (Hz)
   * \p low_cutoff_freq:	center of transition band (Hz)
   * \p high_cutoff_freq:	center of transition band (Hz)
   * \p transition_width:	width of transition band (Hz).
   *				The normalized width of the transition
   *				band is what sets the number of taps
   *				required.  Narrow --> more taps
   * \p window_type: 		What kind of window to use. Determines
   *				maximum attenuation and passband ripple.
   * \p beta:			parameter for Kaiser window
   */
  static std::vector<float>
  band_pass (double gain,
	     double sampling_freq,
	     double low_cutoff_freq,		// Hz center of transition band
	     double high_cutoff_freq,		// Hz center of transition band
	     double transition_width,		// Hz width of transition band
	     win_type window = WIN_HAMMING,
	     double beta = 6.76);		// used only with Kaiser

  /*!
   * \brief use "window method" to design a band-pass FIR filter
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p sampling_freq:		sampling freq (Hz)
   * \p low_cutoff_freq:	center of transition band (Hz)
   * \p high_cutoff_freq:	center of transition band (Hz)
   * \p transition_width:	width of transition band (Hz).
   * \p attenuation_dB          out of band attenuation
   *				The normalized width of the transition
   *				band and the required stop band
   *                            attenuation is what sets the number of taps
   *				required.  Narrow --> more taps
   *                            More attenuation --> more taps
   * \p window_type: 		What kind of window to use. Determines
   *				maximum attenuation and passband ripple.
   * \p beta:			parameter for Kaiser window
   */

  static std::vector<float>
  band_pass_2 (double gain,
	     double sampling_freq,
	     double low_cutoff_freq,		// Hz beginning transition band
	     double high_cutoff_freq,		// Hz beginning transition band
	     double transition_width,		// Hz width of transition band
	     double attenuation_dB,             // out of band attenuation dB
	     win_type window = WIN_HAMMING,
	     double beta = 6.76);		// used only with Kaiser

  /*!
   * \brief use "window method" to design a complex band-pass FIR filter
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p sampling_freq:		sampling freq (Hz)
   * \p low_cutoff_freq:	center of transition band (Hz)
   * \p high_cutoff_freq:	center of transition band (Hz)
   * \p transition_width:	width of transition band (Hz).
   *				The normalized width of the transition
   *				band is what sets the number of taps
   *				required.  Narrow --> more taps
   * \p window_type: 		What kind of window to use. Determines
   *				maximum attenuation and passband ripple.
   * \p beta:			parameter for Kaiser window
   */

  static std::vector<gr_complex>
  complex_band_pass (double gain,
	     double sampling_freq,
	     double low_cutoff_freq,		// Hz center of transition band
	     double high_cutoff_freq,		// Hz center of transition band
	     double transition_width,		// Hz width of transition band
	     win_type window = WIN_HAMMING,
	     double beta = 6.76);		// used only with Kaiser

  /*!
   * \brief use "window method" to design a complex band-pass FIR filter
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p sampling_freq:		sampling freq (Hz)
   * \p low_cutoff_freq:	center of transition band (Hz)
   * \p high_cutoff_freq:	center of transition band (Hz)
   * \p transition_width:	width of transition band (Hz).
   * \p attenuation_dB          out of band attenuation
   *				The normalized width of the transition
   *				band and the required stop band
   *                            attenuation is what sets the number of taps
   *				required.  Narrow --> more taps
   *                            More attenuation --> more taps
   * \p window_type: 		What kind of window to use. Determines
   *				maximum attenuation and passband ripple.
   * \p beta:			parameter for Kaiser window
   */

  static std::vector<gr_complex>
  complex_band_pass_2 (double gain,
	     double sampling_freq,
	     double low_cutoff_freq,		// Hz beginning transition band
	     double high_cutoff_freq,		// Hz beginning transition band
	     double transition_width,		// Hz width of transition band
	     double attenuation_dB,             // out of band attenuation dB
	     win_type window = WIN_HAMMING,
	     double beta = 6.76);		// used only with Kaiser

  /*!
   * \brief use "window method" to design a band-reject FIR filter
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p sampling_freq:		sampling freq (Hz)
   * \p low_cutoff_freq:	center of transition band (Hz)
   * \p high_cutoff_freq:	center of transition band (Hz)
   * \p transition_width:	width of transition band (Hz).
   *				The normalized width of the transition
   *				band is what sets the number of taps
   *				required.  Narrow --> more taps
   * \p window_type: 		What kind of window to use. Determines
   *				maximum attenuation and passband ripple.
   * \p beta:			parameter for Kaiser window
   */

  static std::vector<float>
  band_reject (double gain,
	       double sampling_freq,
	       double low_cutoff_freq,		// Hz center of transition band
	       double high_cutoff_freq,		// Hz center of transition band
	       double transition_width,		// Hz width of transition band
	       win_type window = WIN_HAMMING,
	       double beta = 6.76);		// used only with Kaiser

  /*!
   * \brief use "window method" to design a band-reject FIR filter
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p sampling_freq:		sampling freq (Hz)
   * \p low_cutoff_freq:	center of transition band (Hz)
   * \p high_cutoff_freq:	center of transition band (Hz)
   * \p transition_width:	width of transition band (Hz).
   * \p attenuation_dB          out of band attenuation
   *				The normalized width of the transition
   *				band and the required stop band
   *                            attenuation is what sets the number of taps
   *				required.  Narrow --> more taps
   *                            More attenuation --> more taps
   * \p window_type: 		What kind of window to use. Determines
   *				maximum attenuation and passband ripple.
   * \p beta:			parameter for Kaiser window
   */

  static std::vector<float>
  band_reject_2 (double gain,
	       double sampling_freq,
	       double low_cutoff_freq,		// Hz beginning transition band
	       double high_cutoff_freq,		// Hz beginning transition band
	       double transition_width,		// Hz width of transition band
	       double attenuation_dB,           // out of band attenuation dB
	       win_type window = WIN_HAMMING,
	       double beta = 6.76);		// used only with Kaiser

  /*!\brief design a Hilbert Transform Filter
   *
   * \p ntaps:                  Number of taps, must be odd
   * \p window_type:            What kind of window to use
   * \p beta:                   Only used for Kaiser
   */
  static std::vector<float>
  hilbert (unsigned int ntaps = 19,
	   win_type windowtype = WIN_RECTANGULAR,
	   double beta = 6.76);

  /*!
   * \brief design a Root Cosine FIR Filter (do we need a window?)
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p sampling_freq:		sampling freq (Hz)
   * \p symbol rate:		symbol rate, must be a factor of sample rate
   * \p alpha:		        excess bandwidth factor
   * \p ntaps:		        number of taps
   */
  static std::vector<float>
  root_raised_cosine (double gain,
		      double sampling_freq,
		      double symbol_rate,       // Symbol rate, NOT bitrate (unless BPSK)
		      double alpha,             // Excess Bandwidth Factor
		      int ntaps);

  /*!
   * \brief design a Gaussian filter
   *
   * \p gain:			overall gain of filter (typically 1.0)
   * \p symbols per bit:	symbol rate, must be a factor of sample rate
   * \p ntaps:		        number of taps
   */
  static std::vector<float>
  gaussian (double gain,
	    double spb,
	    double bt,              // Bandwidth to bitrate ratio
	    int ntaps);

  // window functions ...
  static std::vector<float> window (win_type type, int ntaps, double beta);

private:
  static double bessi0(double x);
  static void sanity_check_1f (double sampling_freq, double f1,
			       double transition_width);
  static void sanity_check_2f (double sampling_freq, double f1, double f2,
			       double transition_width);
  static void sanity_check_2f_c (double sampling_freq, double f1, double f2,
			       double transition_width);

  static int compute_ntaps (double sampling_freq,
			    double transition_width,
			    win_type window_type, double beta);

  static int compute_ntaps_windes (double sampling_freq,
				   double transition_width,
				   double attenuation_dB);
};

#endif