annotate base/Window.h @ 0:da6937383da8

initial import
author Chris Cannam
date Tue, 10 Jan 2006 16:33:16 +0000
parents
children d86891498eef
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 A waveform viewer and audio annotation editor.
Chris@0 5 Chris Cannam, Queen Mary University of London, 2005
Chris@0 6
Chris@0 7 This is experimental software. Not for distribution.
Chris@0 8 */
Chris@0 9
Chris@0 10 #ifndef _WINDOW_H_
Chris@0 11 #define _WINDOW_H_
Chris@0 12
Chris@0 13 #include <cmath>
Chris@0 14 #include <iostream>
Chris@0 15 #include <map>
Chris@0 16
Chris@0 17 enum WindowType {
Chris@0 18 RectangularWindow,
Chris@0 19 BartlettWindow,
Chris@0 20 HammingWindow,
Chris@0 21 HanningWindow,
Chris@0 22 BlackmanWindow,
Chris@0 23 GaussianWindow,
Chris@0 24 ParzenWindow
Chris@0 25 };
Chris@0 26
Chris@0 27 template <typename T>
Chris@0 28 class Window
Chris@0 29 {
Chris@0 30 public:
Chris@0 31 /**
Chris@0 32 * Construct a windower of the given type.
Chris@0 33 */
Chris@0 34 Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); }
Chris@0 35 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
Chris@0 36 Window &operator=(const Window &w) {
Chris@0 37 if (&w == this) return;
Chris@0 38 m_type = w.m_type;
Chris@0 39 m_size = w.m_size;
Chris@0 40 encache();
Chris@0 41 return *this;
Chris@0 42 }
Chris@0 43 virtual ~Window() { delete m_cache; }
Chris@0 44
Chris@0 45 void cut(T *src) const { cut(src, src); }
Chris@0 46 void cut(T *src, T *dst) const {
Chris@0 47 for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
Chris@0 48 }
Chris@0 49
Chris@0 50 WindowType getType() const { return m_type; }
Chris@0 51 size_t getSize() const { return m_size; }
Chris@0 52
Chris@0 53 protected:
Chris@0 54 WindowType m_type;
Chris@0 55 size_t m_size;
Chris@0 56 T *m_cache;
Chris@0 57
Chris@0 58 void encache();
Chris@0 59 };
Chris@0 60
Chris@0 61 template <typename T>
Chris@0 62 void Window<T>::encache()
Chris@0 63 {
Chris@0 64 size_t n = m_size;
Chris@0 65 T *mult = new T[n];
Chris@0 66 size_t i;
Chris@0 67 for (i = 0; i < n; ++i) mult[i] = 1.0;
Chris@0 68
Chris@0 69 switch (m_type) {
Chris@0 70
Chris@0 71 case RectangularWindow:
Chris@0 72 for (i = 0; i < n; ++i) {
Chris@0 73 mult[i] = mult[i] * 0.5;
Chris@0 74 }
Chris@0 75 break;
Chris@0 76
Chris@0 77 case BartlettWindow:
Chris@0 78 for (i = 0; i < n/2; ++i) {
Chris@0 79 mult[i] = mult[i] * (i / T(n/2));
Chris@0 80 mult[i + n/2] = mult[i + n/2] * (1.0 - (i / T(n/2)));
Chris@0 81 }
Chris@0 82 break;
Chris@0 83
Chris@0 84 case HammingWindow:
Chris@0 85 for (i = 0; i < n; ++i) {
Chris@0 86 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
Chris@0 87 }
Chris@0 88 break;
Chris@0 89
Chris@0 90 case HanningWindow:
Chris@0 91 for (i = 0; i < n; ++i) {
Chris@0 92 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
Chris@0 93 }
Chris@0 94 break;
Chris@0 95
Chris@0 96 case BlackmanWindow:
Chris@0 97 for (i = 0; i < n; ++i) {
Chris@0 98 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
Chris@0 99 + 0.08 * cos(4 * M_PI * i / n));
Chris@0 100 }
Chris@0 101 break;
Chris@0 102
Chris@0 103 case GaussianWindow:
Chris@0 104 for (i = 0; i < n; ++i) {
Chris@0 105 mult[i] = mult[i] * exp((-1.0 / (n*n)) * ((T(2*i) - n) *
Chris@0 106 (T(2*i) - n)));
Chris@0 107 }
Chris@0 108 break;
Chris@0 109
Chris@0 110 case ParzenWindow:
Chris@0 111 for (i = 0; i < n; ++i) {
Chris@0 112 mult[i] = mult[i] * (1.0 - fabs((T(2*i) - n) / T(n + 1)));
Chris@0 113 }
Chris@0 114 break;
Chris@0 115 }
Chris@0 116
Chris@0 117 m_cache = mult;
Chris@0 118 }
Chris@0 119
Chris@0 120 #endif