~opensatnav-admins/opensatnav/nice-package-rename

« back to all changes in this revision

Viewing changes to src/org/opensatnav/stats/DoubleBuffer.java

  • Committer: evolvedlight
  • Date: 2010-08-09 19:58:34 UTC
  • mfrom: (146.2.29 turn-by-turn)
  • mto: This revision was merged to the branch mainline in revision 163.
  • Revision ID: steve@evolvedlight.co.uk-20100809195834-5p9tmlvitgs6rnhl
Merged with main code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2009 Google Inc.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 
5
 * use this file except in compliance with the License. You may obtain a copy of
 
6
 * the License at
 
7
 * 
 
8
 * http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
13
 * License for the specific language governing permissions and limitations under
 
14
 * the License.
 
15
 */
 
16
 
 
17
package org.opensatnav.stats;
 
18
 
 
19
/**
 
20
 * This class maintains a buffer of doubles. This buffer is a convenient class
 
21
 * for storing a series of doubles and calculating information about them. This
 
22
 * is a FIFO buffer.
 
23
 *
 
24
 * @author Sandor Dornbush
 
25
 */
 
26
public class DoubleBuffer {
 
27
 
 
28
  /**
 
29
   * The location that the next write will occur at.
 
30
   */
 
31
  private int index;
 
32
 
 
33
  /**
 
34
   * The sliding buffer of doubles.
 
35
   */
 
36
  private final double[] buffer;
 
37
 
 
38
  /**
 
39
   * Have all of the slots in the buffer been filled?
 
40
   */
 
41
  private boolean isFull;
 
42
 
 
43
  /**
 
44
   * Creates a buffer with size elements.
 
45
   *
 
46
   * @param size the number of elements in the buffer
 
47
   * @throws IllegalArgumentException if the size is not a positive value
 
48
   */
 
49
  public DoubleBuffer(int size) {
 
50
    if (size < 1) {
 
51
      throw new IllegalArgumentException("The buffer size must be positive.");
 
52
    }
 
53
    buffer = new double[size];
 
54
    reset();
 
55
  }
 
56
 
 
57
  /**
 
58
   * Adds a double to the buffer. If the buffer is full the oldest element is
 
59
   * overwritten.
 
60
   *
 
61
   * @param d the double to add
 
62
   */
 
63
  public void setNext(double d) {
 
64
    if (index == buffer.length) {
 
65
      index = 0;
 
66
    }
 
67
    buffer[index] = d;
 
68
    index++;
 
69
    if (index == buffer.length) {
 
70
      isFull = true;
 
71
    }
 
72
  }
 
73
 
 
74
  /**
 
75
   * Are all of the entries in the buffer used?
 
76
   */
 
77
  public boolean isFull() {
 
78
    return isFull;
 
79
  }
 
80
 
 
81
  /**
 
82
   * Resets the buffer to the initial state.
 
83
   */
 
84
  public void reset() {
 
85
    index = 0;
 
86
    isFull = false;
 
87
  }
 
88
 
 
89
  /**
 
90
   * Gets the average of values from the buffer.
 
91
   *
 
92
   * @return The average of the buffer
 
93
   */
 
94
  public double getAverage() {
 
95
    int numberOfEntries = isFull ? buffer.length : index;
 
96
    if (numberOfEntries == 0) {
 
97
      return 0;
 
98
    }
 
99
 
 
100
    double sum = 0;
 
101
    for (int i = 0; i < numberOfEntries; i++) {
 
102
      sum += buffer[i];
 
103
    }
 
104
    return sum / numberOfEntries;
 
105
  }
 
106
 
 
107
  /**
 
108
   * Gets the average and standard deviation of the buffer.
 
109
   *
 
110
   * @return An array of two elements - the first is the average, and the second
 
111
   *         is the variance
 
112
   */
 
113
  public double[] getAverageAndVariance() {
 
114
    int numberOfEntries = isFull ? buffer.length : index;
 
115
    if (numberOfEntries == 0) {
 
116
      return new double[]{0, 0};
 
117
    }
 
118
 
 
119
    double sum = 0;
 
120
    double sumSquares = 0;
 
121
    for (int i = 0; i < numberOfEntries; i++) {
 
122
      sum += buffer[i];
 
123
      sumSquares += Math.pow(buffer[i], 2);
 
124
    }
 
125
 
 
126
    double average = sum / numberOfEntries;
 
127
    return new double[]{average,
 
128
      sumSquares / numberOfEntries  - Math.pow(average, 2)};
 
129
  }
 
130
 
 
131
  @Override
 
132
  public String toString() {
 
133
    StringBuffer stringBuffer = new StringBuffer("Full: ");
 
134
    stringBuffer.append(isFull);
 
135
    stringBuffer.append("\n");
 
136
    for (int i = 0; i < buffer.length; i++) {
 
137
      stringBuffer.append((i == index) ? "<<" : "[");
 
138
      stringBuffer.append(buffer[i]);
 
139
      stringBuffer.append((i == index) ? ">> " : "] ");
 
140
    }
 
141
    return stringBuffer.toString();
 
142
  }
 
143
}