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 SV_WINDOW_H
17 #define SV_WINDOW_H
18 
19 #include <cmath>
20 #include <iostream>
21 #include <string>
22 #include <map>
23 #include <cstdlib>
24 
25 #include <bqvec/VectorOps.h>
26 #include <bqvec/Allocators.h>
27 
28 #include "system/System.h"
29 
30 enum WindowType {
40 };
41 
42 template <typename T>
43 class Window
44 {
45 public:
53  Window(WindowType type, int size) : m_type(type), m_size(size), m_cache(0) {
54  encache();
55  }
56  Window(const Window &w) : m_type(w.m_type), m_size(w.m_size), m_cache(0) {
57  encache();
58  }
59  Window &operator=(const Window &w) {
60  if (&w == this) return *this;
61  m_type = w.m_type;
62  m_size = w.m_size;
63  encache();
64  return *this;
65  }
66  virtual ~Window() {
67  breakfastquay::deallocate(m_cache);
68  }
69 
70  inline void cut(T *const BQ_R__ block) const {
71  breakfastquay::v_multiply(block, m_cache, m_size);
72  }
73 
74  inline void cut(const T *const BQ_R__ src, T *const BQ_R__ dst) const {
75  breakfastquay::v_multiply(dst, src, m_cache, m_size);
76  }
77 
78  T getArea() { return m_area; }
79  T getValue(int i) { return m_cache[i]; }
80 
81  WindowType getType() const { return m_type; }
82  int getSize() const { return m_size; }
83 
84  // The names used by these functions are un-translated, for use in
85  // e.g. XML I/O. Use Preferences::getPropertyValueLabel if you
86  // want translated names for use in the user interface.
87  static std::string getNameForType(WindowType type);
88  static WindowType getTypeForName(std::string name);
89 
90 protected:
92  int m_size;
93  T *BQ_R__ m_cache;
94  T m_area;
95 
96  void encache();
97  void cosinewin(T *, double, double, double, double);
98 };
99 
100 template <typename T>
102 {
103  if (!m_cache) m_cache = breakfastquay::allocate<T>(m_size);
104 
105  const int n = m_size;
106  breakfastquay::v_set(m_cache, T(1.0), n);
107  int i;
108 
109  switch (m_type) {
110 
111  case RectangularWindow:
112  for (i = 0; i < n; ++i) {
113  m_cache[i] *= T(0.5);
114  }
115  break;
116 
117  case BartlettWindow:
118  for (i = 0; i < n/2; ++i) {
119  m_cache[i] *= T(i) / T(n/2);
120  m_cache[i + n/2] *= T(1.0) - T(i) / T(n/2);
121  }
122  break;
123 
124  case HammingWindow:
125  cosinewin(m_cache, 0.54, 0.46, 0.0, 0.0);
126  break;
127 
128  case HanningWindow:
129  cosinewin(m_cache, 0.50, 0.50, 0.0, 0.0);
130  break;
131 
132  case BlackmanWindow:
133  cosinewin(m_cache, 0.42, 0.50, 0.08, 0.0);
134  break;
135 
136  case GaussianWindow:
137  for (i = 0; i < n; ++i) {
138  m_cache[i] *= T(pow(2, - pow((i - (n-1)/2.0) / ((n-1)/2.0 / 3), 2)));
139  }
140  break;
141 
142  case ParzenWindow:
143  {
144  int N = n-1;
145  for (i = 0; i < N/4; ++i) {
146  T m = T(2 * pow(1.0 - (T(N)/2 - T(i)) / (T(N)/2), 3));
147  m_cache[i] *= m;
148  m_cache[N-i] *= m;
149  }
150  for (i = N/4; i <= N/2; ++i) {
151  int wn = i - N/2;
152  T m = T(1.0 - 6 * pow(T(wn) / (T(N)/2), 2) * (1.0 - T(abs(wn)) / (T(N)/2)));
153  m_cache[i] *= m;
154  m_cache[N-i] *= m;
155  }
156  break;
157  }
158 
159  case NuttallWindow:
160  cosinewin(m_cache, 0.3635819, 0.4891775, 0.1365995, 0.0106411);
161  break;
162 
164  cosinewin(m_cache, 0.35875, 0.48829, 0.14128, 0.01168);
165  break;
166  }
167 
168  m_area = 0;
169  for (int i = 0; i < n; ++i) {
170  m_area += m_cache[i];
171  }
172  m_area /= T(n);
173 }
174 
175 template <typename T>
176 void Window<T>::cosinewin(T *mult, double a0, double a1, double a2, double a3)
177 {
178  const int n = m_size;
179  for (int i = 0; i < n; ++i) {
180  mult[i] *= T(a0
181  - a1 * cos((2 * M_PI * i) / n)
182  + a2 * cos((4 * M_PI * i) / n)
183  - a3 * cos((6 * M_PI * i) / n));
184  }
185 }
186 
187 template <typename T>
188 std::string
190 {
191  switch (type) {
192  case RectangularWindow: return "rectangular";
193  case BartlettWindow: return "bartlett";
194  case HammingWindow: return "hamming";
195  case HanningWindow: return "hanning";
196  case BlackmanWindow: return "blackman";
197  case GaussianWindow: return "gaussian";
198  case ParzenWindow: return "parzen";
199  case NuttallWindow: return "nuttall";
200  case BlackmanHarrisWindow: return "blackman-harris";
201  }
202 
203  std::cerr << "WARNING: Window::getNameForType: unknown type "
204  << type << std::endl;
205 
206  return "unknown";
207 }
208 
209 template <typename T>
211 Window<T>::getTypeForName(std::string name)
212 {
213  if (name == "rectangular") return RectangularWindow;
214  if (name == "bartlett") return BartlettWindow;
215  if (name == "hamming") return HammingWindow;
216  if (name == "hanning") return HanningWindow;
217  if (name == "blackman") return BlackmanWindow;
218  if (name == "gaussian") return GaussianWindow;
219  if (name == "parzen") return ParzenWindow;
220  if (name == "nuttall") return NuttallWindow;
221  if (name == "blackman-harris") return BlackmanHarrisWindow;
222 
223  std::cerr << "WARNING: Window::getTypeForName: unknown name \""
224  << name << "\", defaulting to \"hanning\"" << std::endl;
225 
226  return HanningWindow;
227 }
228 
229 #endif
void cut(const T *const BQ_R__ src, T *const BQ_R__ dst) const
Definition: Window.h:74
static std::string getNameForType(WindowType type)
Definition: Window.h:189
#define M_PI
Definition: System.h:187
T getArea()
Definition: Window.h:78
WindowType m_type
Definition: Window.h:91
int getSize() const
Definition: Window.h:82
void cut(T *const BQ_R__ block) const
Definition: Window.h:70
Definition: Window.h:43
T m_area
Definition: Window.h:94
static WindowType getTypeForName(std::string name)
Definition: Window.h:211
WindowType getType() const
Definition: Window.h:81
WindowType
Definition: Window.h:30
void cosinewin(T *, double, double, double, double)
Definition: Window.h:176
T *BQ_R__ m_cache
Definition: Window.h:93
Window & operator=(const Window &w)
Definition: Window.h:59
T getValue(int i)
Definition: Window.h:79
Window(const Window &w)
Definition: Window.h:56
Window(WindowType type, int size)
Construct a windower of the given type and size.
Definition: Window.h:53
int m_size
Definition: Window.h:92
void encache()
Definition: Window.h:101
virtual ~Window()
Definition: Window.h:66