comparison src/window.c @ 140:67f6b6e63d45

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