changeset 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 63ec44da1666
children 03f1045e1911
files src/scalar.c
diffstat 1 files changed, 24 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/scalar.c	Mon Jul 08 09:52:19 2013 +0100
+++ b/src/scalar.c	Mon Jul 08 10:14:42 2013 +0100
@@ -397,29 +397,35 @@
 int xtract_smoothness(const double *data, const int N, const void *argv, double *result)
 {
 
-    int n, M;
+    int n; 
+    int M = N - 1;
+    double prev = 0.0;
+    double current = 0.0;
+    double next = 0.0;
+    double temp = 0.0;
 
-    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;
-
-    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);
+        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);
     }
-
-    free(input);
+    
+    *result = temp;
 
     return XTRACT_SUCCESS;
 }