-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnusualValueDetector.java
99 lines (69 loc) · 3.22 KB
/
UnusualValueDetector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
public class UnusualValueDetector {
// the value = resultant of three axis-wise vector
private double resultant=0;
// object responsible for calculating standard deviation and mean
private StandardDeviationMean standardDeviationMean;
// state of last received value/sample
private boolean isValueUnusual = false;
// number of data used to calculate initial standard deviation & mean
// (this is connected to how much time you want to wait initially before starting to detect)
private final int initialSampleCount = 0;
// to respond to unusual value detection
private unusualValueResponse unusualValueResponse=null;
public UnusualValueDetector(StandardDeviationMean standardDeviationMean) {
// using the 'UnusualValueDetector' object without responder assigned
this.standardDeviationMean = standardDeviationMean;
}
public UnusualValueDetector(StandardDeviationMean standardDeviationMean,
UnusualValueDetector.unusualValueResponse unusualValueResponse
) {
this.standardDeviationMean = standardDeviationMean;
// assigning the responder class
this.unusualValueResponse = unusualValueResponse;
}
public void analyzeMotionSensorData(double x, double y, double z){
// calculate if current data(resultant) is unusual or not
// resultant of three axis-wise vectors
this.resultant = Math.sqrt(x*x + y*y + z*z);
// the resultants are the samples for mean & standard deviation calculation
standardDeviationMean.addSample(resultant);
// let mean & standard deviation get calculated with some initial data before starting to detect
if(standardDeviationMean.getNumberOfData()<initialSampleCount)
return ;
if( // X > (Mean + StandardDeviation)
isGreater(standardDeviationMean.getX(),
standardDeviationMean.getMean()+standardDeviationMean.getStandardDeviation())
||
// X < (Mean - StandardDeviation)
isGreater(standardDeviationMean.getMean()-standardDeviationMean.getStandardDeviation(),
standardDeviationMean.getX())
) {
isValueUnusual = true;
// call the response method if responder was assigned
if(unusualValueResponse!=null)
unusualValueResponse.unusualValueDetected("unusual value = "+standardDeviationMean.getX());
}
else
isValueUnusual = false;
}
private boolean isGreater(double a, double b){
// proper way to check inequality for 'double' datatype
double EPS = 1e-9;
return (a-b) > EPS; // true iff a>b
}
// getters & setters
public double getResultant() {
return resultant;
}
public StandardDeviationMean getStandardDeviationMean() {
return standardDeviationMean;
}
public boolean isValueUnusual() {
return isValueUnusual;
}
// implement this interface to respond to unusual value detection
public interface unusualValueResponse{
// invoked by the implementer when unusual value is detected
void unusualValueDetected(String message);
}
}