comparison src/scalar.c @ 184:95af6c8dc7f2

Optimise xtract_smoothness() by removing call to malloc(). Also fix bug in xtract_smoothness() where *result was uninitialised but used in calculation.
author Jamie Bullock <jamie@jamiebullock.com>
date Mon, 08 Jul 2013 10:14:42 +0100
parents ff5f5b77bf3f
children bb60691d9570
comparison
equal deleted inserted replaced
183:63ec44da1666 184:95af6c8dc7f2
395 } 395 }
396 396
397 int xtract_smoothness(const double *data, const int N, const void *argv, double *result) 397 int xtract_smoothness(const double *data, const int N, const void *argv, double *result)
398 { 398 {
399 399
400 int n, M; 400 int n;
401 401 int M = N - 1;
402 double *input; 402 double prev = 0.0;
403 403 double current = 0.0;
404 input = (double *)malloc(N * sizeof(double)); 404 double next = 0.0;
405 memcpy(input, data, N * sizeof(double)); 405 double temp = 0.0;
406 406
407 if (input[0] <= 0) 407
408 input[0] = XTRACT_LOG_LIMIT;
409 if (input[1] <= 0)
410 input[1] = XTRACT_LOG_LIMIT;
411
412 M = N - 1;
413 408
414 for(n = 1; n < M; n++) 409 for(n = 1; n < M; n++)
415 { 410 {
416 if(input[n+1] <= 0) 411 if(n == 1)
417 input[n+1] = XTRACT_LOG_LIMIT; 412 {
418 *result += fabs(20.0 * log(input[n]) - (20.0 * log(input[n-1]) + 413 prev = data[n-1] <= 0 ? XTRACT_LOG_LIMIT : data[n-1];
419 20.0 * log(input[n]) + 20.0 * log(input[n+1])) / 3.0); 414 current = data[n] <= 0 ? XTRACT_LOG_LIMIT : data[n];
420 } 415 }
421 416 else
422 free(input); 417 {
418 prev = current;
419 current = next;
420 }
421
422 next = data[n+1] <= 0 ? XTRACT_LOG_LIMIT : data[n+1];
423
424 temp += fabs(20.0 * log(current) - (20.0 * log(prev) +
425 20.0 * log(current) + 20.0 * log(next)) / 3.0);
426 }
427
428 *result = temp;
423 429
424 return XTRACT_SUCCESS; 430 return XTRACT_SUCCESS;
425 } 431 }
426 432
427 int xtract_spread(const double *data, const int N, const void *argv, double *result) 433 int xtract_spread(const double *data, const int N, const void *argv, double *result)