annotate src/dsp/Window.h @ 172:76b70af3a563

Merge from branch chroma-in-library
author Chris Cannam <c.cannam@qmul.ac.uk>
date Thu, 05 Feb 2015 09:53:06 +0000
parents 2375457f2876
children 433e3aac9e52
rev   line source
c@121 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@121 2 /*
c@121 3 Constant-Q library
c@121 4 Copyright (c) 2013-2014 Queen Mary, University of London
c@121 5
c@121 6 Permission is hereby granted, free of charge, to any person
c@121 7 obtaining a copy of this software and associated documentation
c@121 8 files (the "Software"), to deal in the Software without
c@121 9 restriction, including without limitation the rights to use, copy,
c@121 10 modify, merge, publish, distribute, sublicense, and/or sell copies
c@121 11 of the Software, and to permit persons to whom the Software is
c@121 12 furnished to do so, subject to the following conditions:
c@121 13
c@121 14 The above copyright notice and this permission notice shall be
c@121 15 included in all copies or substantial portions of the Software.
c@121 16
c@121 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c@121 18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c@121 19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c@121 20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
c@121 21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
c@121 22 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
c@121 23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
c@121 24
c@121 25 Except as contained in this notice, the names of the Centre for
c@121 26 Digital Music; Queen Mary, University of London; and Chris Cannam
c@121 27 shall not be used in advertising or otherwise to promote the sale,
c@121 28 use or other dealings in this Software without prior written
c@121 29 authorization.
c@121 30 */
c@121 31
c@121 32 #ifndef _WINDOW_H_
c@121 33 #define _WINDOW_H_
c@121 34
c@121 35 #include <cmath>
c@121 36 #include <iostream>
c@121 37 #include <map>
c@121 38 #include <vector>
c@121 39
c@121 40 enum WindowType {
c@121 41 RectangularWindow,
c@121 42 BartlettWindow,
c@121 43 HammingWindow,
c@121 44 HanningWindow,
c@121 45 BlackmanWindow,
c@121 46 BlackmanHarrisWindow,
c@121 47
c@121 48 FirstWindow = RectangularWindow,
c@121 49 LastWindow = BlackmanHarrisWindow
c@121 50 };
c@121 51
c@121 52 /**
c@121 53 * Various shaped windows for sample frame conditioning, including
c@121 54 * cosine windows (Hann etc) and triangular and rectangular windows.
c@121 55 */
c@121 56 template <typename T>
c@121 57 class Window
c@121 58 {
c@121 59 public:
c@121 60 /**
c@121 61 * Construct a windower of the given type and size.
c@121 62 *
c@121 63 * Note that the cosine windows are periodic by design, rather
c@121 64 * than symmetrical. (A window of size N is equivalent to a
c@121 65 * symmetrical window of size N+1 with the final element missing.)
c@121 66 */
c@121 67 Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); }
c@121 68 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
c@121 69 Window &operator=(const Window &w) {
c@121 70 if (&w == this) return *this;
c@121 71 m_type = w.m_type;
c@121 72 m_size = w.m_size;
c@121 73 encache();
c@121 74 return *this;
c@121 75 }
c@121 76 virtual ~Window() { delete[] m_cache; }
c@121 77
c@121 78 void cut(T *src) const { cut(src, src); }
c@121 79 void cut(const T *src, T *dst) const {
c@121 80 for (int i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
c@121 81 }
c@121 82
c@121 83 WindowType getType() const { return m_type; }
c@121 84 int getSize() const { return m_size; }
c@121 85
c@121 86 std::vector<T> getWindowData() const {
c@121 87 std::vector<T> d;
c@121 88 for (int i = 0; i < m_size; ++i) {
c@121 89 d.push_back(m_cache[i]);
c@121 90 }
c@121 91 return d;
c@121 92 }
c@121 93
c@121 94 protected:
c@121 95 WindowType m_type;
c@121 96 int m_size;
c@121 97 T *m_cache;
c@121 98
c@121 99 void encache();
c@121 100 };
c@121 101
c@121 102 template <typename T>
c@121 103 void Window<T>::encache()
c@121 104 {
c@121 105 int n = m_size;
c@121 106 T *mult = new T[n];
c@121 107 int i;
c@121 108 for (i = 0; i < n; ++i) mult[i] = 1.0;
c@121 109
c@121 110 switch (m_type) {
c@121 111
c@121 112 case RectangularWindow:
c@121 113 for (i = 0; i < n; ++i) {
c@121 114 mult[i] = mult[i] * 0.5;
c@121 115 }
c@121 116 break;
c@121 117
c@121 118 case BartlettWindow:
c@121 119 if (n == 2) {
c@121 120 mult[0] = mult[1] = 0; // "matlab compatible"
c@121 121 } else if (n == 3) {
c@121 122 mult[0] = 0;
c@121 123 mult[1] = mult[2] = 2./3.;
c@121 124 } else if (n > 3) {
c@121 125 for (i = 0; i < n/2; ++i) {
c@121 126 mult[i] = mult[i] * (i / T(n/2));
c@121 127 mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2)));
c@121 128 }
c@121 129 }
c@121 130 break;
c@121 131
c@121 132 case HammingWindow:
c@121 133 if (n > 1) {
c@121 134 for (i = 0; i < n; ++i) {
c@121 135 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
c@121 136 }
c@121 137 }
c@121 138 break;
c@121 139
c@121 140 case HanningWindow:
c@121 141 if (n > 1) {
c@121 142 for (i = 0; i < n; ++i) {
c@121 143 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
c@121 144 }
c@121 145 }
c@121 146 break;
c@121 147
c@121 148 case BlackmanWindow:
c@121 149 if (n > 1) {
c@121 150 for (i = 0; i < n; ++i) {
c@121 151 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
c@121 152 + 0.08 * cos(4 * M_PI * i / n));
c@121 153 }
c@121 154 }
c@121 155 break;
c@121 156
c@121 157 case BlackmanHarrisWindow:
c@121 158 if (n > 1) {
c@121 159 for (i = 0; i < n; ++i) {
c@121 160 mult[i] = mult[i] * (0.35875
c@121 161 - 0.48829 * cos(2 * M_PI * i / n)
c@121 162 + 0.14128 * cos(4 * M_PI * i / n)
c@121 163 - 0.01168 * cos(6 * M_PI * i / n));
c@121 164 }
c@121 165 }
c@121 166 break;
c@121 167 }
c@121 168
c@121 169 m_cache = mult;
c@121 170 }
c@121 171
c@121 172 #endif