annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/MovingAverage.java @ 4869:d5f5bdaca316

DatacageWindow: Doc.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Mon, 28 Jan 2013 00:46:19 +0100
parents d35d316049e8
children fb9892036bd6
rev   line source
4630
63368dcc3f94 Moved MovingAverage into math package
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4625
diff changeset
1 package de.intevation.flys.artifacts.math;
4625
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
2
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
3 import java.util.Map;
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
4 import java.util.SortedMap;
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
5 import java.util.TreeMap;
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
6
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
7
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
8 public class MovingAverage
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
9 {
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
10
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
11 public static double[][] simple(double[][] values, double radius) {
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
12 TreeMap<Double, Double> map = toMap(values);
4631
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
13 int N = map.size();
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
14 double [] xs = new double[N];
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
15 double [] ys = new double[N];
4625
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
16 int ndx = 0;
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
17 for (double x: map.keySet()) {
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
18 SortedMap<Double, Double> range =
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
19 map.subMap(x-radius, true, x+radius, true);
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
20 double avg = 0d;
4631
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
21 for (double v: range.values()) {
4625
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
22 avg += v;
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
23 }
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
24 avg /= range.size();
4631
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
25 xs[ndx] = x;
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
26 ys[ndx] = avg;
4625
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
27 ndx++;
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
28 }
4631
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
29 return new double [][] { xs, ys };
4625
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
30 }
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
31
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
32 public static double[][] weighted(double[][] values, double radius) {
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
33 TreeMap<Double, Double> map = toMap(values);
4631
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
34 int N = map.size();
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
35 double [] xs = new double[N];
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
36 double [] ys = new double[N];
4625
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
37 int ndx = 0;
4631
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
38 double _1radius = 1d/radius;
4625
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
39 for (double x: map.keySet()) {
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
40 double avg = 0d;
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
41 double weights = 0d;
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
42 for (Map.Entry<Double, Double> e:
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
43 map.subMap(x-radius, false, x+radius, false).entrySet()
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
44 ) {
4631
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
45 double weight = 1d - Math.abs(x - e.getKey())*_1radius;
4625
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
46 avg += weight*e.getValue();
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
47 weights += weight;
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
48 }
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
49 avg /= weights;
4631
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
50 xs[ndx] = x;
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
51 ys[ndx] = avg;
4625
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
52 ndx++;
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
53 }
4631
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
54 return new double [][] { xs, ys };
4625
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
55 }
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
56
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
57 private static TreeMap<Double, Double> toMap(double[][] values) {
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
58 TreeMap<Double, Double> map = new TreeMap<Double, Double>();
4631
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
59 double [] xs = values[0];
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
60 double [] ys = values[1];
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
61 for (int i = 0; i < xs.length; i++) {
d35d316049e8 A little optimization and handle the case that there are duplicates in the values.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 4630
diff changeset
62 map.put(xs[i], ys[i]);
4625
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
63 }
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
64 return map;
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
65 }
d6d16b5ab2f0 New class for average calculation implementing moving average algorithms.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
66 }

http://dive4elements.wald.intevation.org