c@121
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@121
|
2 /*
|
c@121
|
3 Constant-Q library
|
c@121
|
4 Copyright (c) 2013-2014 Queen Mary, University of London
|
c@121
|
5
|
c@121
|
6 Permission is hereby granted, free of charge, to any person
|
c@121
|
7 obtaining a copy of this software and associated documentation
|
c@121
|
8 files (the "Software"), to deal in the Software without
|
c@121
|
9 restriction, including without limitation the rights to use, copy,
|
c@121
|
10 modify, merge, publish, distribute, sublicense, and/or sell copies
|
c@121
|
11 of the Software, and to permit persons to whom the Software is
|
c@121
|
12 furnished to do so, subject to the following conditions:
|
c@121
|
13
|
c@121
|
14 The above copyright notice and this permission notice shall be
|
c@121
|
15 included in all copies or substantial portions of the Software.
|
c@121
|
16
|
c@121
|
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
c@121
|
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
c@121
|
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
c@121
|
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
c@121
|
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
c@121
|
22 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
c@121
|
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
c@121
|
24
|
c@121
|
25 Except as contained in this notice, the names of the Centre for
|
c@121
|
26 Digital Music; Queen Mary, University of London; and Chris Cannam
|
c@121
|
27 shall not be used in advertising or otherwise to promote the sale,
|
c@121
|
28 use or other dealings in this Software without prior written
|
c@121
|
29 authorization.
|
c@121
|
30 */
|
c@121
|
31
|
c@179
|
32 #ifndef WINDOW_H
|
c@179
|
33 #define WINDOW_H
|
c@121
|
34
|
c@121
|
35 #include <cmath>
|
c@121
|
36 #include <iostream>
|
c@121
|
37 #include <map>
|
c@121
|
38 #include <vector>
|
c@121
|
39
|
c@179
|
40 #include "pi.h"
|
c@179
|
41
|
c@121
|
42 enum WindowType {
|
c@121
|
43 RectangularWindow,
|
c@121
|
44 BartlettWindow,
|
c@121
|
45 HammingWindow,
|
c@121
|
46 HanningWindow,
|
c@121
|
47 BlackmanWindow,
|
c@121
|
48 BlackmanHarrisWindow,
|
c@121
|
49
|
c@121
|
50 FirstWindow = RectangularWindow,
|
c@121
|
51 LastWindow = BlackmanHarrisWindow
|
c@121
|
52 };
|
c@121
|
53
|
c@121
|
54 /**
|
c@121
|
55 * Various shaped windows for sample frame conditioning, including
|
c@121
|
56 * cosine windows (Hann etc) and triangular and rectangular windows.
|
c@121
|
57 */
|
c@121
|
58 template <typename T>
|
c@121
|
59 class Window
|
c@121
|
60 {
|
c@121
|
61 public:
|
c@121
|
62 /**
|
c@121
|
63 * Construct a windower of the given type and size.
|
c@121
|
64 *
|
c@121
|
65 * Note that the cosine windows are periodic by design, rather
|
c@121
|
66 * than symmetrical. (A window of size N is equivalent to a
|
c@121
|
67 * symmetrical window of size N+1 with the final element missing.)
|
c@121
|
68 */
|
c@121
|
69 Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); }
|
c@121
|
70 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
|
c@121
|
71 Window &operator=(const Window &w) {
|
c@121
|
72 if (&w == this) return *this;
|
c@121
|
73 m_type = w.m_type;
|
c@121
|
74 m_size = w.m_size;
|
c@121
|
75 encache();
|
c@121
|
76 return *this;
|
c@121
|
77 }
|
c@121
|
78 virtual ~Window() { delete[] m_cache; }
|
c@121
|
79
|
c@121
|
80 void cut(T *src) const { cut(src, src); }
|
c@121
|
81 void cut(const T *src, T *dst) const {
|
c@121
|
82 for (int i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
|
c@121
|
83 }
|
c@121
|
84
|
c@121
|
85 WindowType getType() const { return m_type; }
|
c@121
|
86 int getSize() const { return m_size; }
|
c@121
|
87
|
c@121
|
88 std::vector<T> getWindowData() const {
|
c@121
|
89 std::vector<T> d;
|
c@121
|
90 for (int i = 0; i < m_size; ++i) {
|
c@121
|
91 d.push_back(m_cache[i]);
|
c@121
|
92 }
|
c@121
|
93 return d;
|
c@121
|
94 }
|
c@121
|
95
|
c@121
|
96 protected:
|
c@121
|
97 WindowType m_type;
|
c@121
|
98 int m_size;
|
c@121
|
99 T *m_cache;
|
c@121
|
100
|
c@121
|
101 void encache();
|
c@121
|
102 };
|
c@121
|
103
|
c@121
|
104 template <typename T>
|
c@121
|
105 void Window<T>::encache()
|
c@121
|
106 {
|
c@121
|
107 int n = m_size;
|
c@121
|
108 T *mult = new T[n];
|
c@121
|
109 int i;
|
c@121
|
110 for (i = 0; i < n; ++i) mult[i] = 1.0;
|
c@121
|
111
|
c@121
|
112 switch (m_type) {
|
c@121
|
113
|
c@121
|
114 case RectangularWindow:
|
c@121
|
115 for (i = 0; i < n; ++i) {
|
c@121
|
116 mult[i] = mult[i] * 0.5;
|
c@121
|
117 }
|
c@121
|
118 break;
|
c@121
|
119
|
c@121
|
120 case BartlettWindow:
|
c@121
|
121 if (n == 2) {
|
c@121
|
122 mult[0] = mult[1] = 0; // "matlab compatible"
|
c@121
|
123 } else if (n == 3) {
|
c@121
|
124 mult[0] = 0;
|
c@121
|
125 mult[1] = mult[2] = 2./3.;
|
c@121
|
126 } else if (n > 3) {
|
c@121
|
127 for (i = 0; i < n/2; ++i) {
|
c@121
|
128 mult[i] = mult[i] * (i / T(n/2));
|
c@121
|
129 mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2)));
|
c@121
|
130 }
|
c@121
|
131 }
|
c@121
|
132 break;
|
c@121
|
133
|
c@121
|
134 case HammingWindow:
|
c@121
|
135 if (n > 1) {
|
c@121
|
136 for (i = 0; i < n; ++i) {
|
c@121
|
137 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
|
c@121
|
138 }
|
c@121
|
139 }
|
c@121
|
140 break;
|
c@121
|
141
|
c@121
|
142 case HanningWindow:
|
c@121
|
143 if (n > 1) {
|
c@121
|
144 for (i = 0; i < n; ++i) {
|
c@121
|
145 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
|
c@121
|
146 }
|
c@121
|
147 }
|
c@121
|
148 break;
|
c@121
|
149
|
c@121
|
150 case BlackmanWindow:
|
c@121
|
151 if (n > 1) {
|
c@121
|
152 for (i = 0; i < n; ++i) {
|
c@121
|
153 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
|
c@121
|
154 + 0.08 * cos(4 * M_PI * i / n));
|
c@121
|
155 }
|
c@121
|
156 }
|
c@121
|
157 break;
|
c@121
|
158
|
c@121
|
159 case BlackmanHarrisWindow:
|
c@121
|
160 if (n > 1) {
|
c@121
|
161 for (i = 0; i < n; ++i) {
|
c@121
|
162 mult[i] = mult[i] * (0.35875
|
c@121
|
163 - 0.48829 * cos(2 * M_PI * i / n)
|
c@121
|
164 + 0.14128 * cos(4 * M_PI * i / n)
|
c@121
|
165 - 0.01168 * cos(6 * M_PI * i / n));
|
c@121
|
166 }
|
c@121
|
167 }
|
c@121
|
168 break;
|
c@121
|
169 }
|
c@121
|
170
|
c@121
|
171 m_cache = mult;
|
c@121
|
172 }
|
c@121
|
173
|
c@121
|
174 #endif
|