comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/MovingAverage.java @ 4630:63368dcc3f94

Moved MovingAverage into math package
author Sascha L. Teichmann <teichmann@intevation.de>
date Mon, 03 Dec 2012 18:44:48 +0100
parents flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/MovingAverage.java@d6d16b5ab2f0
children d35d316049e8
comparison
equal deleted inserted replaced
4629:e29f368c09ba 4630:63368dcc3f94
1 package de.intevation.flys.artifacts.math;
2
3 import java.util.Map;
4 import java.util.SortedMap;
5 import java.util.TreeMap;
6
7
8 public class MovingAverage
9 {
10
11 public static double[][] simple(double[][] values, double radius) {
12 TreeMap<Double, Double> map = toMap(values);
13 double[][] result = new double[values.length][values[0].length];
14 int ndx = 0;
15 for (double x: map.keySet()) {
16 SortedMap<Double, Double> range =
17 map.subMap(x-radius, true, x+radius, true);
18 double avg = 0d;
19 for (Double v: range.values()) {
20 avg += v;
21 }
22 avg /= range.size();
23 result[0][ndx] = x;
24 result[1][ndx] = avg;
25 ndx++;
26 }
27 return result;
28 }
29
30 public static double[][] weighted(double[][] values, double radius) {
31 TreeMap<Double, Double> map = toMap(values);
32 double[][] result = new double[values.length][values[0].length];
33 int ndx = 0;
34 for (double x: map.keySet()) {
35 double avg = 0d;
36 double weights = 0d;
37 for (Map.Entry<Double, Double> e:
38 map.subMap(x-radius, false, x+radius, false).entrySet()
39 ) {
40 double weight = 1d - Math.abs(x - e.getKey())/radius;
41 avg += weight*e.getValue();
42 weights += weight;
43 }
44 avg /= weights;
45 result[0][ndx] = x;
46 result[1][ndx] = avg;
47 ndx++;
48 }
49 return result;
50 }
51
52 private static TreeMap<Double, Double> toMap(double[][] values) {
53 TreeMap<Double, Double> map = new TreeMap<Double, Double>();
54 for(int i = 0; i < values[0].length; i++) {
55 map.put(values[0][i], values[1][i]);
56 }
57 return map;
58 }
59 }

http://dive4elements.wald.intevation.org