Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- This class collects a number of values and counts the peaks and valleys.
- */
- public class DataSet
- {
- private double lastValue;
- private int wasCanidate;
- private double precedingValue;
- private int count;
- public int peaks;
- public int valleys;
- /**
- Constructs a new data set with no data.
- */
- public DataSet()
- {
- lastValue = 0;
- wasCanidate = 0;
- precedingValue = 0;
- count = 0;
- peaks = 0;
- valleys = 0;
- }
- /**
- Adds a value to this data set.
- @param valueToAdd the value to add to this data set
- */
- public void add(double valueToAdd)
- {
- if(count == 0){lastValue = valueToAdd; count++; return;}
- else if(valueToAdd < lastValue && precedingValue < lastValue)
- {
- peaks++;
- precedingValue = lastValue;
- lastValue = valueToAdd;
- count++;
- return;
- }
- else if(valueToAdd > lastValue && precedingValue > lastValue)
- {
- valleys++;
- precedingValue = lastValue;
- lastValue = valueToAdd;
- count++;
- return;
- }
- precedingValue = lastValue;
- lastValue = valueToAdd;
- }
- /**
- Gets the number of peaks in this data set.
- @return the number of peaks in this data set
- */
- public int getPeakCount()
- {
- return peaks;
- }
- /**
- Gets the number of valleys in this data set.
- @return the number of valleys in this data set
- */
- public int getValleyCount()
- {
- return valleys;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement