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