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