GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
window.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2007,2008,2012,2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef INCLUDED_FFT_WINDOW_H
12 #define INCLUDED_FFT_WINDOW_H
13 
14 #include <gnuradio/fft/api.h>
15 #include <gnuradio/gr_complex.h>
16 #include <cmath>
17 #include <vector>
18 
19 namespace gr {
20 namespace fft {
21 
23 {
24 public:
25  // illegal value for any window that requires a parameter
26  static constexpr double INVALID_WIN_PARAM = -1;
27 
28  enum win_type {
29  WIN_NONE = -1, //!< don't use a window
30  WIN_HAMMING = 0, //!< Hamming window; max attenuation 53 dB
31  WIN_HANN = 1, //!< Hann window; max attenuation 44 dB
32  WIN_HANNING = 1, //!< alias to WIN_HANN
33  WIN_BLACKMAN = 2, //!< Blackman window; max attenuation 74 dB
34  WIN_RECTANGULAR = 3, //!< Basic rectangular window; max attenuation 21 dB
35  WIN_KAISER = 4, //!< Kaiser window; max attenuation see window::max_attenuation
36  WIN_BLACKMAN_hARRIS = 5, //!< Blackman-harris window; max attenuation 92 dB
37  WIN_BLACKMAN_HARRIS =
38  5, //!< alias to WIN_BLACKMAN_hARRIS for capitalization consistency
39  WIN_BARTLETT = 6, //!< Barlett (triangular) window; max attenuation 26 dB
40  WIN_FLATTOP = 7, //!< flat top window; useful in FFTs; max attenuation 93 dB
41  WIN_NUTTALL = 8, //!< Nuttall window; max attenuation 114 dB
42  WIN_BLACKMAN_NUTTALL = 8, //!< Nuttall window; max attenuation 114 dB
43  WIN_NUTTALL_CFD =
44  9, //!< Nuttall continuous-first-derivative window; max attenuation 112 dB
45  WIN_WELCH = 10, //!< Welch window; max attenuation 31 dB
46  WIN_PARZEN = 11, //!< Parzen window; max attenuation 56 dB
47  WIN_EXPONENTIAL =
48  12, //!< Exponential window; max attenuation see window::max_attenuation
49  WIN_RIEMANN = 13, //!< Riemann window; max attenuation 39 dB
50  WIN_GAUSSIAN =
51  14, //!< Gaussian window; max attenuation see window::max_attenuation
52  WIN_TUKEY = 15, //!< Tukey window; max attenuation see window::max_attenuation
53  };
54 
55  /*!
56  * \brief Given a window::win_type, this tells you the maximum
57  * attenuation (really the maximum approximation error) you can expect.
58  *
59  * \details
60  * For most windows, this is a set value. For the Kaiser, Exponential, Gaussian, and
61  * Tukey windows, the attenuation is based on the value of a provided parameter.
62  *
63  * For the Kaiser window the actual relationship is a piece-wise exponential
64  * relationship to calculate beta from the desired attenuation and can be found on
65  * page 542 of Oppenheim and Schafer (Discrete-Time Signal Processing, 3rd edition).
66  * To simplify this function to solve for A given beta, we use a linear form that is
67  * exact for attenuation >= 50 dB. For an attenuation of 50 dB, beta = 4.55; for an
68  * attenuation of 70 dB, beta = 6.76.
69  *
70  * Exponential attenuation is complicated to measure due to the irregular error ripple
71  * structure, but it ranges between 23 and 26 dB depending on the decay factor; 26 dB
72  * is a good bound.
73  *
74  * The Gaussian window should not be used for window based filter construction;
75  * instead there is a dedicated gaussian filter construction function. There is no
76  * meaningful way to measure approximation error 'delta' as shown in Fig 7.23 of
77  * Oppenheim and Schafer (Discrete-Time Signal Processing, 3rd edition).
78  *
79  * Tukey windows provide attenuation that varies non-linearily between Rectangular (21
80  * dB) and Hann (44 dB) windows.
81  *
82  * \param type The window::win_type enumeration of the window type.
83  * \param param Parameter value used for Kaiser (beta), Exponential (d), Gaussian
84  * (sigma) and Tukey (alpha) window creation.
85  */
86  static double max_attenuation(win_type type, double param = INVALID_WIN_PARAM);
87 
88  /*!
89  * \brief Helper function to build cosine-based windows. 3-coefficient version.
90  */
91  static std::vector<float> coswindow(int ntaps, float c0, float c1, float c2);
92 
93  /*!
94  * \brief Helper function to build cosine-based windows. 4-coefficient version.
95  */
96  static std::vector<float>
97  coswindow(int ntaps, float c0, float c1, float c2, float c3);
98 
99  /*!
100  * \brief Helper function to build cosine-based windows. 5-coefficient version.
101  */
102  static std::vector<float>
103  coswindow(int ntaps, float c0, float c1, float c2, float c3, float c4);
104 
105  /*!
106  * \brief Build a rectangular window.
107  *
108  * Taps are flat across the window.
109  *
110  * \param ntaps Number of coefficients in the window.
111  */
112  static std::vector<float> rectangular(int ntaps);
113 
114  /*!
115  * \brief Build a Hamming window.
116  *
117  * See:
118  * <pre>
119  * A. V. Oppenheim and R. W. Schafer, "Discrete-Time
120  * Signal Processing," Upper Saddle River, N.J.: Prentice
121  * Hall, 2010, pp. 535-538.
122  * </pre>
123  *
124  * \param ntaps Number of coefficients in the window.
125  */
126  static std::vector<float> hamming(int ntaps);
127 
128  /*!
129  * \brief Build a Hann window (sometimes known as Hanning).
130  *
131  * See:
132  * <pre>
133  * A. V. Oppenheim and R. W. Schafer, "Discrete-Time
134  * Signal Processing," Upper Saddle River, N.J.: Prentice
135  * Hall, 2010, pp. 535-538.
136  * </pre>
137  *
138  * \param ntaps Number of coefficients in the window.
139  */
140  static std::vector<float> hann(int ntaps);
141 
142  /*!
143  * \brief Alias to build a Hann window.
144  *
145  * \param ntaps Number of coefficients in the window.
146  */
147  static std::vector<float> hanning(int ntaps);
148 
149  /*!
150  * \brief Build an exact Blackman window.
151  *
152  * See:
153  * <pre>
154  * A. V. Oppenheim and R. W. Schafer, "Discrete-Time
155  * Signal Processing," Upper Saddle River, N.J.: Prentice
156  * Hall, 2010, pp. 535-538.
157  * </pre>
158  *
159  * \param ntaps Number of coefficients in the window.
160  */
161  static std::vector<float> blackman(int ntaps);
162 
163  /*!
164  * \brief Build Blackman window, variation 1.
165  */
166  static std::vector<float> blackman2(int ntaps);
167 
168  /*!
169  * \brief Build Blackman window, variation 2.
170  */
171  static std::vector<float> blackman3(int ntaps);
172 
173  /*!
174  * \brief Build Blackman window, variation 3.
175  */
176  static std::vector<float> blackman4(int ntaps);
177 
178  /*!
179  * \brief Build a Blackman-harris window with a given attenuation.
180  *
181  * <pre>
182  * f. j. harris, "On the use of windows for harmonic analysis
183  * with the discrete Fourier transforms," Proc. IEEE, Vol. 66,
184  * ppg. 51-83, Jan. 1978.
185  * </pre>
186  *
187  * \param ntaps Number of coefficients in the window.
188 
189  * \param atten Attenuation factor. Must be [61, 67, 74, 92].
190  * See the above paper for details.
191  */
192  static std::vector<float> blackman_harris(int ntaps, int atten = 92);
193 
194  /*!
195  * Alias to gr::fft::window::blackman_harris.
196  */
197  static std::vector<float> blackmanharris(int ntaps, int atten = 92);
198 
199  /*!
200  * \brief Build a minimum 4-term Nuttall (or Blackman-Nuttall) window, referred to by
201  * Heinzel G. et al. as a Nuttall4c window.
202  *
203  * See: A.H. Nuttall: 'Some windows with very good sidelobe behaviour', IEEE Trans. on
204  * Acoustics, Speech and Signal Processing, Vol ASSP-29, figure 15
205  *
206  * See: 'Spectrum and spectral density estimation by the Discrete Fourier transform
207  * (DFT), including a comprehensive list of window functions and some new flat-top
208  * windows', February 15, 2002 https://holometer.fnal.gov/GH_FFT.pdf
209  *
210  * Also: http://en.wikipedia.org/wiki/Window_function#Blackman.E2.80.93Nuttall_window
211  *
212  * \param ntaps Number of coefficients in the window.
213  */
214  static std::vector<float> nuttall(int ntaps);
215 
216  /*!
217  * \brief Alias to the Nuttall window.
218  *
219  * \param ntaps Number of coefficients in the window.
220  */
221  static std::vector<float> blackman_nuttall(int ntaps);
222 
223  /*!
224  * \brief Build a Nuttall 4-term continuous first derivative window, referred to by
225  * Heinzel G. et al. as a Nuttall4b window
226  *
227  * See: A.H. Nuttall: 'Some windows with very good sidelobe behaviour', IEEE Trans. on
228  * Acoustics, Speech and Signal Processing, Vol ASSP-29, figure 12
229  *
230  * See: 'Spectrum and spectral density estimation by the Discrete Fourier transform
231  * (DFT), including a comprehensive list of window functions and some new flat-top
232  * windows', February 15, 2002 https://holometer.fnal.gov/GH_FFT.pdf
233  *
234  * Also:
235  * http://en.wikipedia.org/wiki/Window_function#Nuttall_window.2C_continuous_first_derivative
236  *
237  * \param ntaps Number of coefficients in the window.
238  */
239  static std::vector<float> nuttall_cfd(int ntaps);
240 
241  /*!
242  * \brief Build a flat top window per the SRS specification
243  *
244  * See:
245  * <pre>
246  * Stanford Research Systems, "Model SR785 Dynamic Signal
247  * Analyzer: Operating Manual and Programming Reference,"
248  * 2017, pp 2-13
249  * </pre>
250  *
251  * Note: there are many flat top windows, and this implementation is different from
252  * SciPY and Matlab which use the coefficients from D’Antona et al. "Digital Signal
253  * Processing for Measurement Systems" with the following cosine coefficients: <pre>
254  * [0.21557895, 0.41663158, 0.277263158, 0.083578947, 0.006947368]
255  * </pre>
256  *
257  * \param ntaps Number of coefficients in the window.
258  */
259  static std::vector<float> flattop(int ntaps);
260 
261  /*!
262  * \brief Build a Kaiser window with a given beta.
263  *
264  * See:
265  * <pre>
266  * A. V. Oppenheim and R. W. Schafer, "Discrete-Time
267  * Signal Processing," Upper Saddle River, N.J.: Prentice
268  * Hall, 2010, pp. 541-545.
269  * </pre>
270  *
271  * \param ntaps Number of coefficients in the window.
272  * \param beta Shaping parameter of the window. See the
273  * discussion in Oppenheim and Schafer.
274  */
275  static std::vector<float> kaiser(int ntaps, double beta);
276 
277  /*!
278  * \brief Build a Barlett (triangular) window.
279  *
280  * See:
281  * <pre>
282  * A. V. Oppenheim and R. W. Schafer, "Discrete-Time
283  * Signal Processing," Upper Saddle River, N.J.: Prentice
284  * Hall, 2010, pp. 535-538.
285  * </pre>
286  *
287  * \param ntaps Number of coefficients in the window.
288  */
289  static std::vector<float> bartlett(int ntaps);
290 
291  static std::vector<float> welch(int ntaps);
292 
293  /*!
294  * \brief Build a Parzen (or de la Valle-Poussin) window.
295  *
296  * See:
297  * <pre>
298  * A. D. Poularikas, "Handbook of Formulas and Tables for
299  * Signal Processing," Springer, Oct 28, 1998
300  * </pre>
301  *
302  * \param ntaps Number of coefficients in the window.
303  */
304  static std::vector<float> parzen(int ntaps);
305 
306  /*!
307  * \brief Build an exponential window with a given decay.
308  *
309  * See: http://en.wikipedia.org/wiki/Window_function#Exponential_or_Poisson_window
310  *
311  * \param ntaps Number of coefficients in the window.
312  * \param d Decay of \p d dB over half the window length.
313  */
314  static std::vector<float> exponential(int ntaps, double d);
315 
316  /*!
317  * \brief Build a Riemann window.
318  *
319  * See:
320  * <pre>
321  * A. D. Poularikas, "Handbook of Formulas and Tables for
322  * Signal Processing," Springer, Oct 28, 1998
323  * </pre>
324  *
325  * \param ntaps Number of coefficients in the window.
326  */
327  static std::vector<float> riemann(int ntaps);
328 
329  /*!
330  * \brief Build a Tukey window.
331  * <pre>
332  * Bloomfield, P. Fourier Analysis of Time Series: An Introduction. New York:
333  * Wiley-Interscience, 2000, pp 69 (eqn 6.9)
334  * </pre>
335  *
336  * \param ntaps Number of coefficients in the window.
337  * \param alpha Shaping parameter for the Tukey window, an
338  * alpha of zero is equivalent to a rectangular
339  * window, an alpha of 1 is equivalent to Hann.
340  */
341  static std::vector<float> tukey(int ntaps, float alpha);
342 
343  /*!
344  * \brief Build a Gaussian window using the equation
345  * <pre>
346  * exp(-(1/2) * (n/sigma)^2)
347  * </pre>
348  *
349  * \param ntaps Number of coefficients in the window.
350  * \param sigma Standard deviation of gaussian distribution.
351  */
352  static std::vector<float> gaussian(int ntaps, float sigma);
353 
354  /*!
355  * \brief Build a window using gr::fft::win_type to index the
356  * type of window desired.
357  *
358  * \param type a gr::fft::win_type index for the type of window.
359  * \param ntaps Number of coefficients in the window.
360  * \param param Parameter value used for Kaiser (beta), Exponential (d), Gaussian
361  * (sigma) and Tukey (alpha) window creation. \param normalize If true, return a
362  * window with unit power
363  */
364  static std::vector<float> build(win_type type,
365  int ntaps,
366  double param = INVALID_WIN_PARAM,
367  const bool normalize = false);
368 };
369 
370 } /* namespace fft */
371 } /* namespace gr */
372 
373 #endif /* INCLUDED_FFT_WINDOW_H */
Definition: window.h:23
static std::vector< float > bartlett(int ntaps)
Build a Barlett (triangular) window.
static std::vector< float > coswindow(int ntaps, float c0, float c1, float c2, float c3, float c4)
Helper function to build cosine-based windows. 5-coefficient version.
static std::vector< float > hamming(int ntaps)
Build a Hamming window.
static std::vector< float > blackman2(int ntaps)
Build Blackman window, variation 1.
static std::vector< float > parzen(int ntaps)
Build a Parzen (or de la Valle-Poussin) window.
static std::vector< float > blackman3(int ntaps)
Build Blackman window, variation 2.
win_type
Definition: window.h:28
static std::vector< float > coswindow(int ntaps, float c0, float c1, float c2)
Helper function to build cosine-based windows. 3-coefficient version.
static std::vector< float > gaussian(int ntaps, float sigma)
Build a Gaussian window using the equation.
static std::vector< float > blackman_nuttall(int ntaps)
Alias to the Nuttall window.
static std::vector< float > blackman(int ntaps)
Build an exact Blackman window.
static std::vector< float > nuttall_cfd(int ntaps)
Build a Nuttall 4-term continuous first derivative window, referred to by Heinzel G....
static std::vector< float > nuttall(int ntaps)
Build a minimum 4-term Nuttall (or Blackman-Nuttall) window, referred to by Heinzel G....
static std::vector< float > blackman_harris(int ntaps, int atten=92)
Build a Blackman-harris window with a given attenuation.
static std::vector< float > build(win_type type, int ntaps, double param=INVALID_WIN_PARAM, const bool normalize=false)
Build a window using gr::fft::win_type to index the type of window desired.
static std::vector< float > riemann(int ntaps)
Build a Riemann window.
static std::vector< float > tukey(int ntaps, float alpha)
Build a Tukey window.
static std::vector< float > blackmanharris(int ntaps, int atten=92)
static std::vector< float > rectangular(int ntaps)
Build a rectangular window.
static double max_attenuation(win_type type, double param=INVALID_WIN_PARAM)
Given a window::win_type, this tells you the maximum attenuation (really the maximum approximation er...
static std::vector< float > welch(int ntaps)
static std::vector< float > blackman4(int ntaps)
Build Blackman window, variation 3.
static std::vector< float > hanning(int ntaps)
Alias to build a Hann window.
static std::vector< float > coswindow(int ntaps, float c0, float c1, float c2, float c3)
Helper function to build cosine-based windows. 4-coefficient version.
static std::vector< float > exponential(int ntaps, double d)
Build an exponential window with a given decay.
static std::vector< float > hann(int ntaps)
Build a Hann window (sometimes known as Hanning).
static std::vector< float > flattop(int ntaps)
Build a flat top window per the SRS specification.
static std::vector< float > kaiser(int ntaps, double beta)
Build a Kaiser window with a given beta.
#define FFT_API
Definition: gr-fft/include/gnuradio/fft/api.h:18
GNU Radio logging wrapper.
Definition: basic_block.h:29