SigPack - the C++ signal processing library
window.h
Go to the documentation of this file.
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 #ifndef SP_WINDOW_H
5 #define SP_WINDOW_H
6 namespace sp
7 {
15 
25 arma_inline arma::vec cos_win(const arma::uword N, const arma::vec &a)
26 {
27  arma::vec h(N);
28  for (arma::uword i = 0; i < N; i++)
29  {
30  h[i] = a[0] - a[1] * std::cos(1.0 * PI_2 * i / (N - 1)) +
31  a[2] * std::cos(2.0 * PI_2 * i / (N - 1)) -
32  a[3] * std::cos(3.0 * PI_2 * i / (N - 1)) +
33  a[4] * std::cos(4.0 * PI_2 * i / (N - 1));
34  }
35  return h;
36 }
37 
44 arma_inline arma::vec hamming(const arma::uword N)
45 {
46  arma::vec a = arma::zeros<arma::vec>(5);
47  a[0] = 0.54;
48  a[1] = 0.46;
49  return cos_win(N, a);
50 }
51 
58 arma_inline arma::vec hann(const arma::uword N)
59 {
60  arma::vec a = arma::zeros<arma::vec>(5);
61  a[0] = 0.5;
62  a[1] = 0.5;
63  return cos_win(N, a);
64 }
65 
72 arma_inline arma::vec blackman(const arma::uword N)
73 {
74  arma::vec a = arma::zeros<arma::vec>(5);
75  a[0] = 0.42; // 7938/18608.0
76  a[1] = 0.5; // 9240/18608.0
77  a[2] = 0.08; // 1430/18608.0
78  return cos_win(N, a);
79 }
80 
89 arma_inline arma::vec blackmanharris(const arma::uword N)
90 {
91  arma::vec a = arma::zeros<arma::vec>(5);
92  a[0] = 0.35875;
93  a[1] = 0.48829;
94  a[2] = 0.14128;
95  a[3] = 0.01168;
96  return cos_win(N, a);
97 }
98 
106 arma_inline arma::vec flattopwin(const arma::uword N)
107 {
108  arma::vec a = arma::zeros<arma::vec>(5);
109  a[0] = 0.21557895;
110  a[1] = 0.41663158;
111  a[2] = 0.277263158;
112  a[3] = 0.083578947;
113  a[4] = 0.006947368;
114  return cos_win(N, a);
115 }
116 
123 arma_inline arma::vec hanning(const arma::uword N)
124 {
125  arma::vec h(N);
126  for (arma::uword i = 0; i < N; i++)
127  {
128  h[i] = 0.5 - 0.5 * std::cos(PI_2 * (i + 1) / (N + 1));
129  }
130  return h;
131 }
132 
141 arma_inline arma::vec kaiser(const arma::uword N, double beta)
142 {
143  arma::vec h(N);
144  double bb = besseli0(beta);
145  for (arma::uword i = 0; i < N; i++)
146  {
147  h[i] = besseli0(beta * sqrt(4.0 * i * (N - 1 - i)) / (N - 1)) / bb;
148  }
149  return h;
150 }
151 
159 arma_inline arma::vec triang(const arma::uword N)
160 {
161  arma::vec h(N);
162  if (N % 2) // Odd
163  {
164  for (arma::uword i = 0; i < (N - 1) / 2; i++)
165  {
166  h[i] = 2.0 * (i + 1) / (N + 1);
167  h[N - i - 1] = h[i];
168  }
169  h[(N - 1) / 2] = 1.0;
170  }
171  else // Even
172  {
173  for (arma::uword i = 0; i < N / 2; i++)
174  {
175  h[i] = (2.0 * i + 1) / N;
176  h[N - i - 1] = h[i];
177  }
178  }
179  return h;
180 }
182 } // namespace sp
183 #endif
arma_inline arma::vec blackmanharris(const arma::uword N)
Blackman-Harris window. Symmetric BH4 window.
Definition: window.h:89
arma_inline arma::vec flattopwin(const arma::uword N)
Flattop window.
Definition: window.h:106
Definition: base.h:7
arma_inline arma::vec hamming(const arma::uword N)
Hamming window.
Definition: window.h:44
arma_inline arma::vec hann(const arma::uword N)
Hann window.
Definition: window.h:58
arma_inline double besseli0(double x)
Modified first kind bessel function order zero.
Definition: base.h:51
arma_inline arma::vec hanning(const arma::uword N)
Hanning window.
Definition: window.h:123
arma_inline arma::vec triang(const arma::uword N)
Triangle window.
Definition: window.h:159
const double PI_2
Definition: base.h:15
arma_inline arma::vec cos_win(const arma::uword N, const arma::vec &a)
Generic fifth order symmetric cos window.
Definition: window.h:25
arma_inline arma::vec blackman(const arma::uword N)
Blackman window.
Definition: window.h:72
arma_inline arma::vec kaiser(const arma::uword N, double beta)
Kaiser window.
Definition: window.h:141