c@225: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@225: c@225: /* c@225: QM DSP library c@225: Centre for Digital Music, Queen Mary, University of London. c@225: This file Copyright 2006 Chris Cannam. c@309: c@309: This program is free software; you can redistribute it and/or c@309: modify it under the terms of the GNU General Public License as c@309: published by the Free Software Foundation; either version 2 of the c@309: License, or (at your option) any later version. See the file c@309: COPYING included with this distribution for more information. c@225: */ c@225: cannam@489: #ifndef QM_DSP_WINDOW_H cannam@489: #define QM_DSP_WINDOW_H c@225: c@225: #include c@225: #include c@225: #include c@382: #include c@225: c@225: enum WindowType { c@225: RectangularWindow, c@225: BartlettWindow, c@225: HammingWindow, c@225: HanningWindow, c@225: BlackmanWindow, c@382: BlackmanHarrisWindow, c@334: c@334: FirstWindow = RectangularWindow, c@382: LastWindow = BlackmanHarrisWindow c@225: }; c@225: c@377: /** c@377: * Various shaped windows for sample frame conditioning, including c@377: * cosine windows (Hann etc) and triangular and rectangular windows. c@377: */ c@225: template c@225: class Window c@225: { c@225: public: c@225: /** c@334: * Construct a windower of the given type and size. c@334: * c@334: * Note that the cosine windows are periodic by design, rather c@334: * than symmetrical. (A window of size N is equivalent to a c@334: * symmetrical window of size N+1 with the final element missing.) c@225: */ c@382: Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); } c@225: Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); } c@225: Window &operator=(const Window &w) { cannam@483: if (&w == this) return *this; cannam@483: m_type = w.m_type; cannam@483: m_size = w.m_size; cannam@483: encache(); cannam@483: return *this; c@225: } c@251: virtual ~Window() { delete[] m_cache; } c@225: c@225: void cut(T *src) const { cut(src, src); } c@280: void cut(const T *src, T *dst) const { cannam@494: for (int i = 0; i < m_size; ++i) { cannam@494: dst[i] = src[i] * m_cache[i]; cannam@494: } c@225: } c@225: c@225: WindowType getType() const { return m_type; } c@382: int getSize() const { return m_size; } c@382: c@382: std::vector getWindowData() const { c@382: std::vector d; c@382: for (int i = 0; i < m_size; ++i) { c@382: d.push_back(m_cache[i]); c@382: } c@382: return d; c@382: } c@225: c@225: protected: c@225: WindowType m_type; c@382: int m_size; c@225: T *m_cache; c@225: c@225: void encache(); c@225: }; c@225: c@225: template c@225: void Window::encache() c@225: { c@382: int n = m_size; c@225: T *mult = new T[n]; c@382: int i; c@225: for (i = 0; i < n; ++i) mult[i] = 1.0; c@225: c@225: switch (m_type) { cannam@483: c@225: case RectangularWindow: c@334: for (i = 0; i < n; ++i) { c@334: mult[i] = mult[i] * 0.5; cannam@483: } cannam@483: break; cannam@483: c@225: case BartlettWindow: c@334: if (n == 2) { c@334: mult[0] = mult[1] = 0; // "matlab compatible" c@334: } else if (n == 3) { c@334: mult[0] = 0; c@334: mult[1] = mult[2] = 2./3.; c@334: } else if (n > 3) { c@334: for (i = 0; i < n/2; ++i) { c@334: mult[i] = mult[i] * (i / T(n/2)); c@334: mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2))); c@334: } cannam@483: } cannam@483: break; cannam@483: c@225: case HammingWindow: c@334: if (n > 1) { c@334: for (i = 0; i < n; ++i) { c@334: mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n)); c@334: } cannam@483: } cannam@483: break; cannam@483: c@225: case HanningWindow: c@334: if (n > 1) { c@334: for (i = 0; i < n; ++i) { c@334: mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n)); c@334: } cannam@483: } cannam@483: break; cannam@483: c@225: case BlackmanWindow: c@334: if (n > 1) { c@334: for (i = 0; i < n; ++i) { c@334: mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n) c@334: + 0.08 * cos(4 * M_PI * i / n)); c@334: } cannam@483: } cannam@483: break; cannam@483: c@382: case BlackmanHarrisWindow: c@382: if (n > 1) { c@382: for (i = 0; i < n; ++i) { c@382: mult[i] = mult[i] * (0.35875 c@382: - 0.48829 * cos(2 * M_PI * i / n) c@382: + 0.14128 * cos(4 * M_PI * i / n) c@382: - 0.01168 * cos(6 * M_PI * i / n)); c@382: } cannam@483: } cannam@483: break; c@225: } cannam@483: c@225: m_cache = mult; c@225: } c@225: c@225: #endif