annotate constant-q-cpp/src/dsp/Window.h @ 372:af71cbdab621 tip

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