diff options
author | Jamie Bullock <jamie@jamiebullock.com> | 2013-07-08 10:14:42 +0100 |
---|---|---|
committer | Jamie Bullock <jamie@jamiebullock.com> | 2013-07-08 10:25:50 +0100 |
commit | 6b93bef4c900c77365af7ca418e23b645b748e7b (patch) | |
tree | d88fee0c024ba3ee01fd96aeea578ee1ea959177 /src/scalar.c | |
parent | 6925592a1f77df7f76fff1447b5732d3333a31dc (diff) | |
download | LibXtract-6b93bef4c900c77365af7ca418e23b645b748e7b.tar.gz LibXtract-6b93bef4c900c77365af7ca418e23b645b748e7b.tar.bz2 LibXtract-6b93bef4c900c77365af7ca418e23b645b748e7b.zip |
Optimise xtract_smoothness() by removing call to malloc(). Also fix bug in xtract_smoothness() where *result was uninitialised but used in calculation.
Diffstat (limited to 'src/scalar.c')
-rw-r--r-- | src/scalar.c | 44 |
1 files changed, 25 insertions, 19 deletions
diff --git a/src/scalar.c b/src/scalar.c index 74e3486..934f14e 100644 --- a/src/scalar.c +++ b/src/scalar.c @@ -397,29 +397,35 @@ int xtract_tristimulus_3(const double *data, const int N, const void *argv, doub int xtract_smoothness(const double *data, const int N, const void *argv, double *result) { - int n, M; - - double *input; - - input = (double *)malloc(N * sizeof(double)); - memcpy(input, data, N * sizeof(double)); - - if (input[0] <= 0) - input[0] = XTRACT_LOG_LIMIT; - if (input[1] <= 0) - input[1] = XTRACT_LOG_LIMIT; + int n; + int M = N - 1; + double prev = 0.0; + double current = 0.0; + double next = 0.0; + double temp = 0.0; - M = N - 1; + for(n = 1; n < M; n++) { - if(input[n+1] <= 0) - input[n+1] = XTRACT_LOG_LIMIT; - *result += fabs(20.0 * log(input[n]) - (20.0 * log(input[n-1]) + - 20.0 * log(input[n]) + 20.0 * log(input[n+1])) / 3.0); - } - - free(input); + if(n == 1) + { + prev = data[n-1] <= 0 ? XTRACT_LOG_LIMIT : data[n-1]; + current = data[n] <= 0 ? XTRACT_LOG_LIMIT : data[n]; + } + else + { + prev = current; + current = next; + } + + next = data[n+1] <= 0 ? XTRACT_LOG_LIMIT : data[n+1]; + + temp += fabs(20.0 * log(current) - (20.0 * log(prev) + + 20.0 * log(current) + 20.0 * log(next)) / 3.0); + } + + *result = temp; return XTRACT_SUCCESS; } |