Logo coherent WaveBurst  
Library Reference Guide
Logo
Window.cc
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Gabriele Vedovato
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 /***************************************************************************
20  CWB::Window.cc - description
21  -------------------
22  begin : Sun May 2 2004
23  copyright : (C) 2004 by Gabriele Vedovato
24  email : vedovato@lnl.infn.it
25  ***************************************************************************/
26 
27 /***************************************************************************
28  * *
29  * This program is free software; you can redistribute it and/or modify *
30  * it under the terms of the GNU General Public License as published by *
31  * the Free Software Foundation; either version 2 of the License, or *
32  * (at your option) any later version. *
33  * *
34  ***************************************************************************/
35 
36 #include "Window.hh"
37 
38 CWB::Window::Window(char* formula, unsigned n, double fParameter) {
39 
40  if (n <= 0) {cout << "CWB::Window::Window - window size must be > 0" << endl;exit(1);}
41  if (formula == NULL) {cout << "CWB::Window::Window - window formula not defined" << endl;exit(1);}
42 
43  window = new double[n];
44  size = n;
45 
46  bool iswindow = false;
47 
48  // initialize window
49 
50  if (strcmp(formula,"barthann") == 0) {barthann (window, n);iswindow=true;}
51  if (strcmp(formula,"bartlett") == 0) {bartlett (window, n);iswindow=true;}
52  if (strcmp(formula,"blackman") == 0) {blackman (window, n);iswindow=true;}
53  if (strcmp(formula,"blackmanharris") == 0) {blackmanharris (window, n);iswindow=true;}
54  if (strcmp(formula,"bohman") == 0) {bohman (window, n);iswindow=true;}
55  if (strcmp(formula,"flattop") == 0) {flattop (window, n);iswindow=true;}
56  if (strcmp(formula,"gauss") == 0) {gauss (window, n, fParameter);iswindow=true;}
57  if (strcmp(formula,"hamming") == 0) {hamming (window, n);iswindow=true;}
58  if (strcmp(formula,"hann") == 0) {hann (window, n);iswindow=true;}
59  if (strcmp(formula,"nuttall") == 0) {nuttall (window, n);iswindow=true;}
60  if (strcmp(formula,"rectangular") == 0) {rectangular (window, n);iswindow=true;}
61  if (strcmp(formula,"triangular") == 0) {triangular (window, n);iswindow=true;}
62  if (strcmp(formula,"tuckey") == 0) {tuckey (window, n, fParameter);iswindow=true;}
63  if (strcmp(formula,"welch") == 0) {welch (window, n);iswindow=true;}
64 
65  // user window
66  if (iswindow==false) {cout << "CWB::Window::Window - window not defined" << endl;exit(1);}
67 
68  // normalize window
69  Normalize(window, n);
70 }
71 
73  delete [] window;
74 }
75 
76 double
78  if ((i<0) || (i>size)) {cout << "CWB::Window::GetValue - index not allowed" << endl;exit(1);}
79  return window[i];
80 }
81 
82 void
83 CWB::Window::Normalize (double* out_window, unsigned n) {
84  double norm = 0;
85  for (unsigned int i=0;i<n;i++) norm += pow(out_window[i],2);
86  norm /= n;
87  for (unsigned int i=0;i<n;i++) out_window[i] /= sqrt(norm);
88 }
89 
90 void
91 CWB::Window::barthann (double* out_window, unsigned n) {
92  unsigned i = 0;
93 
94  for (i = 0; i < n; i++) {
95  double f = 0.0;
96  f = ((double) i) / ((double) (n - 1));
97  out_window[i] = 0.62 -0.48*(f - 0.5) +
98  0.38 * cos(2 * M_PI * (f - 0.5));
99  }
100 }
101 
102 void
103 CWB::Window::bartlett (double* out_window, unsigned n) {
104  unsigned i = 0;
105  unsigned odd = 0;
106 
107  odd = n % 2;
108 
109  for (i = 0; i < (n/2) +odd; i++)
110  out_window[i] = 2.0 * ((double) i) / ((double) (n - 1));
111  for (i = (n/2) + odd; i < n; i++)
112  out_window[i] = 2.0 - 2.0 * ((double) i) / ((double) (n - 1));
113 }
114 
115 void
116 CWB::Window::blackman (double* out_window, unsigned n) {
117  unsigned i = 0;
118 
119  for (i = 0; i < n; i++)
120  {
121  double f = 0.0;
122  f = ((double) i) / ((double) (n - 1));
123  out_window[i] = (0.42
124  - 0.5 * cos (2.0 * M_PI * f)
125  + 0.08 * cos (4.0 * M_PI * f));
126  }
127 }
128 
129 void
130 CWB::Window::blackmanharris (double* out_window, unsigned n) {
131  unsigned i = 0;
132 
133  for (i = 0; i < n; i++)
134  {
135  double f = 0.0;
136  f = ((double) i) / ((double) (n - 1));
137  out_window[i] = 0.35875 -
138  0.48829 * cos(2.0 * M_PI * f) +
139  0.14128 * cos(4.0 * M_PI * f) -
140  0.01168 * cos(6.0 * M_PI * f);
141  }
142 }
143 
144 void
145 CWB::Window::bohman (double* out_window, unsigned n) {
146  unsigned i = 0;
147 
148  for (i = 0; i < n; i++)
149  {
150  double f = 0.0;
151  f = (((double) i) - ((double) (n / 2))) /
152  ((double) (n / 2));
153  out_window[i] = (1.0 - f) * cos(M_PI * f) +
154  (1 / M_PI) * sin(M_PI * f);
155  }
156 }
157 
158 /* [UNIMPLEMENTED] chebyshev */
159 
160 void
161 CWB::Window::flattop (double* out_window, unsigned n) {
162  unsigned i = 0;
163 
164  for (i = 0; i < n; i++)
165  {
166  double f = 0.0;
167  f = ((double) i) / ((double) (n - 1));
168  out_window[i] = 1.0 -
169  1.93 * cos(2.0 * M_PI * f) +
170  1.29 * cos(4.0 * M_PI * f) -
171  0.388 * cos(6.0 * M_PI * f) +
172  0.322 * cos(8.0 * M_PI * f);
173  }
174 }
175 
176 void
177 CWB::Window::gauss (double* out_window, unsigned n, double alpha) {
178  unsigned i = 0;
179 
180  if (alpha < 2) alpha = 2.5;
181 
182  for (i = 0; i < n; i++)
183  {
184  double f = 0.0;
185  f = (((double) i) - ((double) (n / 2))) /
186  ((double) (n / 2));
187  out_window[i] = exp(-0.5 * (alpha * f) * (alpha * f));
188  }
189 }
190 
191 void
192 CWB::Window::hamming (double* out_window, unsigned n) {
193  unsigned i = 0;
194 
195  for (i = 0; i < n; i++)
196  {
197  double f = 0.0;
198  f = ((double) i) / ((double) (n - 1));
199  out_window[i] = 0.54 - 0.46 * cos (2.0 * M_PI * f);
200  }
201 }
202 
203 void
204 CWB::Window::hann (double* out_window, unsigned n) {
205  unsigned i = 0;
206 
207  for (i = 0; i < n; i++)
208  {
209  double f = 0.0;
210  f = ((double) i) / ((double) (n - 1));
211  out_window[i] = 0.5 - 0.5 * cos (2.0 * M_PI * f);
212  }
213 }
214 
215 /* [UNIMPLEMENTED] kaiser */
216 
217 void
218 CWB::Window::nuttall (double* out_window, unsigned n) {
219  unsigned i = 0;
220 
221  for (i = 0; i < n; i++)
222  {
223  double f = 0.0;
224  f = ((double) i) / ((double) (n - 1));
225  out_window[i] = 0.3635819 -
226  0.4891775 * cos(2.0 * M_PI * f) +
227  0.1365995 * cos(4.0 * M_PI * f) -
228  0.0106411 * cos(6.0 * M_PI * f);
229  }
230 }
231 
232 /* [UNIMPLEMENTED] parzen */
233 void
234 CWB::Window::rectangular (double* out_window, unsigned n) {
235  unsigned i = 0;
236 
237  for (i = 0; i < n; i++)
238  {
239  out_window[i] = 1.0;
240  }
241 }
242 
243 /* [UNIMPLEMENTED] saramaki */
244 
245 /* [UNIMPLEMENTED] transversal */
246 
247 void
248 CWB::Window::triangular (double* out_window, unsigned n) {
249  unsigned i = 0;
250  unsigned odd = 0;
251  unsigned mirror = 0;
252 
253  /* helpful constants */
254  odd = n % 2;
255  mirror = (n + odd) / 2;
256 
257  /* fill the first half */
258  for (i = 0; i < mirror; i++)
259  {
260  unsigned k = 0;
261  k = 2 * (i + 1) + odd - 1;
262  out_window[i] = (((double) k) / ((double) (n + odd)));
263  }
264  /* and mirror the other */
265  for (i = mirror; i < n; i++)
266  {
267  out_window[i] = out_window[n - i - 1];
268  }
269 }
270 
271 void
272 CWB::Window::tuckey (double* out_window, unsigned n, double r) {
273  unsigned i = 0;
274 
275  for (i = 0; i < (((double) n) / 2.0) * (1 + r); i++)
276  out_window[i] = 1.0;
277  for (; i < n; i++)
278  {
279  double f = 0.0;
280  f = ((double) i) - (((double) n) / 2.0) * (1 + r);
281  f = f / (((double) n) * (1 - r));
282  out_window[i] = 0.5 * ( 1.0 + cos(M_PI * f));
283  }
284 }
285 
286 void
287 CWB::Window::welch (double* out_window, unsigned n) {
288  unsigned i = 0;
289 
290  for (i = 0; i < n; i++)
291  {
292  out_window[i] = 1-pow(((double)i-(double)n/2.0)/((double)n/2.0),2);
293  }
294 }
void blackmanharris(double *out_window, unsigned n)
Definition: Window.cc:130
void flattop(double *out_window, unsigned n)
Definition: Window.cc:161
void hamming(double *out_window, unsigned n)
Definition: Window.cc:192
int n
Definition: cwb_net.C:28
i drho i
void gauss(double *out_window, unsigned n, double alpha)
Definition: Window.cc:177
cout<< "SNR "<< xsnr<< endl;wavearray< double > f
Definition: ComputeSNR.C:75
double GetValue(unsigned i)
Definition: Window.cc:77
void hann(double *out_window, unsigned n)
Definition: Window.cc:204
void blackman(double *out_window, unsigned n)
Definition: Window.cc:116
void rectangular(double *out_window, unsigned n)
Definition: Window.cc:234
void tuckey(double *out_window, unsigned n, double r)
Definition: Window.cc:272
void triangular(double *out_window, unsigned n)
Definition: Window.cc:248
void bohman(double *out_window, unsigned n)
Definition: Window.cc:145
void welch(double *out_window, unsigned n)
Definition: Window.cc:287
int k
void nuttall(double *out_window, unsigned n)
Definition: Window.cc:218
unsigned size
Definition: Window.hh:88
regression r
Definition: Regression_H1.C:44
void barthann(double *out_window, unsigned n)
Definition: Window.cc:91
void bartlett(double *out_window, unsigned n)
Definition: Window.cc:103
char formula[256]
void Normalize(double *out_window, unsigned n)
Definition: Window.cc:83
Window(char *formula, unsigned n, double fParameter=0)
Definition: Window.cc:38
double * window
Definition: Window.hh:87
exit(0)