comparison artifacts/src/main/java/org/dive4elements/river/artifacts/math/MovingAverage.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/math/MovingAverage.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.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 int N = map.size();
14 double [] xs = new double[N];
15 double [] ys = new double[N];
16 int ndx = 0;
17 for (double x: map.keySet()) {
18 SortedMap<Double, Double> range =
19 map.subMap(x-radius, true, x+radius, true);
20 double avg = 0d;
21 for (double v: range.values()) {
22 avg += v;
23 }
24 avg /= range.size();
25 xs[ndx] = x;
26 ys[ndx] = avg;
27 ndx++;
28 }
29 return new double [][] { xs, ys };
30 }
31
32 public static double[][] weighted(double[][] values, double radius) {
33 TreeMap<Double, Double> map = toMap(values);
34 int N = map.size();
35 double [] xs = new double[N];
36 double [] ys = new double[N];
37 int ndx = 0;
38 double _1radius = 1d/radius;
39 for (double x: map.keySet()) {
40 double avg = 0d;
41 double weights = 0d;
42 for (Map.Entry<Double, Double> e:
43 map.subMap(x-radius, false, x+radius, false).entrySet()
44 ) {
45 double weight = 1d - Math.abs(x - e.getKey())*_1radius;
46 avg += weight*e.getValue();
47 weights += weight;
48 }
49 avg /= weights;
50 xs[ndx] = x;
51 ys[ndx] = avg;
52 ndx++;
53 }
54 return new double [][] { xs, ys };
55 }
56
57 private static TreeMap<Double, Double> toMap(double[][] values) {
58 TreeMap<Double, Double> map = new TreeMap<Double, Double>();
59 double [] xs = values[0];
60 double [] ys = values[1];
61 for (int i = 0; i < xs.length; i++) {
62 map.put(xs[i], ys[i]);
63 }
64 return map;
65 }
66 }
67 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org