Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. package ca.concordia.ece390.VoltmeterApp;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /**
  7.  * Stores a list of VoltageMeasurement objects. Usually, the Measurements should be added
  8.  * in chronological order of the measurement being taken.
  9.  */
  10. public class MeasurementAccumulator {
  11.     // You could define this as an interface and then a class implementing this interface...
  12.     // but in this case we don't expect to have different kinds of implementations, so
  13.     // just this one class will do.
  14.     private List<VoltageMeasurement> myList = new ArrayList<VoltageMeasurement>();
  15.    
  16.     /** Add a voltage measurement to the list. */
  17.     public void add(VoltageMeasurement v) {
  18.         myList.add(v);
  19.     }
  20.  
  21.     /** Clear the list of all measurements. */
  22.     public void clear() {
  23.         myList.clear();
  24.     }
  25.  
  26.     /** Get the measurement at the specified index. Index = 0 is the oldest measurement, and
  27.      * index = size() - 1 is the latest measurement. */
  28.     public VoltageMeasurement get(int index) {
  29.         return myList.get(index);
  30.     }
  31.    
  32.     /** The number of measurements in this list. */
  33.     public int size() {
  34.         return myList.size();
  35.     }
  36.    
  37.     /**
  38.      * Get the waveform measurement at `index`, filtered through a FIR moving-average filter.
  39.      *
  40.      * The filter averages FOUR measurement at or before the given `index`. If less than four
  41.      * measurements exist, then the average of all measurements are returned.
  42.      *
  43.      * @param index
  44.      * @return The voltage at `index` after the waveform is passed through the filter
  45.      */
  46.     public VoltageMeasurement getFiltered(int index) {
  47.         double tempVolt = 0.0;
  48.         switch (index)
  49.         {
  50.         case 0: return myList.get(index);
  51.         case 1: tempVolt = (myList.get(index).getVoltage() + myList.get(index-1).getVoltage()) / 2;
  52.                 return new VoltageMeasurement(myList.get(index).getTime(), tempVolt);
  53.                
  54.         case 2: tempVolt = (myList.get(index).getVoltage() + myList.get(index-1).getVoltage() + myList.get(index-2).getVoltage()) / 3;
  55.                 return new VoltageMeasurement(myList.get(index).getTime(), tempVolt);
  56.                
  57.         case 3: tempVolt = (myList.get(index).getVoltage() + myList.get(index-1).getVoltage() + myList.get(index-2).getVoltage() + myList.get(index-3).getVoltage()) / 4;
  58.                 return new VoltageMeasurement(myList.get(index).getTime(), tempVolt);
  59.                
  60.         }
  61.         return null;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement