Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/MovingAverage.java @ 4631:d35d316049e8
A little optimization and handle the case that there are duplicates in the values.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Mon, 03 Dec 2012 21:16:15 +0100 |
parents | 63368dcc3f94 |
children | fb9892036bd6 |
comparison
equal
deleted
inserted
replaced
4630:63368dcc3f94 | 4631:d35d316049e8 |
---|---|
8 public class MovingAverage | 8 public class MovingAverage |
9 { | 9 { |
10 | 10 |
11 public static double[][] simple(double[][] values, double radius) { | 11 public static double[][] simple(double[][] values, double radius) { |
12 TreeMap<Double, Double> map = toMap(values); | 12 TreeMap<Double, Double> map = toMap(values); |
13 double[][] result = new double[values.length][values[0].length]; | 13 int N = map.size(); |
14 double [] xs = new double[N]; | |
15 double [] ys = new double[N]; | |
14 int ndx = 0; | 16 int ndx = 0; |
15 for (double x: map.keySet()) { | 17 for (double x: map.keySet()) { |
16 SortedMap<Double, Double> range = | 18 SortedMap<Double, Double> range = |
17 map.subMap(x-radius, true, x+radius, true); | 19 map.subMap(x-radius, true, x+radius, true); |
18 double avg = 0d; | 20 double avg = 0d; |
19 for (Double v: range.values()) { | 21 for (double v: range.values()) { |
20 avg += v; | 22 avg += v; |
21 } | 23 } |
22 avg /= range.size(); | 24 avg /= range.size(); |
23 result[0][ndx] = x; | 25 xs[ndx] = x; |
24 result[1][ndx] = avg; | 26 ys[ndx] = avg; |
25 ndx++; | 27 ndx++; |
26 } | 28 } |
27 return result; | 29 return new double [][] { xs, ys }; |
28 } | 30 } |
29 | 31 |
30 public static double[][] weighted(double[][] values, double radius) { | 32 public static double[][] weighted(double[][] values, double radius) { |
31 TreeMap<Double, Double> map = toMap(values); | 33 TreeMap<Double, Double> map = toMap(values); |
32 double[][] result = new double[values.length][values[0].length]; | 34 int N = map.size(); |
35 double [] xs = new double[N]; | |
36 double [] ys = new double[N]; | |
33 int ndx = 0; | 37 int ndx = 0; |
38 double _1radius = 1d/radius; | |
34 for (double x: map.keySet()) { | 39 for (double x: map.keySet()) { |
35 double avg = 0d; | 40 double avg = 0d; |
36 double weights = 0d; | 41 double weights = 0d; |
37 for (Map.Entry<Double, Double> e: | 42 for (Map.Entry<Double, Double> e: |
38 map.subMap(x-radius, false, x+radius, false).entrySet() | 43 map.subMap(x-radius, false, x+radius, false).entrySet() |
39 ) { | 44 ) { |
40 double weight = 1d - Math.abs(x - e.getKey())/radius; | 45 double weight = 1d - Math.abs(x - e.getKey())*_1radius; |
41 avg += weight*e.getValue(); | 46 avg += weight*e.getValue(); |
42 weights += weight; | 47 weights += weight; |
43 } | 48 } |
44 avg /= weights; | 49 avg /= weights; |
45 result[0][ndx] = x; | 50 xs[ndx] = x; |
46 result[1][ndx] = avg; | 51 ys[ndx] = avg; |
47 ndx++; | 52 ndx++; |
48 } | 53 } |
49 return result; | 54 return new double [][] { xs, ys }; |
50 } | 55 } |
51 | 56 |
52 private static TreeMap<Double, Double> toMap(double[][] values) { | 57 private static TreeMap<Double, Double> toMap(double[][] values) { |
53 TreeMap<Double, Double> map = new TreeMap<Double, Double>(); | 58 TreeMap<Double, Double> map = new TreeMap<Double, Double>(); |
54 for(int i = 0; i < values[0].length; i++) { | 59 double [] xs = values[0]; |
55 map.put(values[0][i], values[1][i]); | 60 double [] ys = values[1]; |
61 for (int i = 0; i < xs.length; i++) { | |
62 map.put(xs[i], ys[i]); | |
56 } | 63 } |
57 return map; | 64 return map; |
58 } | 65 } |
59 } | 66 } |