1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
5 Centre for Digital Music, Queen Mary, University of London.
6 This file Copyright 2006 Chris Cannam.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
31 FirstWindow = RectangularWindow,
32 LastWindow = BlackmanHarrisWindow
36 * Various shaped windows for sample frame conditioning, including
37 * cosine windows (Hann etc) and triangular and rectangular windows.
44 * Construct a windower of the given type and size.
46 * Note that the cosine windows are periodic by design, rather
47 * than symmetrical. (A window of size N is equivalent to a
48 * symmetrical window of size N+1 with the final element missing.)
50 Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); }
51 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
52 Window &operator=(const Window &w) {
53 if (&w == this) return *this;
59 virtual ~Window() { delete[] m_cache; }
61 void cut(T *src) const { cut(src, src); }
62 void cut(const T *src, T *dst) const {
63 for (int i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
66 WindowType getType() const { return m_type; }
67 int getSize() const { return m_size; }
69 std::vector<T> getWindowData() const {
71 for (int i = 0; i < m_size; ++i) {
72 d.push_back(m_cache[i]);
86 void Window<T>::encache()
91 for (i = 0; i < n; ++i) mult[i] = 1.0;
95 case RectangularWindow:
96 for (i = 0; i < n; ++i) {
97 mult[i] = mult[i] * 0.5;
103 mult[0] = mult[1] = 0; // "matlab compatible"
106 mult[1] = mult[2] = 2./3.;
108 for (i = 0; i < n/2; ++i) {
109 mult[i] = mult[i] * (i / T(n/2));
110 mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2)));
117 for (i = 0; i < n; ++i) {
118 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
125 for (i = 0; i < n; ++i) {
126 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
133 for (i = 0; i < n; ++i) {
134 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
135 + 0.08 * cos(4 * M_PI * i / n));
140 case BlackmanHarrisWindow:
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));