# HG changeset patch # User Sascha L. Teichmann # Date 1354556688 -3600 # Node ID 63368dcc3f941d46d558fc3da2603ace32298c2b # Parent e29f368c09ba62ab59c73fddf4163b9753be6675 Moved MovingAverage into math package diff -r e29f368c09ba -r 63368dcc3f94 flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/MovingAverage.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/MovingAverage.java Mon Dec 03 18:44:48 2012 +0100 @@ -0,0 +1,59 @@ +package de.intevation.flys.artifacts.math; + +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + + +public class MovingAverage +{ + + public static double[][] simple(double[][] values, double radius) { + TreeMap map = toMap(values); + double[][] result = new double[values.length][values[0].length]; + int ndx = 0; + for (double x: map.keySet()) { + SortedMap 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 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 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 toMap(double[][] values) { + TreeMap map = new TreeMap(); + for(int i = 0; i < values[0].length; i++) { + map.put(values[0][i], values[1][i]); + } + return map; + } +} diff -r e29f368c09ba -r 63368dcc3f94 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FlowVelocityFacet.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FlowVelocityFacet.java Mon Dec 03 17:55:04 2012 +0100 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FlowVelocityFacet.java Mon Dec 03 18:44:48 2012 +0100 @@ -9,11 +9,12 @@ import de.intevation.flys.artifacts.access.RiverAccess; import de.intevation.flys.artifacts.context.FLYSContext; + +import de.intevation.flys.artifacts.math.MovingAverage; import de.intevation.flys.artifacts.states.DefaultState.ComputeType; import org.apache.log4j.Logger; - /** * Facet of a FlowVelocity curve. */ diff -r e29f368c09ba -r 63368dcc3f94 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/MovingAverage.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/MovingAverage.java Mon Dec 03 17:55:04 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -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 map = toMap(values); - double[][] result = new double[values.length][values[0].length]; - int ndx = 0; - for (double x: map.keySet()) { - SortedMap 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 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 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 toMap(double[][] values) { - TreeMap map = new TreeMap(); - for(int i = 0; i < values[0].length; i++) { - map.put(values[0][i], values[1][i]); - } - return map; - } -}