[Dive4elements-commits] [PATCH 08 of 11] New class for average calculation implementing moving average algorithms
Wald Commits
scm-commit at wald.intevation.org
Mon Dec 3 17:27:28 CET 2012
# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1354550919 -3600
# Node ID d6d16b5ab2f07cb63651ee2fe3f1b4e1c2d454fb
# Parent bb267a0aa8c215bfd9018f0467ae6e49a601c81f
New class for average calculation implementing moving average algorithms.
diff -r bb267a0aa8c2 -r d6d16b5ab2f0 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/MovingAverage.java
--- /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;
+ }
+}
More information about the Dive4elements-commits
mailing list