Mercurial > hg > qm-dsp
diff base/Window.h @ 382:a73d94ee8072
Add Blackman-Harris window
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Fri, 01 Nov 2013 15:31:57 +0000 |
parents | 59b151f13b3e |
children | fdaa63607c15 |
line wrap: on
line diff
--- a/base/Window.h Fri Nov 01 12:07:08 2013 +0000 +++ b/base/Window.h Fri Nov 01 15:31:57 2013 +0000 @@ -18,6 +18,7 @@ #include <cmath> #include <iostream> #include <map> +#include <vector> enum WindowType { RectangularWindow, @@ -25,9 +26,10 @@ HammingWindow, HanningWindow, BlackmanWindow, + BlackmanHarrisWindow, FirstWindow = RectangularWindow, - LastWindow = BlackmanWindow + LastWindow = BlackmanHarrisWindow }; /** @@ -45,7 +47,7 @@ * than symmetrical. (A window of size N is equivalent to a * symmetrical window of size N+1 with the final element missing.) */ - Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); } + Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); } Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); } Window &operator=(const Window &w) { if (&w == this) return *this; @@ -58,15 +60,23 @@ void cut(T *src) const { cut(src, src); } void cut(const T *src, T *dst) const { - for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i]; + for (int i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i]; } WindowType getType() const { return m_type; } - size_t getSize() const { return m_size; } + int getSize() const { return m_size; } + + std::vector<T> getWindowData() const { + std::vector<T> d; + for (int i = 0; i < m_size; ++i) { + d.push_back(m_cache[i]); + } + return d; + } protected: WindowType m_type; - size_t m_size; + int m_size; T *m_cache; void encache(); @@ -75,9 +85,9 @@ template <typename T> void Window<T>::encache() { - size_t n = m_size; + int n = m_size; T *mult = new T[n]; - size_t i; + int i; for (i = 0; i < n; ++i) mult[i] = 1.0; switch (m_type) { @@ -126,6 +136,17 @@ } } break; + + case BlackmanHarrisWindow: + if (n > 1) { + for (i = 0; i < n; ++i) { + mult[i] = mult[i] * (0.35875 + - 0.48829 * cos(2 * M_PI * i / n) + + 0.14128 * cos(4 * M_PI * i / n) + - 0.01168 * cos(6 * M_PI * i / n)); + } + } + break; } m_cache = mult;