aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJamie Bullock <jamie@jamiebullock.com>2013-07-08 10:14:42 +0100
committerJamie Bullock <jamie@jamiebullock.com>2013-07-08 10:25:50 +0100
commit6b93bef4c900c77365af7ca418e23b645b748e7b (patch)
treed88fee0c024ba3ee01fd96aeea578ee1ea959177 /src
parent6925592a1f77df7f76fff1447b5732d3333a31dc (diff)
downloadLibXtract-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')
-rw-r--r--src/scalar.c44
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;
}