svcore  1.9
Window.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef _WINDOW_H_
17 #define _WINDOW_H_
18 
19 #include <cmath>
20 #include <iostream>
21 #include <string>
22 #include <map>
23 #include <cstdlib>
24 
25 #include <cstdlib>
26 
27 enum WindowType {
37 };
38 
39 template <typename T>
40 class Window
41 {
42 public:
50  Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); }
51  Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
52  Window &operator=(const Window &w) {
53  if (&w == this) return *this;
54  m_type = w.m_type;
55  m_size = w.m_size;
56  encache();
57  return *this;
58  }
59  virtual ~Window() { delete[] m_cache; }
60 
61  void cut(T *src) const { cut(src, src); }
62  void cut(T *src, T *dst) const {
63  for (int i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
64  }
65 
66  T getArea() { return m_area; }
67  T getValue(int i) { return m_cache[i]; }
68 
69  WindowType getType() const { return m_type; }
70  int getSize() const { return m_size; }
71 
72  // The names used by these functions are un-translated, for use in
73  // e.g. XML I/O. Use Preferences::getPropertyValueLabel if you
74  // want translated names for use in the user interface.
75  static std::string getNameForType(WindowType type);
76  static WindowType getTypeForName(std::string name);
77 
78 protected:
80  int m_size;
81  T *m_cache;
82  T m_area;
83 
84  void encache();
85  void cosinewin(T *, T, T, T, T);
86 };
87 
88 template <typename T>
90 {
91  const int n = m_size;
92  T *mult = new T[n];
93  int i;
94  for (i = 0; i < n; ++i) mult[i] = 1.0;
95 
96  switch (m_type) {
97 
98  case RectangularWindow:
99  for (i = 0; i < n; ++i) {
100  mult[i] *= 0.5;
101  }
102  break;
103 
104  case BartlettWindow:
105  for (i = 0; i < n/2; ++i) {
106  mult[i] *= (i / T(n/2));
107  mult[i + n/2] *= (1.0 - (i / T(n/2)));
108  }
109  break;
110 
111  case HammingWindow:
112  cosinewin(mult, 0.54, 0.46, 0.0, 0.0);
113  break;
114 
115  case HanningWindow:
116  cosinewin(mult, 0.50, 0.50, 0.0, 0.0);
117  break;
118 
119  case BlackmanWindow:
120  cosinewin(mult, 0.42, 0.50, 0.08, 0.0);
121  break;
122 
123  case GaussianWindow:
124  for (i = 0; i < n; ++i) {
125  mult[i] *= pow(2, - pow((i - (n-1)/2.0) / ((n-1)/2.0 / 3), 2));
126  }
127  break;
128 
129  case ParzenWindow:
130  {
131  int N = n-1;
132  for (i = 0; i < N/4; ++i) {
133  T m = 2 * pow(1.0 - (T(N)/2 - i) / (T(N)/2), 3);
134  mult[i] *= m;
135  mult[N-i] *= m;
136  }
137  for (i = N/4; i <= N/2; ++i) {
138  int wn = i - N/2;
139  T m = 1.0 - 6 * pow(wn / (T(N)/2), 2) * (1.0 - abs(wn) / (T(N)/2));
140  mult[i] *= m;
141  mult[N-i] *= m;
142  }
143  break;
144  }
145 
146  case NuttallWindow:
147  cosinewin(mult, 0.3635819, 0.4891775, 0.1365995, 0.0106411);
148  break;
149 
151  cosinewin(mult, 0.35875, 0.48829, 0.14128, 0.01168);
152  break;
153  }
154 
155  m_cache = mult;
156 
157  m_area = 0;
158  for (int i = 0; i < n; ++i) {
159  m_area += m_cache[i];
160  }
161  m_area /= n;
162 }
163 
164 template <typename T>
165 void Window<T>::cosinewin(T *mult, T a0, T a1, T a2, T a3)
166 {
167  const int n = m_size;
168  for (int i = 0; i < n; ++i) {
169  mult[i] *= (a0
170  - a1 * cos((2 * M_PI * i) / n)
171  + a2 * cos((4 * M_PI * i) / n)
172  - a3 * cos((6 * M_PI * i) / n));
173  }
174 }
175 
176 template <typename T>
177 std::string
179 {
180  switch (type) {
181  case RectangularWindow: return "rectangular";
182  case BartlettWindow: return "bartlett";
183  case HammingWindow: return "hamming";
184  case HanningWindow: return "hanning";
185  case BlackmanWindow: return "blackman";
186  case GaussianWindow: return "gaussian";
187  case ParzenWindow: return "parzen";
188  case NuttallWindow: return "nuttall";
189  case BlackmanHarrisWindow: return "blackman-harris";
190  }
191 
192  std::cerr << "WARNING: Window::getNameForType: unknown type "
193  << type << std::endl;
194 
195  return "unknown";
196 }
197 
198 template <typename T>
200 Window<T>::getTypeForName(std::string name)
201 {
202  if (name == "rectangular") return RectangularWindow;
203  if (name == "bartlett") return BartlettWindow;
204  if (name == "hamming") return HammingWindow;
205  if (name == "hanning") return HanningWindow;
206  if (name == "blackman") return BlackmanWindow;
207  if (name == "gaussian") return GaussianWindow;
208  if (name == "parzen") return ParzenWindow;
209  if (name == "nuttall") return NuttallWindow;
210  if (name == "blackman-harris") return BlackmanHarrisWindow;
211 
212  std::cerr << "WARNING: Window::getTypeForName: unknown name \""
213  << name << "\", defaulting to \"hanning\"" << std::endl;
214 
215  return HanningWindow;
216 }
217 
218 #endif
static std::string getNameForType(WindowType type)
Definition: Window.h:178
T getArea()
Definition: Window.h:66
T * m_cache
Definition: Window.h:81
WindowType m_type
Definition: Window.h:79
Definition: Window.h:40
T m_area
Definition: Window.h:82
static WindowType getTypeForName(std::string name)
Definition: Window.h:200
WindowType
Definition: Window.h:27
Window & operator=(const Window &w)
Definition: Window.h:52
void cut(T *src, T *dst) const
Definition: Window.h:62
int getSize() const
Definition: Window.h:70
T getValue(int i)
Definition: Window.h:67
void cosinewin(T *, T, T, T, T)
Definition: Window.h:165
Window(const Window &w)
Definition: Window.h:51
Window(WindowType type, int size)
Construct a windower of the given type and size.
Definition: Window.h:50
WindowType getType() const
Definition: Window.h:69
int m_size
Definition: Window.h:80
void encache()
Definition: Window.h:89
virtual ~Window()
Definition: Window.h:59
void cut(T *src) const
Definition: Window.h:61