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
|
c@225
|
15 #ifndef _WINDOW_H_
|
c@225
|
16 #define _WINDOW_H_
|
c@225
|
17
|
c@225
|
18 #include <cmath>
|
c@225
|
19 #include <iostream>
|
c@225
|
20 #include <map>
|
c@225
|
21
|
c@225
|
22 enum WindowType {
|
c@225
|
23 RectangularWindow,
|
c@225
|
24 BartlettWindow,
|
c@225
|
25 HammingWindow,
|
c@225
|
26 HanningWindow,
|
c@225
|
27 BlackmanWindow,
|
c@334
|
28
|
c@334
|
29 FirstWindow = RectangularWindow,
|
c@334
|
30 LastWindow = BlackmanWindow
|
c@225
|
31 };
|
c@225
|
32
|
c@225
|
33 template <typename T>
|
c@225
|
34 class Window
|
c@225
|
35 {
|
c@225
|
36 public:
|
c@225
|
37 /**
|
c@334
|
38 * Construct a windower of the given type and size.
|
c@334
|
39 *
|
c@334
|
40 * Note that the cosine windows are periodic by design, rather
|
c@334
|
41 * than symmetrical. (A window of size N is equivalent to a
|
c@334
|
42 * symmetrical window of size N+1 with the final element missing.)
|
c@225
|
43 */
|
c@225
|
44 Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); }
|
c@225
|
45 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
|
c@225
|
46 Window &operator=(const Window &w) {
|
c@225
|
47 if (&w == this) return *this;
|
c@225
|
48 m_type = w.m_type;
|
c@225
|
49 m_size = w.m_size;
|
c@225
|
50 encache();
|
c@225
|
51 return *this;
|
c@225
|
52 }
|
c@251
|
53 virtual ~Window() { delete[] m_cache; }
|
c@225
|
54
|
c@225
|
55 void cut(T *src) const { cut(src, src); }
|
c@280
|
56 void cut(const T *src, T *dst) const {
|
c@225
|
57 for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
|
c@225
|
58 }
|
c@225
|
59
|
c@225
|
60 WindowType getType() const { return m_type; }
|
c@225
|
61 size_t getSize() const { return m_size; }
|
c@225
|
62
|
c@225
|
63 protected:
|
c@225
|
64 WindowType m_type;
|
c@225
|
65 size_t m_size;
|
c@225
|
66 T *m_cache;
|
c@225
|
67
|
c@225
|
68 void encache();
|
c@225
|
69 };
|
c@225
|
70
|
c@225
|
71 template <typename T>
|
c@225
|
72 void Window<T>::encache()
|
c@225
|
73 {
|
c@225
|
74 size_t n = m_size;
|
c@225
|
75 T *mult = new T[n];
|
c@225
|
76 size_t i;
|
c@225
|
77 for (i = 0; i < n; ++i) mult[i] = 1.0;
|
c@225
|
78
|
c@225
|
79 switch (m_type) {
|
c@225
|
80
|
c@225
|
81 case RectangularWindow:
|
c@334
|
82 for (i = 0; i < n; ++i) {
|
c@334
|
83 mult[i] = mult[i] * 0.5;
|
c@225
|
84 }
|
c@225
|
85 break;
|
c@225
|
86
|
c@225
|
87 case BartlettWindow:
|
c@334
|
88 if (n == 2) {
|
c@334
|
89 mult[0] = mult[1] = 0; // "matlab compatible"
|
c@334
|
90 } else if (n == 3) {
|
c@334
|
91 mult[0] = 0;
|
c@334
|
92 mult[1] = mult[2] = 2./3.;
|
c@334
|
93 } else if (n > 3) {
|
c@334
|
94 for (i = 0; i < n/2; ++i) {
|
c@334
|
95 mult[i] = mult[i] * (i / T(n/2));
|
c@334
|
96 mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2)));
|
c@334
|
97 }
|
c@225
|
98 }
|
c@225
|
99 break;
|
c@225
|
100
|
c@225
|
101 case HammingWindow:
|
c@334
|
102 if (n > 1) {
|
c@334
|
103 for (i = 0; i < n; ++i) {
|
c@334
|
104 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
|
c@334
|
105 }
|
c@225
|
106 }
|
c@225
|
107 break;
|
c@225
|
108
|
c@225
|
109 case HanningWindow:
|
c@334
|
110 if (n > 1) {
|
c@334
|
111 for (i = 0; i < n; ++i) {
|
c@334
|
112 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
|
c@334
|
113 }
|
c@225
|
114 }
|
c@225
|
115 break;
|
c@225
|
116
|
c@225
|
117 case BlackmanWindow:
|
c@334
|
118 if (n > 1) {
|
c@334
|
119 for (i = 0; i < n; ++i) {
|
c@334
|
120 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
|
c@334
|
121 + 0.08 * cos(4 * M_PI * i / n));
|
c@334
|
122 }
|
c@225
|
123 }
|
c@225
|
124 break;
|
c@225
|
125 }
|
c@334
|
126
|
c@225
|
127 m_cache = mult;
|
c@225
|
128 }
|
c@225
|
129
|
c@225
|
130 #endif
|