Mercurial > hg > qm-dsp
comparison base/Window.h @ 334:f8c7ca4e5667
Fix to triangular window; comment periodic nature of windows; remove two window types for which I don't have adequate tests
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Mon, 30 Sep 2013 16:50:27 +0100 |
parents | d5014ab8b0e5 |
children | 0d3b3c66652b |
comparison
equal
deleted
inserted
replaced
333:dc42cb75dd25 | 334:f8c7ca4e5667 |
---|---|
23 RectangularWindow, | 23 RectangularWindow, |
24 BartlettWindow, | 24 BartlettWindow, |
25 HammingWindow, | 25 HammingWindow, |
26 HanningWindow, | 26 HanningWindow, |
27 BlackmanWindow, | 27 BlackmanWindow, |
28 GaussianWindow, | 28 |
29 ParzenWindow | 29 FirstWindow = RectangularWindow, |
30 LastWindow = BlackmanWindow | |
30 }; | 31 }; |
31 | 32 |
32 template <typename T> | 33 template <typename T> |
33 class Window | 34 class Window |
34 { | 35 { |
35 public: | 36 public: |
36 /** | 37 /** |
37 * Construct a windower of the given type. | 38 * Construct a windower of the given type and size. |
39 * | |
40 * Note that the cosine windows are periodic by design, rather | |
41 * than symmetrical. (A window of size N is equivalent to a | |
42 * symmetrical window of size N+1 with the final element missing.) | |
38 */ | 43 */ |
39 Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); } | 44 Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); } |
40 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); } | 45 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); } |
41 Window &operator=(const Window &w) { | 46 Window &operator=(const Window &w) { |
42 if (&w == this) return *this; | 47 if (&w == this) return *this; |
72 for (i = 0; i < n; ++i) mult[i] = 1.0; | 77 for (i = 0; i < n; ++i) mult[i] = 1.0; |
73 | 78 |
74 switch (m_type) { | 79 switch (m_type) { |
75 | 80 |
76 case RectangularWindow: | 81 case RectangularWindow: |
77 for (i = 0; i < n; ++i) { | 82 for (i = 0; i < n; ++i) { |
78 mult[i] = mult[i] * 0.5; | 83 mult[i] = mult[i] * 0.5; |
79 } | 84 } |
80 break; | 85 break; |
81 | 86 |
82 case BartlettWindow: | 87 case BartlettWindow: |
83 for (i = 0; i < n/2; ++i) { | 88 if (n == 2) { |
84 mult[i] = mult[i] * (i / T(n/2)); | 89 mult[0] = mult[1] = 0; // "matlab compatible" |
85 mult[i + n/2] = mult[i + n/2] * (1.0 - (i / T(n/2))); | 90 } else if (n == 3) { |
91 mult[0] = 0; | |
92 mult[1] = mult[2] = 2./3.; | |
93 } else if (n > 3) { | |
94 for (i = 0; i < n/2; ++i) { | |
95 mult[i] = mult[i] * (i / T(n/2)); | |
96 mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2))); | |
97 } | |
86 } | 98 } |
87 break; | 99 break; |
88 | 100 |
89 case HammingWindow: | 101 case HammingWindow: |
90 for (i = 0; i < n; ++i) { | 102 if (n > 1) { |
91 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n)); | 103 for (i = 0; i < n; ++i) { |
104 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n)); | |
105 } | |
92 } | 106 } |
93 break; | 107 break; |
94 | 108 |
95 case HanningWindow: | 109 case HanningWindow: |
96 for (i = 0; i < n; ++i) { | 110 if (n > 1) { |
97 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n)); | 111 for (i = 0; i < n; ++i) { |
112 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n)); | |
113 } | |
98 } | 114 } |
99 break; | 115 break; |
100 | 116 |
101 case BlackmanWindow: | 117 case BlackmanWindow: |
102 for (i = 0; i < n; ++i) { | 118 if (n > 1) { |
103 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n) | 119 for (i = 0; i < n; ++i) { |
104 + 0.08 * cos(4 * M_PI * i / n)); | 120 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n) |
105 } | 121 + 0.08 * cos(4 * M_PI * i / n)); |
106 break; | 122 } |
107 | |
108 case GaussianWindow: | |
109 for (i = 0; i < n; ++i) { | |
110 mult[i] = mult[i] * exp((-1.0 / (n*n)) * ((T(2*i) - n) * | |
111 (T(2*i) - n))); | |
112 } | |
113 break; | |
114 | |
115 case ParzenWindow: | |
116 for (i = 0; i < n; ++i) { | |
117 mult[i] = mult[i] * (1.0 - fabs((T(2*i) - n) / T(n + 1))); | |
118 } | 123 } |
119 break; | 124 break; |
120 } | 125 } |
121 | 126 |
122 m_cache = mult; | 127 m_cache = mult; |
123 } | 128 } |
124 | 129 |
125 #endif | 130 #endif |