Mercurial > hg > libxtract
comparison src/window.c @ 107:3e648eec95cb
- Added new helper functions: xtract_windowed() and xtract_features_from_subframes()
- Added windowing functions (window.c)
author | Jamie Bullock <jamie@postlude.co.uk> |
---|---|
date | Fri, 28 Dec 2007 19:34:51 +0000 |
parents | |
children | 67f6b6e63d45 |
comparison
equal
deleted
inserted
replaced
106:3693573a07fa | 107:3e648eec95cb |
---|---|
1 /* libxtract feature extraction library | |
2 * | |
3 * Copyright (C) 2006 Jamie Bullock | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | |
18 * USA. | |
19 */ | |
20 | |
21 /* window.c: defines window generation functions (formulae courtesy of Wikipedia (http://en.wikipedia.org/wiki/Window_function) */ | |
22 | |
23 #include <math.h> | |
24 | |
25 #include "xtract_window_private.h" | |
26 | |
27 void gauss(float *window, const int N, const float sd){ | |
28 | |
29 int n; | |
30 const float M = N - 1; | |
31 float num, | |
32 den, | |
33 exponent; | |
34 | |
35 for (n = 0; n < N; n++) { | |
36 | |
37 num = n - M / 2.f; | |
38 den = sd * M / 2.f; | |
39 | |
40 exponent = -0.5 * powf(num / den, 2); | |
41 | |
42 window[n] = exp(exponent); | |
43 | |
44 } | |
45 } | |
46 | |
47 void hamming(float *window, const int N){ | |
48 | |
49 int n; | |
50 const float M = N - 1; | |
51 | |
52 for (n = 0; n < N; n++) | |
53 window[n] = 0.53836 - (0.46164 * cosf(2.0 * PI * (float)n / M)); | |
54 | |
55 } | |
56 | |
57 void hann(float *window, const int N){ | |
58 | |
59 int n; | |
60 const float M = N - 1; | |
61 | |
62 for (n = 0; n < N; n++) | |
63 window[n] = 0.5 * (1.0 - cosf(2.0 * PI * (float)n / M)); | |
64 | |
65 } | |
66 | |
67 void bartlett(float *window, const int N){ | |
68 | |
69 int n; | |
70 const float M = N - 1; | |
71 | |
72 for (n = 0; n < N; n++) | |
73 window[n] = 2.f / M * (M / 2.f - fabsf(n - M / 2.f)); | |
74 | |
75 } | |
76 | |
77 void triangular(float *window, const int N){ | |
78 | |
79 int n; | |
80 const float M = N - 1; | |
81 | |
82 for (n = 0; n < N; n++) | |
83 window[n] = 2.f / N * (N / 2.f - fabsf(n - M / 2.f)); | |
84 } | |
85 | |
86 void bartlett_hann(float *window, const int N){ | |
87 | |
88 int n; | |
89 const float M = N - 1, | |
90 a0 = 0.62, | |
91 a1 = 0.5, | |
92 a2 = 0.38; | |
93 float term1 = 0.f, | |
94 term2 = 0.f; | |
95 | |
96 for (n = 0; n < N; n++){ | |
97 | |
98 term1 = a1 * fabsf(n / M - 0.5); | |
99 term2 = a2 * cosf(2.0 * PI * (float)n / M); | |
100 | |
101 window[n] = a0 - term1 - term2; | |
102 } | |
103 } | |
104 | |
105 void blackman(float *window, const int N){ | |
106 | |
107 int n; | |
108 const float M = N - 1, | |
109 a0 = 0.42, | |
110 a1 = 0.5, | |
111 a2 = 0.08; | |
112 float term1 = 0.f, | |
113 term2 = 0.f; | |
114 | |
115 for (n = 0; n < N; n++) { | |
116 | |
117 term1 = a1 * cosf(2.0 * PI * (float)n / M); | |
118 term2 = a2 * cosf(4.0 * PI * (float)n / M); | |
119 | |
120 window[n] = a0 - term1 + term2; | |
121 } | |
122 } | |
123 | |
124 #define BIZ_EPSILON 1E-21 // Max error acceptable | |
125 | |
126 /* Based on code from mplayer window.c, and somewhat beyond me */ | |
127 float besselI0(float x){ | |
128 | |
129 float temp; | |
130 float sum = 1.0; | |
131 float u = 1.0; | |
132 float halfx = x/2.0; | |
133 int n = 1; | |
134 | |
135 do { | |
136 | |
137 temp = halfx/(float)n; | |
138 u *=temp * temp; | |
139 sum += u; | |
140 n++; | |
141 | |
142 } while (u >= BIZ_EPSILON * sum); | |
143 | |
144 return(sum); | |
145 | |
146 } | |
147 | |
148 void kaiser(float *window, const int N, const float alpha){ | |
149 | |
150 int n; | |
151 const float M = N - 1; | |
152 float num; | |
153 | |
154 for (n = 0; n < N; n++) { | |
155 | |
156 num = besselI0(alpha * sqrtf(1.0 - powf((2.0 * n / M - 1), 2))); | |
157 window[n] = num / besselI0(alpha); | |
158 | |
159 } | |
160 } | |
161 | |
162 void blackman_harris(float *window, const int N){ | |
163 | |
164 int n; | |
165 const float M = N - 1, | |
166 a0 = 0.35875, | |
167 a1 = 0.48829, | |
168 a2 = 0.14128, | |
169 a3 = 0.01168; | |
170 float term1 = 0.f, | |
171 term2 = 0.f, | |
172 term3 = 0.f; | |
173 | |
174 for (n = 0; n < N; n++) { | |
175 | |
176 term1 = a1 * cosf(2.0 * PI * n / M); | |
177 term2 = a2 * cosf(4.0 * PI * n / M); | |
178 term3 = a3 * cosf(6.0 * PI * n / M); | |
179 | |
180 window[n] = a0 - term1 + term2 - term3; | |
181 } | |
182 } |