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: c@225: #ifndef _WINDOW_H_ c@225: #define _WINDOW_H_ c@225: c@225: #include c@225: #include c@225: #include c@225: c@225: enum WindowType { c@225: RectangularWindow, c@225: BartlettWindow, c@225: HammingWindow, c@225: HanningWindow, c@225: BlackmanWindow, c@334: c@334: FirstWindow = RectangularWindow, c@334: LastWindow = BlackmanWindow c@225: }; c@225: 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@225: Window(WindowType type, size_t 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) { c@225: if (&w == this) return *this; c@225: m_type = w.m_type; c@225: m_size = w.m_size; c@225: encache(); c@225: 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 { c@225: for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i]; c@225: } c@225: c@225: WindowType getType() const { return m_type; } c@225: size_t getSize() const { return m_size; } c@225: c@225: protected: c@225: WindowType m_type; c@225: size_t 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@225: size_t n = m_size; c@225: T *mult = new T[n]; c@225: size_t i; c@225: for (i = 0; i < n; ++i) mult[i] = 1.0; c@225: c@225: switch (m_type) { c@225: c@225: case RectangularWindow: c@334: for (i = 0; i < n; ++i) { c@334: mult[i] = mult[i] * 0.5; c@225: } c@225: break; c@225: 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: } c@225: } c@225: break; c@225: 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: } c@225: } c@225: break; c@225: 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: } c@225: } c@225: break; c@225: 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: } c@225: } c@225: break; c@225: } c@334: c@225: m_cache = mult; c@225: } c@225: c@225: #endif