comparison src/vector.c @ 49:4e931b464278

Added xtract_sharpness()
author Jamie Bullock <jamie@postlude.co.uk>
date Thu, 21 Dec 2006 13:23:12 +0000
parents c20e91e86f08
children 45c585bb7996
comparison
equal deleted inserted replaced
48:8703c202a247 49:4e931b464278
30 30
31 #include <fftw3.h> 31 #include <fftw3.h>
32 32
33 int xtract_magnitude_spectrum(const float *data, const int N, const void *argv, float *result){ 33 int xtract_magnitude_spectrum(const float *data, const int N, const void *argv, float *result){
34 34
35 float *temp, *input, q; 35 float *temp, *input, q, sr;
36 size_t bytes; 36 size_t bytes;
37 int n , M = N >> 1; 37 int n , M = N >> 1;
38 fftwf_plan plan; 38 fftwf_plan plan;
39 39
40 temp = (float *)fftwf_malloc(N * sizeof(float)); 40 temp = (float *)fftwf_malloc(N * sizeof(float));
41 input = (float *)malloc(bytes = N * sizeof(float)); 41 input = (float *)malloc(bytes = N * sizeof(float));
42 input = memcpy(input, data, bytes); 42 input = memcpy(input, data, bytes);
43 43
44 q = 0.f; 44 q = sr = 0.f;
45 45
46 q = *(float *)argv; 46 sr = *(float *)argv;
47 q = (q ? (q * .5) / M : 0.f); 47
48 CHECK_SR;
49
50 q = (sr * .5) / M;
48 51
49 plan = fftwf_plan_r2r_1d(N, input, temp, FFTW_R2HC, FFTW_ESTIMATE); 52 plan = fftwf_plan_r2r_1d(N, input, temp, FFTW_R2HC, FFTW_ESTIMATE);
50 53
51 fftwf_execute(plan); 54 fftwf_execute(plan);
52 55
240 243
241 float thresh, max, y, y2, 244 float thresh, max, y, y2,
242 y3, p, width, sr, 245 y3, p, width, sr,
243 *input = NULL; 246 *input = NULL;
244 size_t bytes; 247 size_t bytes;
245 int n = N, M, return_code = SUCCESS; 248 int n = N, M, rv = SUCCESS;
249
250 thresh = max = y = y2 = y3 = p = width = sr = 0.f;
246 251
247 if(argv != NULL){ 252 if(argv != NULL){
248 thresh = ((float *)argv)[0]; 253 thresh = ((float *)argv)[0];
249 sr = ((float *)argv)[1]; 254 sr = ((float *)argv)[1];
250 return_code = BAD_ARGV; 255 }
251 } 256 else
252 else{ 257 rv = BAD_ARGV;
253 thresh = 0.f; 258
254 sr = 44100.f; 259 if(thresh < 0 || thresh > 100){
255 } 260 thresh = 0;
261 rv = BAD_ARGV;
262 }
263
264 CHECK_SR;
256 265
257 input = (float *)malloc(bytes = N * sizeof(float)); 266 input = (float *)malloc(bytes = N * sizeof(float));
258 267
259 if(input != NULL) 268 if(input != NULL)
260 input = memcpy(input, data, bytes); 269 input = memcpy(input, data, bytes);
261 else 270 else
262 return MALLOC_FAILED; 271 return MALLOC_FAILED;
263 272
264 M = N >> 1; 273 M = N >> 1;
265 width = sr / N; 274 width = sr / N;
266
267 y = y2 = y3 = p = max = 0.f;
268
269 if(thresh < 0 || thresh > 100){
270 thresh = 0;
271 return_code = BAD_ARGV;
272 }
273
274 if(!sr){
275 sr = 44100.f;
276 return_code = BAD_ARGV;
277 }
278 275
279 while(n--) 276 while(n--)
280 max = MAX(max, input[n]); 277 max = MAX(max, input[n]);
281 278
282 thresh *= .01 * max; 279 thresh *= .01 * max;
300 result[M + n] = 0; 297 result[M + n] = 0;
301 } 298 }
302 } 299 }
303 300
304 free(input); 301 free(input);
305 return (return_code ? return_code : SUCCESS); 302 return (rv ? rv : SUCCESS);
306 } 303 }
307 304
308 int xtract_harmonics(const float *data, const int N, const void *argv, float *result){ 305 int xtract_harmonics(const float *data, const int N, const void *argv, float *result){
309 306
310 int n = (N >> 1), M = n; 307 int n = (N >> 1), M = n;