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  QM DSP library
5  Centre for Digital Music, Queen Mary, University of London.
6  This file Copyright 2006 Chris Cannam.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 #ifndef QM_DSP_WINDOW_H
16 #define QM_DSP_WINDOW_H
17 
18 #include <cmath>
19 #include <iostream>
20 #include <map>
21 #include <vector>
22 
23 enum WindowType {
30 
33 };
34 
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(const T *src, T *dst) const {
63  for (int i = 0; i < m_size; ++i) {
64  dst[i] = src[i] * m_cache[i];
65  }
66  }
67 
68  WindowType getType() const { return m_type; }
69  int getSize() const { return m_size; }
70 
71  std::vector<T> getWindowData() const {
72  std::vector<T> d;
73  for (int i = 0; i < m_size; ++i) {
74  d.push_back(m_cache[i]);
75  }
76  return d;
77  }
78 
79 protected:
81  int m_size;
82  T *m_cache;
83 
84  void encache();
85 };
86 
87 template <typename T>
89 {
90  int n = m_size;
91  T *mult = new T[n];
92  int i;
93  for (i = 0; i < n; ++i) mult[i] = 1.0;
94 
95  switch (m_type) {
96 
97  case RectangularWindow:
98  for (i = 0; i < n; ++i) {
99  mult[i] = mult[i] * 0.5;
100  }
101  break;
102 
103  case BartlettWindow:
104  if (n == 2) {
105  mult[0] = mult[1] = 0; // "matlab compatible"
106  } else if (n == 3) {
107  mult[0] = 0;
108  mult[1] = mult[2] = 2./3.;
109  } else if (n > 3) {
110  for (i = 0; i < n/2; ++i) {
111  mult[i] = mult[i] * (i / T(n/2));
112  mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2)));
113  }
114  }
115  break;
116 
117  case HammingWindow:
118  if (n > 1) {
119  for (i = 0; i < n; ++i) {
120  mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
121  }
122  }
123  break;
124 
125  case HanningWindow:
126  if (n > 1) {
127  for (i = 0; i < n; ++i) {
128  mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
129  }
130  }
131  break;
132 
133  case BlackmanWindow:
134  if (n > 1) {
135  for (i = 0; i < n; ++i) {
136  mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
137  + 0.08 * cos(4 * M_PI * i / n));
138  }
139  }
140  break;
141 
143  if (n > 1) {
144  for (i = 0; i < n; ++i) {
145  mult[i] = mult[i] * (0.35875
146  - 0.48829 * cos(2 * M_PI * i / n)
147  + 0.14128 * cos(4 * M_PI * i / n)
148  - 0.01168 * cos(6 * M_PI * i / n));
149  }
150  }
151  break;
152  }
153 
154  m_cache = mult;
155 }
156 
157 #endif
std::vector< T > getWindowData() const
Definition: Window.h:71
T * m_cache
Definition: Window.h:82
WindowType m_type
Definition: Window.h:80
void cut(const T *src, T *dst) const
Definition: Window.h:62
int getSize() const
Definition: Window.h:69
Various shaped windows for sample frame conditioning, including cosine windows (Hann etc) and triangu...
Definition: Window.h:40
WindowType getType() const
Definition: Window.h:68
WindowType
Definition: Window.h:23
Window & operator=(const Window &w)
Definition: Window.h:52
void cut(T *src) const
Definition: Window.h:61
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
int m_size
Definition: Window.h:81
void encache()
Definition: Window.h:88
virtual ~Window()
Definition: Window.h:59