Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ca.concordia.ece390.VoltmeterApp;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Stores a list of VoltageMeasurement objects. Usually, the Measurements should be added
- * in chronological order of the measurement being taken.
- */
- public class MeasurementAccumulator {
- // You could define this as an interface and then a class implementing this interface...
- // but in this case we don't expect to have different kinds of implementations, so
- // just this one class will do.
- private List<VoltageMeasurement> myList = new ArrayList<VoltageMeasurement>();
- /** Add a voltage measurement to the list. */
- public void add(VoltageMeasurement v) {
- myList.add(v);
- }
- /** Clear the list of all measurements. */
- public void clear() {
- myList.clear();
- }
- /** Get the measurement at the specified index. Index = 0 is the oldest measurement, and
- * index = size() - 1 is the latest measurement. */
- public VoltageMeasurement get(int index) {
- return myList.get(index);
- }
- /** The number of measurements in this list. */
- public int size() {
- return myList.size();
- }
- /**
- * Get the waveform measurement at `index`, filtered through a FIR moving-average filter.
- *
- * The filter averages FOUR measurement at or before the given `index`. If less than four
- * measurements exist, then the average of all measurements are returned.
- *
- * @param index
- * @return The voltage at `index` after the waveform is passed through the filter
- */
- public VoltageMeasurement getFiltered(int index) {
- double tempVolt = 0.0;
- switch (index)
- {
- case 0: return myList.get(index);
- case 1: tempVolt = (myList.get(index).getVoltage() + myList.get(index-1).getVoltage()) / 2;
- return new VoltageMeasurement(myList.get(index).getTime(), tempVolt);
- case 2: tempVolt = (myList.get(index).getVoltage() + myList.get(index-1).getVoltage() + myList.get(index-2).getVoltage()) / 3;
- return new VoltageMeasurement(myList.get(index).getTime(), tempVolt);
- case 3: tempVolt = (myList.get(index).getVoltage() + myList.get(index-1).getVoltage() + myList.get(index-2).getVoltage() + myList.get(index-3).getVoltage()) / 4;
- return new VoltageMeasurement(myList.get(index).getTime(), tempVolt);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement