comparison base/Window.h @ 0:fc9323a41f5a

start base : Sonic Visualiser sv1-1.0rc1
author lbajardsilogic
date Fri, 11 May 2007 09:08:14 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:fc9323a41f5a
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #ifndef _WINDOW_H_
17 #define _WINDOW_H_
18
19 #include <cmath>
20 #include <iostream>
21 #include <map>
22
23 enum WindowType {
24 RectangularWindow,
25 BartlettWindow,
26 HammingWindow,
27 HanningWindow,
28 BlackmanWindow,
29 GaussianWindow,
30 ParzenWindow,
31 NuttallWindow,
32 BlackmanHarrisWindow
33 };
34
35 template <typename T>
36 class Window
37 {
38 public:
39 /**
40 * Construct a windower of the given type.
41 */
42 Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); }
43 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
44 Window &operator=(const Window &w) {
45 if (&w == this) return *this;
46 m_type = w.m_type;
47 m_size = w.m_size;
48 encache();
49 return *this;
50 }
51 virtual ~Window() { delete[] m_cache; }
52
53 void cut(T *src) const { cut(src, src); }
54 void cut(T *src, T *dst) const {
55 for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
56 }
57
58 T getArea() { return m_area; }
59 T getValue(size_t i) { return m_cache[i]; }
60
61 WindowType getType() const { return m_type; }
62 size_t getSize() const { return m_size; }
63
64 protected:
65 WindowType m_type;
66 size_t m_size;
67 T *m_cache;
68 T m_area;
69
70 void encache();
71 void cosinewin(T *, T, T, T, T);
72 };
73
74 template <typename T>
75 void Window<T>::encache()
76 {
77 int n = int(m_size);
78 T *mult = new T[n];
79 int i;
80 for (i = 0; i < n; ++i) mult[i] = 1.0;
81
82 switch (m_type) {
83
84 case RectangularWindow:
85 for (i = 0; i < n; ++i) {
86 mult[i] *= 0.5;
87 }
88 break;
89
90 case BartlettWindow:
91 for (i = 0; i < n/2; ++i) {
92 mult[i] *= (i / T(n/2));
93 mult[i + n/2] *= (1.0 - (i / T(n/2)));
94 }
95 break;
96
97 case HammingWindow:
98 cosinewin(mult, 0.54, 0.46, 0.0, 0.0);
99 break;
100
101 case HanningWindow:
102 cosinewin(mult, 0.50, 0.50, 0.0, 0.0);
103 break;
104
105 case BlackmanWindow:
106 cosinewin(mult, 0.42, 0.50, 0.08, 0.0);
107 break;
108
109 case GaussianWindow:
110 for (i = 0; i < n; ++i) {
111 mult[i] *= pow(2, - pow((i - (n-1)/2.0) / ((n-1)/2.0 / 3), 2));
112 }
113 break;
114
115 case ParzenWindow:
116 {
117 int N = n-1;
118 for (i = 0; i < N/4; ++i) {
119 T m = 2 * pow(1.0 - (T(N)/2 - i) / (T(N)/2), 3);
120 mult[i] *= m;
121 mult[N-i] *= m;
122 }
123 for (i = N/4; i <= N/2; ++i) {
124 int wn = i - N/2;
125 T m = 1.0 - 6 * pow(wn / (T(N)/2), 2) * (1.0 - abs(wn) / (T(N)/2));
126 mult[i] *= m;
127 mult[N-i] *= m;
128 }
129 break;
130 }
131
132 case NuttallWindow:
133 cosinewin(mult, 0.3635819, 0.4891775, 0.1365995, 0.0106411);
134 break;
135
136 case BlackmanHarrisWindow:
137 cosinewin(mult, 0.35875, 0.48829, 0.14128, 0.01168);
138 break;
139 }
140
141 m_cache = mult;
142
143 m_area = 0;
144 for (int i = 0; i < n; ++i) {
145 m_area += m_cache[i];
146 }
147 m_area /= n;
148 }
149
150 template <typename T>
151 void Window<T>::cosinewin(T *mult, T a0, T a1, T a2, T a3)
152 {
153 int n = int(m_size);
154 for (int i = 0; i < n; ++i) {
155 mult[i] *= (a0
156 - a1 * cos(2 * M_PI * i / n)
157 + a2 * cos(4 * M_PI * i / n)
158 - a3 * cos(6 * M_PI * i / n));
159 }
160 }
161
162 #endif