
net.intelie.liverig.plugin.widgets.Derivatives Maven / Gradle / Ivy
Show all versions of plugin-assets Show documentation
package net.intelie.liverig.plugin.widgets;
import java.util.ArrayList;
import java.util.List;
public class Derivatives {
private Double firstAverage;
private List firstPoints;
private Double secondAverage;
private List secondPoints;
public Double getFirstAverage() {
return firstAverage;
}
public void setFirstAverage(Double firstAverage) {
this.firstAverage = firstAverage;
}
public List getFirstPoints() {
return firstPoints;
}
public void setFirstPoints(List firstPoints) {
this.firstPoints = firstPoints;
}
public Double getSecondAverage() {
return secondAverage;
}
public void setSecondAverage(Double secondAverage) {
this.secondAverage = secondAverage;
}
public List getSecondPoints() {
return secondPoints;
}
public void setSecondPoints(List secondPoints) {
this.secondPoints = secondPoints;
}
/**
* @param p1 First point
* @param p2 Second point
* @param factor Time unit: time is given in mls multiplying it by 1000 we can get it in s
* @return The slope of the line between points p1 and p2
*/
private Double slopeOfSegment(CurvePoint p1, CurvePoint p2, Double factor) {
return ((p2.getY() - p1.getY()) / (p2.getX() - p1.getX())) * factor;
}
/**
* The average rate of change of a function y = f(x)
* from x = a to x = b is given by:
* ( f(b) - f(a) ) / (b - a)
*
* Here we use a = first and b = last
*
* @param y Curve original points
* @return The average of the points of y'
*/
private Double averageFirstDerivative(List y, Double factor) {
return slopeOfSegment(y.get(0), y.get(y.size() - 1), factor);
}
/**
* Computes an approximated first derivative by calculating
* the slope of each segment of y
*
* @param y Original curve points
* @return Approximated points of y'
*/
private List firstDerivative(List y, Double factor) {
List dydx = new ArrayList<>();
for (int i = 0; i < y.size() - 1; i++) {
Double slp = slopeOfSegment(y.get(i), y.get(i + 1), factor);
dydx.add(new CurvePoint(y.get(i).getTimestamp(), y.get(i).getX(), slp));
}
return dydx;
}
/**
* @param y2 Approximated second derivative points
* @return Average of the points of y2
*/
private Double averageSecondDerivative(List y2, Double factor) {
return slopeOfSegment(y2.get(0), y2.get(y2.size() - 1), factor);
}
/**
* Computes an approximated second derivative by calculating
* the slope of each segment of y'
*
* @param y1 First derivative points
* @return Approximated points of y''
*/
private List secondDerivative(List y1, Double factor) {
List dydx = new ArrayList<>();
for (int i = 0; i < y1.size() - 1; i++) {
Double slp = slopeOfSegment(y1.get(i), y1.get(i + 1), factor);
dydx.add(new CurvePoint(y1.get(i).getTimestamp(), y1.get(i).getX(), slp));
}
return dydx;
}
public void calculate(List originalPoints, Double factor) {
if (originalPoints.size() > 1) {
firstAverage = averageFirstDerivative(originalPoints, factor);
firstPoints = firstDerivative(originalPoints, factor);
}
if (firstPoints != null && firstPoints.size() > 1) {
secondPoints = secondDerivative(firstPoints, factor);
if (secondPoints.size() > 1) {
secondAverage = averageSecondDerivative(secondPoints, factor);
}
}
}
}