Mercurial > hg > qm-dsp
comparison base/Window.h @ 157:152abaf17c62
Add Blackman-Harris window
| author | Chris Cannam |
|---|---|
| date | Fri, 01 Nov 2013 15:31:57 +0000 |
| parents | 0d3b3c66652b |
| children | fdaa63607c15 |
comparison
equal
deleted
inserted
replaced
| 156:edb86e0d850c | 157:152abaf17c62 |
|---|---|
| 16 #define _WINDOW_H_ | 16 #define _WINDOW_H_ |
| 17 | 17 |
| 18 #include <cmath> | 18 #include <cmath> |
| 19 #include <iostream> | 19 #include <iostream> |
| 20 #include <map> | 20 #include <map> |
| 21 #include <vector> | |
| 21 | 22 |
| 22 enum WindowType { | 23 enum WindowType { |
| 23 RectangularWindow, | 24 RectangularWindow, |
| 24 BartlettWindow, | 25 BartlettWindow, |
| 25 HammingWindow, | 26 HammingWindow, |
| 26 HanningWindow, | 27 HanningWindow, |
| 27 BlackmanWindow, | 28 BlackmanWindow, |
| 29 BlackmanHarrisWindow, | |
| 28 | 30 |
| 29 FirstWindow = RectangularWindow, | 31 FirstWindow = RectangularWindow, |
| 30 LastWindow = BlackmanWindow | 32 LastWindow = BlackmanHarrisWindow |
| 31 }; | 33 }; |
| 32 | 34 |
| 33 /** | 35 /** |
| 34 * Various shaped windows for sample frame conditioning, including | 36 * Various shaped windows for sample frame conditioning, including |
| 35 * cosine windows (Hann etc) and triangular and rectangular windows. | 37 * cosine windows (Hann etc) and triangular and rectangular windows. |
| 43 * | 45 * |
| 44 * Note that the cosine windows are periodic by design, rather | 46 * Note that the cosine windows are periodic by design, rather |
| 45 * than symmetrical. (A window of size N is equivalent to a | 47 * than symmetrical. (A window of size N is equivalent to a |
| 46 * symmetrical window of size N+1 with the final element missing.) | 48 * symmetrical window of size N+1 with the final element missing.) |
| 47 */ | 49 */ |
| 48 Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); } | 50 Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); } |
| 49 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); } | 51 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); } |
| 50 Window &operator=(const Window &w) { | 52 Window &operator=(const Window &w) { |
| 51 if (&w == this) return *this; | 53 if (&w == this) return *this; |
| 52 m_type = w.m_type; | 54 m_type = w.m_type; |
| 53 m_size = w.m_size; | 55 m_size = w.m_size; |
| 56 } | 58 } |
| 57 virtual ~Window() { delete[] m_cache; } | 59 virtual ~Window() { delete[] m_cache; } |
| 58 | 60 |
| 59 void cut(T *src) const { cut(src, src); } | 61 void cut(T *src) const { cut(src, src); } |
| 60 void cut(const T *src, T *dst) const { | 62 void cut(const T *src, T *dst) const { |
| 61 for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i]; | 63 for (int i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i]; |
| 62 } | 64 } |
| 63 | 65 |
| 64 WindowType getType() const { return m_type; } | 66 WindowType getType() const { return m_type; } |
| 65 size_t getSize() const { return m_size; } | 67 int getSize() const { return m_size; } |
| 68 | |
| 69 std::vector<T> getWindowData() const { | |
| 70 std::vector<T> d; | |
| 71 for (int i = 0; i < m_size; ++i) { | |
| 72 d.push_back(m_cache[i]); | |
| 73 } | |
| 74 return d; | |
| 75 } | |
| 66 | 76 |
| 67 protected: | 77 protected: |
| 68 WindowType m_type; | 78 WindowType m_type; |
| 69 size_t m_size; | 79 int m_size; |
| 70 T *m_cache; | 80 T *m_cache; |
| 71 | 81 |
| 72 void encache(); | 82 void encache(); |
| 73 }; | 83 }; |
| 74 | 84 |
| 75 template <typename T> | 85 template <typename T> |
| 76 void Window<T>::encache() | 86 void Window<T>::encache() |
| 77 { | 87 { |
| 78 size_t n = m_size; | 88 int n = m_size; |
| 79 T *mult = new T[n]; | 89 T *mult = new T[n]; |
| 80 size_t i; | 90 int i; |
| 81 for (i = 0; i < n; ++i) mult[i] = 1.0; | 91 for (i = 0; i < n; ++i) mult[i] = 1.0; |
| 82 | 92 |
| 83 switch (m_type) { | 93 switch (m_type) { |
| 84 | 94 |
| 85 case RectangularWindow: | 95 case RectangularWindow: |
| 124 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n) | 134 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n) |
| 125 + 0.08 * cos(4 * M_PI * i / n)); | 135 + 0.08 * cos(4 * M_PI * i / n)); |
| 126 } | 136 } |
| 127 } | 137 } |
| 128 break; | 138 break; |
| 139 | |
| 140 case BlackmanHarrisWindow: | |
| 141 if (n > 1) { | |
| 142 for (i = 0; i < n; ++i) { | |
| 143 mult[i] = mult[i] * (0.35875 | |
| 144 - 0.48829 * cos(2 * M_PI * i / n) | |
| 145 + 0.14128 * cos(4 * M_PI * i / n) | |
| 146 - 0.01168 * cos(6 * M_PI * i / n)); | |
| 147 } | |
| 148 } | |
| 149 break; | |
| 129 } | 150 } |
| 130 | 151 |
| 131 m_cache = mult; | 152 m_cache = mult; |
| 132 } | 153 } |
| 133 | 154 |
