GNU Radio Manual and C++ API Reference  3.9.0.0
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  enum win_type {
26  WIN_NONE = -1, //!< don't use a window
27  WIN_HAMMING = 0, //!< Hamming window; max attenuation 53 dB
28  WIN_HANN = 1, //!< Hann window; max attenuation 44 dB
29  WIN_BLACKMAN = 2, //!< Blackman window; max attenuation 74 dB
30  WIN_RECTANGULAR = 3, //!< Basic rectangular window; max attenuation 21 dB
31  WIN_KAISER = 4, //!< Kaiser window; max attenuation see window::max_attenuation
32  WIN_BLACKMAN_hARRIS = 5, //!< Blackman-harris window; max attenuation 92 dB
33  WIN_BLACKMAN_HARRIS =
34  5, //!< alias to WIN_BLACKMAN_hARRIS for capitalization consistency
35  WIN_BARTLETT = 6, //!< Barlett (triangular) window; max attenuation 26 dB
36  WIN_FLATTOP = 7, //!< flat top window; useful in FFTs; max attenuation 93 dB
37  };
38 
39  /*!
40  * \brief Given a window::win_type, this tells you the maximum
41  * attenuation you can expect.
42  *
43  * \details
44  * For most windows, this is a set value. For the Kaiser window,
45  * the attenuation is based on the value of beta. The actual
46  * relationship is a piece-wise exponential relationship to
47  * calculate beta from the desired attenuation and can be found
48  * on page 542 of Oppenheim and Schafer (Discrete-Time Signal
49  * Processing, 3rd edition). To simplify this function to solve
50  * for A given beta, we use a linear form that is exact for
51  * attenuation >= 50 dB.
52  *
53  * For an attenuation of 50 dB, beta = 4.55.
54  *
55  * For an attenuation of 70 dB, beta = 6.76.
56  *
57  * \param type The window::win_type enumeration of the window type.
58  * \param beta Beta value only used for the Kaiser window.
59  */
60  static double max_attenuation(win_type type, double beta = 6.76);
61 
62  /*!
63  * \brief Helper function to build cosine-based windows. 3-coefficient version.
64  */
65  static std::vector<float> coswindow(int ntaps, float c0, float c1, float c2);
66 
67  /*!
68  * \brief Helper function to build cosine-based windows. 4-coefficient version.
69  */
70  static std::vector<float>
71  coswindow(int ntaps, float c0, float c1, float c2, float c3);
72 
73  /*!
74  * \brief Helper function to build cosine-based windows. 5-coefficient version.
75  */
76  static std::vector<float>
77  coswindow(int ntaps, float c0, float c1, float c2, float c3, float c4);
78 
79  /*!
80  * \brief Build a rectangular window.
81  *
82  * Taps are flat across the window.
83  *
84  * \param ntaps Number of coefficients in the window.
85  */
86  static std::vector<float> rectangular(int ntaps);
87 
88  /*!
89  * \brief Build a Hamming window.
90  *
91  * See:
92  * <pre>
93  * A. V. Oppenheim and R. W. Schafer, "Discrete-Time
94  * Signal Processing," Upper Saddle River, N.J.: Prentice
95  * Hall, 2010, pp. 535-538.
96  * </pre>
97  *
98  * \param ntaps Number of coefficients in the window.
99  */
100  static std::vector<float> hamming(int ntaps);
101 
102  /*!
103  * \brief Build a Hann window (sometimes known as Hanning).
104  *
105  * See:
106  * <pre>
107  * A. V. Oppenheim and R. W. Schafer, "Discrete-Time
108  * Signal Processing," Upper Saddle River, N.J.: Prentice
109  * Hall, 2010, pp. 535-538.
110  * </pre>
111  *
112  * \param ntaps Number of coefficients in the window.
113  */
114  static std::vector<float> hann(int ntaps);
115 
116  /*!
117  * \brief Alias to build a Hann window.
118  *
119  * \param ntaps Number of coefficients in the window.
120  */
121  static std::vector<float> hanning(int ntaps);
122 
123  /*!
124  * \brief Build an exact Blackman window.
125  *
126  * See:
127  * <pre>
128  * A. V. Oppenheim and R. W. Schafer, "Discrete-Time
129  * Signal Processing," Upper Saddle River, N.J.: Prentice
130  * Hall, 2010, pp. 535-538.
131  * </pre>
132  *
133  * \param ntaps Number of coefficients in the window.
134  */
135  static std::vector<float> blackman(int ntaps);
136 
137  /*!
138  * \brief Build Blackman window, variation 1.
139  */
140  static std::vector<float> blackman2(int ntaps);
141 
142  /*!
143  * \brief Build Blackman window, variation 2.
144  */
145  static std::vector<float> blackman3(int ntaps);
146 
147  /*!
148  * \brief Build Blackman window, variation 3.
149  */
150  static std::vector<float> blackman4(int ntaps);
151 
152  /*!
153  * \brief Build a Blackman-harris window with a given attenuation.
154  *
155  * <pre>
156  * f. j. harris, "On the use of windows for harmonic analysis
157  * with the discrete Fourier transforms," Proc. IEEE, Vol. 66,
158  * ppg. 51-83, Jan. 1978.
159  * </pre>
160  *
161  * \param ntaps Number of coefficients in the window.
162 
163  * \param atten Attenuation factor. Must be [61, 67, 74, 92].
164  * See the above paper for details.
165  */
166  static std::vector<float> blackman_harris(int ntaps, int atten = 92);
167 
168  /*!
169  * Alias to gr::fft::window::blackman_harris.
170  */
171  static std::vector<float> blackmanharris(int ntaps, int atten = 92);
172 
173  /*!
174  * \brief Build a Nuttall (or Blackman-Nuttall) window.
175  *
176  * See: http://en.wikipedia.org/wiki/Window_function#Blackman.E2.80.93Nuttall_window
177  *
178  * \param ntaps Number of coefficients in the window.
179  */
180  static std::vector<float> nuttall(int ntaps);
181 
182  /*!
183  * Deprecated: use nuttall window instead.
184  */
185  static std::vector<float> nuttal(int ntaps);
186 
187  /*!
188  * \brief Alias to the Nuttall window.
189  *
190  * \param ntaps Number of coefficients in the window.
191  */
192  static std::vector<float> blackman_nuttall(int ntaps);
193 
194  /*!
195  * Deprecated: use blackman_nuttall window instead.
196  */
197  static std::vector<float> blackman_nuttal(int ntaps);
198 
199  /*!
200  * \brief Build a Nuttall continuous first derivative window.
201  *
202  * See:
203  * http://en.wikipedia.org/wiki/Window_function#Nuttall_window.2C_continuous_first_derivative
204  *
205  * \param ntaps Number of coefficients in the window.
206  */
207  static std::vector<float> nuttall_cfd(int ntaps);
208 
209  /*!
210  * Deprecated: use nuttall_cfd window instead.
211  */
212  static std::vector<float> nuttal_cfd(int ntaps);
213 
214  /*!
215  * \brief Build a flat top window per the SRS specification
216  *
217  * See:
218  * <pre>
219  * Stanford Research Systems, "Model SR785 Dynamic Signal
220  * Analyzer: Operating Manual and Programming Reference,"
221  * 2017, pp 2-13
222  * </pre>
223  *
224  * Note: there are many flat top windows, and this implementation is different from
225  * SciPY and Matlab which use the coefficients from D’Antona et al. "Digital Signal
226  * Processing for Measurement Systems" with the following cosine coefficients: <pre>
227  * [0.21557895, 0.41663158, 0.277263158, 0.083578947, 0.006947368]
228  * </pre>
229  *
230  * \param ntaps Number of coefficients in the window.
231  */
232  static std::vector<float> flattop(int ntaps);
233 
234  /*!
235  * \brief Build a Kaiser window with a given beta.
236  *
237  * See:
238  * <pre>
239  * A. V. Oppenheim and R. W. Schafer, "Discrete-Time
240  * Signal Processing," Upper Saddle River, N.J.: Prentice
241  * Hall, 2010, pp. 541-545.
242  * </pre>
243  *
244  * \param ntaps Number of coefficients in the window.
245  * \param beta Shaping parameter of the window. See the
246  * discussion in Oppenheim and Schafer.
247  */
248  static std::vector<float> kaiser(int ntaps, double beta);
249 
250  /*!
251  * \brief Build a Barlett (triangular) window.
252  *
253  * See:
254  * <pre>
255  * A. V. Oppenheim and R. W. Schafer, "Discrete-Time
256  * Signal Processing," Upper Saddle River, N.J.: Prentice
257  * Hall, 2010, pp. 535-538.
258  * </pre>
259  *
260  * \param ntaps Number of coefficients in the window.
261  */
262  static std::vector<float> bartlett(int ntaps);
263 
264  static std::vector<float> welch(int ntaps);
265 
266  /*!
267  * \brief Build a Parzen (or de la Valle-Poussin) window.
268  *
269  * See:
270  * <pre>
271  * A. D. Poularikas, "Handbook of Formulas and Tables for
272  * Signal Processing," Springer, Oct 28, 1998
273  * </pre>
274  *
275  * \param ntaps Number of coefficients in the window.
276  */
277  static std::vector<float> parzen(int ntaps);
278 
279  /*!
280  * \brief Build an exponential window with a given decay.
281  *
282  * See: http://en.wikipedia.org/wiki/Window_function#Exponential_or_Poisson_window
283  *
284  * \param ntaps Number of coefficients in the window.
285  * \param d Decay of \p d dB over half the window length.
286  */
287  static std::vector<float> exponential(int ntaps, double d);
288 
289  /*!
290  * \brief Build a Riemann window.
291  *
292  * See:
293  * <pre>
294  * A. D. Poularikas, "Handbook of Formulas and Tables for
295  * Signal Processing," Springer, Oct 28, 1998
296  * </pre>
297  *
298  * \param ntaps Number of coefficients in the window.
299  */
300  static std::vector<float> riemann(int ntaps);
301 
302  /*!
303  * \brief Build a Tukey window.
304  * <pre>
305  * Bloomfield, P. Fourier Analysis of Time Series: An Introduction. New York:
306  * Wiley-Interscience, 2000, pp 69 (eqn 6.9)
307  * </pre>
308  *
309  * \param ntaps Number of coefficients in the window.
310  * \param alpha Shaping parameter for the Tukey window, an
311  * alpha of zero is equivalent to a rectangular
312  * window, an alpha of 1 is equivalent to Hann.
313  */
314  static std::vector<float> tukey(int ntaps, float alpha);
315 
316  /*!
317  * \brief Build a Gaussian window using the equation
318  * <pre>
319  * exp(-(1/2) * (n/sigma)^2)
320  * </pre>
321  *
322  * \param ntaps Number of coefficients in the window.
323  * \param sigma Standard deviation of gaussian distribution.
324  */
325  static std::vector<float> gaussian(int ntaps, float sigma);
326 
327  /*!
328  * \brief Build a window using gr::fft::win_type to index the
329  * type of window desired.
330  *
331  * \param type a gr::fft::win_type index for the type of window.
332  * \param ntaps Number of coefficients in the window.
333  * \param beta Used only for building Kaiser windows.
334  * \param normalize If true, return a window with unit power
335  */
336  static std::vector<float>
337  build(win_type type, int ntaps, double beta = 6.76, const bool normalize = false);
338 };
339 
340 } /* namespace fft */
341 } /* namespace gr */
342 
343 #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:25
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 > build(win_type type, int ntaps, double beta=6.76, const bool normalize=false)
Build a window using gr::fft::win_type to index the type of window desired.
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 continuous first derivative window.
static std::vector< float > nuttall(int ntaps)
Build a Nuttall (or Blackman-Nuttall) window.
static std::vector< float > blackman_harris(int ntaps, int atten=92)
Build a Blackman-harris window with a given attenuation.
static std::vector< float > nuttal(int ntaps)
static std::vector< float > riemann(int ntaps)
Build a Riemann window.
static std::vector< float > blackman_nuttal(int ntaps)
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 std::vector< float > welch(int ntaps)
static double max_attenuation(win_type type, double beta=6.76)
Given a window::win_type, this tells you the maximum attenuation you can expect.
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 > nuttal_cfd(int ntaps)
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 for log4cpp library (C++ port of log4j)
Definition: basic_block.h:29