changeset 4625:d6d16b5ab2f0

New class for average calculation implementing moving average algorithms.
author Raimund Renkert <rrenkert@intevation.de>
date Mon, 03 Dec 2012 17:08:39 +0100
parents bb267a0aa8c2
children 5b551e3a58d5
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/MovingAverage.java
diffstat 1 files changed, 59 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/MovingAverage.java	Mon Dec 03 17:08:39 2012 +0100
@@ -0,0 +1,59 @@
+package de.intevation.flys.artifacts.model;
+
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+
+public class MovingAverage
+{
+
+    public static double[][] simple(double[][] values, double radius) {
+        TreeMap<Double, Double> map = toMap(values);
+        double[][] result = new double[values.length][values[0].length];
+        int ndx = 0;
+        for (double x: map.keySet()) {
+            SortedMap<Double, Double> range =
+                map.subMap(x-radius, true, x+radius, true);
+            double avg = 0d;
+            for (Double v: range.values()) {
+                avg += v;
+            }
+            avg /= range.size();
+            result[0][ndx] = x;
+            result[1][ndx] = avg;
+            ndx++;
+        }
+        return result;
+    }
+
+    public static double[][] weighted(double[][] values, double radius) {
+        TreeMap<Double, Double> map = toMap(values);
+        double[][] result = new double[values.length][values[0].length];
+        int ndx = 0;
+        for (double x: map.keySet()) {
+            double avg = 0d;
+            double weights = 0d;
+            for (Map.Entry<Double, Double> e:
+                map.subMap(x-radius, false, x+radius, false).entrySet()
+            ) {
+                double weight = 1d - Math.abs(x - e.getKey())/radius;
+                avg += weight*e.getValue();
+                weights += weight;
+            }
+            avg /= weights;
+            result[0][ndx] = x;
+            result[1][ndx] = avg;
+            ndx++;
+        }
+        return result;
+    }
+
+    private static TreeMap<Double, Double> toMap(double[][] values) {
+        TreeMap<Double, Double> map = new TreeMap<Double, Double>();
+        for(int i = 0; i < values[0].length; i++) {
+            map.put(values[0][i], values[1][i]);
+        }
+        return map;
+    }
+}

http://dive4elements.wald.intevation.org