c@225
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@225
|
2
|
c@225
|
3 /*
|
c@225
|
4 QM DSP library
|
c@225
|
5 Centre for Digital Music, Queen Mary, University of London.
|
c@225
|
6 This file Copyright 2006 Chris Cannam.
|
c@309
|
7
|
c@309
|
8 This program is free software; you can redistribute it and/or
|
c@309
|
9 modify it under the terms of the GNU General Public License as
|
c@309
|
10 published by the Free Software Foundation; either version 2 of the
|
c@309
|
11 License, or (at your option) any later version. See the file
|
c@309
|
12 COPYING included with this distribution for more information.
|
c@225
|
13 */
|
c@225
|
14
|
cannam@489
|
15 #ifndef QM_DSP_WINDOW_H
|
cannam@489
|
16 #define QM_DSP_WINDOW_H
|
c@225
|
17
|
c@225
|
18 #include <cmath>
|
c@225
|
19 #include <iostream>
|
c@225
|
20 #include <map>
|
c@382
|
21 #include <vector>
|
c@225
|
22
|
c@225
|
23 enum WindowType {
|
c@225
|
24 RectangularWindow,
|
c@225
|
25 BartlettWindow,
|
c@225
|
26 HammingWindow,
|
c@225
|
27 HanningWindow,
|
c@225
|
28 BlackmanWindow,
|
c@382
|
29 BlackmanHarrisWindow,
|
c@334
|
30
|
c@334
|
31 FirstWindow = RectangularWindow,
|
c@382
|
32 LastWindow = BlackmanHarrisWindow
|
c@225
|
33 };
|
c@225
|
34
|
c@377
|
35 /**
|
c@377
|
36 * Various shaped windows for sample frame conditioning, including
|
c@377
|
37 * cosine windows (Hann etc) and triangular and rectangular windows.
|
c@377
|
38 */
|
c@225
|
39 template <typename T>
|
c@225
|
40 class Window
|
c@225
|
41 {
|
c@225
|
42 public:
|
c@225
|
43 /**
|
c@334
|
44 * Construct a windower of the given type and size.
|
c@334
|
45 *
|
c@334
|
46 * Note that the cosine windows are periodic by design, rather
|
c@334
|
47 * than symmetrical. (A window of size N is equivalent to a
|
c@334
|
48 * symmetrical window of size N+1 with the final element missing.)
|
c@225
|
49 */
|
c@382
|
50 Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); }
|
c@225
|
51 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
|
c@225
|
52 Window &operator=(const Window &w) {
|
cannam@483
|
53 if (&w == this) return *this;
|
cannam@483
|
54 m_type = w.m_type;
|
cannam@483
|
55 m_size = w.m_size;
|
cannam@483
|
56 encache();
|
cannam@483
|
57 return *this;
|
c@225
|
58 }
|
c@251
|
59 virtual ~Window() { delete[] m_cache; }
|
c@225
|
60
|
c@225
|
61 void cut(T *src) const { cut(src, src); }
|
c@280
|
62 void cut(const T *src, T *dst) const {
|
cannam@494
|
63 for (int i = 0; i < m_size; ++i) {
|
cannam@494
|
64 dst[i] = src[i] * m_cache[i];
|
cannam@494
|
65 }
|
c@225
|
66 }
|
c@225
|
67
|
c@225
|
68 WindowType getType() const { return m_type; }
|
c@382
|
69 int getSize() const { return m_size; }
|
c@382
|
70
|
c@382
|
71 std::vector<T> getWindowData() const {
|
c@382
|
72 std::vector<T> d;
|
c@382
|
73 for (int i = 0; i < m_size; ++i) {
|
c@382
|
74 d.push_back(m_cache[i]);
|
c@382
|
75 }
|
c@382
|
76 return d;
|
c@382
|
77 }
|
c@225
|
78
|
c@225
|
79 protected:
|
c@225
|
80 WindowType m_type;
|
c@382
|
81 int m_size;
|
c@225
|
82 T *m_cache;
|
c@225
|
83
|
c@225
|
84 void encache();
|
c@225
|
85 };
|
c@225
|
86
|
c@225
|
87 template <typename T>
|
c@225
|
88 void Window<T>::encache()
|
c@225
|
89 {
|
c@382
|
90 int n = m_size;
|
c@225
|
91 T *mult = new T[n];
|
c@382
|
92 int i;
|
c@225
|
93 for (i = 0; i < n; ++i) mult[i] = 1.0;
|
c@225
|
94
|
c@225
|
95 switch (m_type) {
|
cannam@483
|
96
|
c@225
|
97 case RectangularWindow:
|
c@334
|
98 for (i = 0; i < n; ++i) {
|
c@334
|
99 mult[i] = mult[i] * 0.5;
|
cannam@483
|
100 }
|
cannam@483
|
101 break;
|
cannam@483
|
102
|
c@225
|
103 case BartlettWindow:
|
c@334
|
104 if (n == 2) {
|
c@334
|
105 mult[0] = mult[1] = 0; // "matlab compatible"
|
c@334
|
106 } else if (n == 3) {
|
c@334
|
107 mult[0] = 0;
|
c@334
|
108 mult[1] = mult[2] = 2./3.;
|
c@334
|
109 } else if (n > 3) {
|
c@334
|
110 for (i = 0; i < n/2; ++i) {
|
c@334
|
111 mult[i] = mult[i] * (i / T(n/2));
|
c@334
|
112 mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2)));
|
c@334
|
113 }
|
cannam@483
|
114 }
|
cannam@483
|
115 break;
|
cannam@483
|
116
|
c@225
|
117 case HammingWindow:
|
c@334
|
118 if (n > 1) {
|
c@334
|
119 for (i = 0; i < n; ++i) {
|
c@334
|
120 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
|
c@334
|
121 }
|
cannam@483
|
122 }
|
cannam@483
|
123 break;
|
cannam@483
|
124
|
c@225
|
125 case HanningWindow:
|
c@334
|
126 if (n > 1) {
|
c@334
|
127 for (i = 0; i < n; ++i) {
|
c@334
|
128 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
|
c@334
|
129 }
|
cannam@483
|
130 }
|
cannam@483
|
131 break;
|
cannam@483
|
132
|
c@225
|
133 case BlackmanWindow:
|
c@334
|
134 if (n > 1) {
|
c@334
|
135 for (i = 0; i < n; ++i) {
|
c@334
|
136 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
|
c@334
|
137 + 0.08 * cos(4 * M_PI * i / n));
|
c@334
|
138 }
|
cannam@483
|
139 }
|
cannam@483
|
140 break;
|
cannam@483
|
141
|
c@382
|
142 case BlackmanHarrisWindow:
|
c@382
|
143 if (n > 1) {
|
c@382
|
144 for (i = 0; i < n; ++i) {
|
c@382
|
145 mult[i] = mult[i] * (0.35875
|
c@382
|
146 - 0.48829 * cos(2 * M_PI * i / n)
|
c@382
|
147 + 0.14128 * cos(4 * M_PI * i / n)
|
c@382
|
148 - 0.01168 * cos(6 * M_PI * i / n));
|
c@382
|
149 }
|
cannam@483
|
150 }
|
cannam@483
|
151 break;
|
c@225
|
152 }
|
cannam@483
|
153
|
c@225
|
154 m_cache = mult;
|
c@225
|
155 }
|
c@225
|
156
|
c@225
|
157 #endif
|