annotate base/Window.h @ 132:06aba53ea0cf

* Update FeatureExtractionPluginTransform to use FFT fuzzy server. The results seem to contain a lot of gaps -- investigate (with chromagram plugin)
author Chris Cannam
date Fri, 30 Jun 2006 11:47:52 +0000
parents 534373d65f39
children a35098a9c814
rev   line source
Chris@49 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@52 4 Sonic Visualiser
Chris@52 5 An audio file viewer and annotation editor.
Chris@52 6 Centre for Digital Music, Queen Mary, University of London.
Chris@52 7 This file copyright 2006 Chris Cannam.
Chris@0 8
Chris@52 9 This program is free software; you can redistribute it and/or
Chris@52 10 modify it under the terms of the GNU General Public License as
Chris@52 11 published by the Free Software Foundation; either version 2 of the
Chris@52 12 License, or (at your option) any later version. See the file
Chris@52 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #ifndef _WINDOW_H_
Chris@0 17 #define _WINDOW_H_
Chris@0 18
Chris@0 19 #include <cmath>
Chris@0 20 #include <iostream>
Chris@0 21 #include <map>
Chris@0 22
Chris@0 23 enum WindowType {
Chris@0 24 RectangularWindow,
Chris@0 25 BartlettWindow,
Chris@0 26 HammingWindow,
Chris@0 27 HanningWindow,
Chris@0 28 BlackmanWindow,
Chris@0 29 GaussianWindow,
Chris@0 30 ParzenWindow
Chris@0 31 };
Chris@0 32
Chris@0 33 template <typename T>
Chris@0 34 class Window
Chris@0 35 {
Chris@0 36 public:
Chris@0 37 /**
Chris@0 38 * Construct a windower of the given type.
Chris@0 39 */
Chris@0 40 Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); }
Chris@0 41 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
Chris@0 42 Window &operator=(const Window &w) {
Chris@52 43 if (&w == this) return *this;
Chris@0 44 m_type = w.m_type;
Chris@0 45 m_size = w.m_size;
Chris@0 46 encache();
Chris@0 47 return *this;
Chris@0 48 }
Chris@0 49 virtual ~Window() { delete m_cache; }
Chris@0 50
Chris@0 51 void cut(T *src) const { cut(src, src); }
Chris@0 52 void cut(T *src, T *dst) const {
Chris@0 53 for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
Chris@0 54 }
Chris@0 55
Chris@0 56 WindowType getType() const { return m_type; }
Chris@0 57 size_t getSize() const { return m_size; }
Chris@0 58
Chris@0 59 protected:
Chris@0 60 WindowType m_type;
Chris@0 61 size_t m_size;
Chris@0 62 T *m_cache;
Chris@0 63
Chris@0 64 void encache();
Chris@0 65 };
Chris@0 66
Chris@0 67 template <typename T>
Chris@0 68 void Window<T>::encache()
Chris@0 69 {
Chris@0 70 size_t n = m_size;
Chris@0 71 T *mult = new T[n];
Chris@0 72 size_t i;
Chris@0 73 for (i = 0; i < n; ++i) mult[i] = 1.0;
Chris@0 74
Chris@0 75 switch (m_type) {
Chris@0 76
Chris@0 77 case RectangularWindow:
Chris@0 78 for (i = 0; i < n; ++i) {
Chris@0 79 mult[i] = mult[i] * 0.5;
Chris@0 80 }
Chris@0 81 break;
Chris@0 82
Chris@0 83 case BartlettWindow:
Chris@0 84 for (i = 0; i < n/2; ++i) {
Chris@0 85 mult[i] = mult[i] * (i / T(n/2));
Chris@0 86 mult[i + n/2] = mult[i + n/2] * (1.0 - (i / T(n/2)));
Chris@0 87 }
Chris@0 88 break;
Chris@0 89
Chris@0 90 case HammingWindow:
Chris@0 91 for (i = 0; i < n; ++i) {
Chris@0 92 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
Chris@0 93 }
Chris@0 94 break;
Chris@0 95
Chris@0 96 case HanningWindow:
Chris@0 97 for (i = 0; i < n; ++i) {
Chris@0 98 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
Chris@0 99 }
Chris@0 100 break;
Chris@0 101
Chris@0 102 case BlackmanWindow:
Chris@0 103 for (i = 0; i < n; ++i) {
Chris@0 104 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
Chris@0 105 + 0.08 * cos(4 * M_PI * i / n));
Chris@0 106 }
Chris@0 107 break;
Chris@0 108
Chris@0 109 case GaussianWindow:
Chris@0 110 for (i = 0; i < n; ++i) {
Chris@113 111 mult[i] = mult[i] * exp(-(pow(i - (n-1)/2.0, 2) /
Chris@113 112 (2 * pow(0.25 * n, 2)))); // sd = 0.25
Chris@0 113 }
Chris@0 114 break;
Chris@0 115
Chris@0 116 case ParzenWindow:
Chris@0 117 for (i = 0; i < n; ++i) {
Chris@0 118 mult[i] = mult[i] * (1.0 - fabs((T(2*i) - n) / T(n + 1)));
Chris@0 119 }
Chris@0 120 break;
Chris@0 121 }
Chris@0 122
Chris@0 123 m_cache = mult;
Chris@0 124 }
Chris@0 125
Chris@0 126 #endif